docker-wrapper 0.11.1

A Docker CLI wrapper for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! MySQL template for quick MySQL container setup

#![allow(clippy::doc_markdown)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::return_self_not_must_use)]
#![allow(clippy::map_unwrap_or)]
#![allow(clippy::format_push_string)]
#![allow(clippy::uninlined_format_args)]

use crate::template::{HealthCheck, Template, TemplateConfig, VolumeMount};
use async_trait::async_trait;
use std::collections::HashMap;

/// MySQL container template with sensible defaults
pub struct MysqlTemplate {
    config: TemplateConfig,
}

impl MysqlTemplate {
    /// Create a new MySQL template with default settings
    pub fn new(name: impl Into<String>) -> Self {
        let name = name.into();
        let mut env = HashMap::new();

        // Default MySQL configuration
        env.insert("MYSQL_ROOT_PASSWORD".to_string(), "mysql".to_string());
        env.insert("MYSQL_DATABASE".to_string(), "mysql".to_string());

        let config = TemplateConfig {
            name: name.clone(),
            image: "mysql".to_string(),
            tag: "8.0".to_string(),
            ports: vec![(3306, 3306)],
            env,
            volumes: Vec::new(),
            network: None,
            health_check: Some(HealthCheck {
                test: vec![
                    "mysqladmin".to_string(),
                    "ping".to_string(),
                    "-h".to_string(),
                    "localhost".to_string(),
                ],
                interval: "10s".to_string(),
                timeout: "5s".to_string(),
                retries: 5,
                start_period: "30s".to_string(),
            }),
            auto_remove: false,
            memory_limit: None,
            cpu_limit: None,
            platform: None,
        };

        Self { config }
    }

    /// Set a custom MySQL port
    pub fn port(mut self, port: u16) -> Self {
        self.config.ports = vec![(port, 3306)];
        self
    }

    /// Set root password
    pub fn root_password(mut self, password: impl Into<String>) -> Self {
        self.config
            .env
            .insert("MYSQL_ROOT_PASSWORD".to_string(), password.into());
        self
    }

    /// Set database name
    pub fn database(mut self, db: impl Into<String>) -> Self {
        self.config
            .env
            .insert("MYSQL_DATABASE".to_string(), db.into());
        self
    }

    /// Set database user (non-root)
    pub fn user(mut self, user: impl Into<String>) -> Self {
        self.config
            .env
            .insert("MYSQL_USER".to_string(), user.into());
        self
    }

    /// Set database user password
    pub fn password(mut self, password: impl Into<String>) -> Self {
        self.config
            .env
            .insert("MYSQL_PASSWORD".to_string(), password.into());
        self
    }

    /// Allow empty password for root (development only!)
    pub fn allow_empty_password(mut self) -> Self {
        self.config.env.remove("MYSQL_ROOT_PASSWORD");
        self.config
            .env
            .insert("MYSQL_ALLOW_EMPTY_PASSWORD".to_string(), "yes".to_string());
        self
    }

    /// Set random root password
    pub fn random_root_password(mut self) -> Self {
        self.config.env.remove("MYSQL_ROOT_PASSWORD");
        self.config
            .env
            .insert("MYSQL_RANDOM_ROOT_PASSWORD".to_string(), "yes".to_string());
        self
    }

    /// Enable persistence with a volume
    pub fn with_persistence(mut self, volume_name: impl Into<String>) -> Self {
        self.config.volumes.push(VolumeMount {
            source: volume_name.into(),
            target: "/var/lib/mysql".to_string(),
            read_only: false,
        });
        self
    }

    /// Mount initialization scripts directory
    pub fn init_scripts(mut self, scripts_path: impl Into<String>) -> Self {
        self.config.volumes.push(VolumeMount {
            source: scripts_path.into(),
            target: "/docker-entrypoint-initdb.d".to_string(),
            read_only: true,
        });
        self
    }

    /// Mount custom MySQL configuration
    pub fn config_file(mut self, config_path: impl Into<String>) -> Self {
        self.config.volumes.push(VolumeMount {
            source: config_path.into(),
            target: "/etc/mysql/conf.d/custom.cnf".to_string(),
            read_only: true,
        });
        self
    }

    /// Set memory limit for MySQL
    pub fn memory_limit(mut self, limit: impl Into<String>) -> Self {
        self.config.memory_limit = Some(limit.into());
        self
    }

    /// Set character set
    pub fn character_set(mut self, charset: impl Into<String>) -> Self {
        let charset = charset.into();
        self.config
            .env
            .insert("MYSQL_CHARSET".to_string(), charset.clone());
        let current_cmd = self
            .config
            .env
            .get("MYSQL_COMMAND")
            .map(|s| format!("{} --character-set-server={}", s, charset))
            .unwrap_or_else(|| format!("--character-set-server={}", charset));
        self.config
            .env
            .insert("MYSQL_COMMAND".to_string(), current_cmd);
        self
    }

    /// Set collation
    pub fn collation(mut self, collation: impl Into<String>) -> Self {
        let collation = collation.into();
        self.config
            .env
            .insert("MYSQL_COLLATION".to_string(), collation.clone());
        let current_cmd = self
            .config
            .env
            .get("MYSQL_COMMAND")
            .map(|s| format!("{} --collation-server={}", s, collation))
            .unwrap_or_else(|| format!("--collation-server={}", collation));
        self.config
            .env
            .insert("MYSQL_COMMAND".to_string(), current_cmd);
        self
    }

    /// Use a specific MySQL version
    pub fn version(mut self, version: impl Into<String>) -> Self {
        self.config.tag = version.into();
        self
    }

    /// Connect to a specific network
    pub fn network(mut self, network: impl Into<String>) -> Self {
        self.config.network = Some(network.into());
        self
    }

    /// Enable auto-remove when stopped
    pub fn auto_remove(mut self) -> Self {
        self.config.auto_remove = true;
        self
    }

    /// Use a custom image and tag
    pub fn custom_image(mut self, image: impl Into<String>, tag: impl Into<String>) -> Self {
        self.config.image = image.into();
        self.config.tag = tag.into();
        self
    }

    /// Set the platform for the container (e.g., "linux/arm64", "linux/amd64")
    pub fn platform(mut self, platform: impl Into<String>) -> Self {
        self.config.platform = Some(platform.into());
        self
    }
}

#[async_trait]
impl Template for MysqlTemplate {
    fn name(&self) -> &str {
        &self.config.name
    }

    fn config(&self) -> &TemplateConfig {
        &self.config
    }

    fn config_mut(&mut self) -> &mut TemplateConfig {
        &mut self.config
    }

    fn build_command(&self) -> crate::RunCommand {
        let config = self.config();
        let image_tag = format!("{}:{}", config.image, config.tag);

        let mut cmd = crate::RunCommand::new(image_tag)
            .name(&config.name)
            .detach();

        // Add port mappings
        for (host, container) in &config.ports {
            cmd = cmd.port(*host, *container);
        }

        // Add volume mounts
        for mount in &config.volumes {
            if mount.read_only {
                cmd = cmd.volume_ro(&mount.source, &mount.target);
            } else {
                cmd = cmd.volume(&mount.source, &mount.target);
            }
        }

        // Add network
        if let Some(network) = &config.network {
            cmd = cmd.network(network);
        }

        // Add environment variables
        for (key, value) in &config.env {
            // Skip MYSQL_COMMAND as it's not a real env var
            if key != "MYSQL_COMMAND" {
                cmd = cmd.env(key, value);
            }
        }

        // Add health check
        if let Some(health) = &config.health_check {
            cmd = cmd
                .health_cmd(&health.test.join(" "))
                .health_interval(&health.interval)
                .health_timeout(&health.timeout)
                .health_retries(health.retries)
                .health_start_period(&health.start_period);
        }

        // Add resource limits
        if let Some(memory) = &config.memory_limit {
            cmd = cmd.memory(memory);
        }

        if let Some(cpu) = &config.cpu_limit {
            cmd = cmd.cpus(cpu);
        }

        // Auto-remove
        if config.auto_remove {
            cmd = cmd.remove();
        }

        // Add platform if specified
        if let Some(platform) = &config.platform {
            cmd = cmd.platform(platform);
        }

        // Add MySQL-specific command args for charset and collation
        if let Some(mysql_cmd) = config.env.get("MYSQL_COMMAND") {
            // Parse the command string to get individual arguments
            let args: Vec<String> = mysql_cmd
                .split_whitespace()
                .map(|s| s.to_string())
                .collect();
            if !args.is_empty() {
                // Override the default command with mysqld and our custom args
                cmd = cmd.cmd(std::iter::once("mysqld".to_string()).chain(args).collect());
            }
        }

        cmd
    }

    async fn wait_for_ready(&self) -> crate::template::Result<()> {
        use std::time::Duration;
        use tokio::time::{sleep, timeout};

        // Custom MySQL readiness check - increased timeout for charset/collation configs
        // MySQL 8.0 can take 90+ seconds to initialize on slower CI systems
        let wait_timeout = Duration::from_secs(120);
        let check_interval = Duration::from_millis(1000);

        timeout(wait_timeout, async {
            let mut consecutive_successes = 0;
            loop {
                // Check if container is running - keep retrying if not yet started
                // Don't fail immediately as the container may still be starting up
                if !self.is_running().await.unwrap_or(false) {
                    consecutive_successes = 0;
                    sleep(check_interval).await;
                    continue;
                }

                // Try to connect to MySQL using the mysql client with an actual query
                // Use 127.0.0.1 instead of localhost to force TCP connection
                // (localhost uses Unix socket which may not be ready even when TCP is)
                let password = self
                    .config
                    .env
                    .get("MYSQL_ROOT_PASSWORD")
                    .or_else(|| self.config.env.get("MYSQL_PASSWORD"))
                    .map(|s| s.as_str())
                    .unwrap_or("mysql");

                let password_arg = format!("-p{}", password);
                let check_cmd = vec![
                    "mysql",
                    "-h",
                    "127.0.0.1",
                    "-u",
                    "root",
                    &password_arg,
                    "-e",
                    "SELECT 1",
                ];

                // Execute readiness check
                if let Ok(result) = self.exec(check_cmd).await {
                    // If we got output containing '1', MySQL responded successfully
                    if result.stdout.contains('1') {
                        consecutive_successes += 1;
                        // Require 2 consecutive successes to ensure stability
                        if consecutive_successes >= 2 {
                            return Ok(());
                        }
                        sleep(Duration::from_millis(500)).await;
                        continue;
                    }
                }

                consecutive_successes = 0;
                sleep(check_interval).await;
            }
        })
        .await
        .map_err(|_| {
            crate::template::TemplateError::InvalidConfig(format!(
                "MySQL container {} failed to become ready within timeout",
                self.config().name
            ))
        })?
    }
}

/// Builder for MySQL connection strings
pub struct MysqlConnectionString {
    host: String,
    port: u16,
    database: String,
    user: String,
    password: String,
}

impl MysqlConnectionString {
    /// Create from a MysqlTemplate
    pub fn from_template(template: &MysqlTemplate) -> Self {
        let config = template.config();
        let port = config.ports.first().map(|(h, _)| *h).unwrap_or(3306);

        // Determine user and password
        let (user, password) = if let Some(user) = config.env.get("MYSQL_USER") {
            let password = config
                .env
                .get("MYSQL_PASSWORD")
                .cloned()
                .unwrap_or_default();
            (user.clone(), password)
        } else {
            let password = config
                .env
                .get("MYSQL_ROOT_PASSWORD")
                .cloned()
                .unwrap_or_else(|| "mysql".to_string());
            ("root".to_string(), password)
        };

        Self {
            host: "localhost".to_string(),
            port,
            database: config
                .env
                .get("MYSQL_DATABASE")
                .cloned()
                .unwrap_or_else(|| "mysql".to_string()),
            user,
            password,
        }
    }

    /// Get the connection string in MySQL URL format
    pub fn url(&self) -> String {
        format!(
            "mysql://{}:{}@{}:{}/{}",
            self.user, self.password, self.host, self.port, self.database
        )
    }

    /// Get the connection string for JDBC
    pub fn jdbc(&self) -> String {
        format!(
            "jdbc:mysql://{}:{}/{}?user={}&password={}",
            self.host, self.port, self.database, self.user, self.password
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mysql_template_basic() {
        let template = MysqlTemplate::new("test-mysql");
        assert_eq!(template.name(), "test-mysql");
        assert_eq!(template.config().image, "mysql");
        assert_eq!(template.config().tag, "8.0");
        assert_eq!(template.config().ports, vec![(3306, 3306)]);
    }

    #[test]
    fn test_mysql_template_custom_config() {
        let template = MysqlTemplate::new("test-mysql")
            .database("mydb")
            .user("myuser")
            .password("secret123")
            .port(13306);

        assert_eq!(
            template.config().env.get("MYSQL_DATABASE"),
            Some(&"mydb".to_string())
        );
        assert_eq!(
            template.config().env.get("MYSQL_USER"),
            Some(&"myuser".to_string())
        );
        assert_eq!(
            template.config().env.get("MYSQL_PASSWORD"),
            Some(&"secret123".to_string())
        );
        assert_eq!(template.config().ports, vec![(13306, 3306)]);
    }

    #[test]
    fn test_mysql_template_with_persistence() {
        let template = MysqlTemplate::new("test-mysql").with_persistence("mysql-data");

        assert_eq!(template.config().volumes.len(), 1);
        assert_eq!(template.config().volumes[0].source, "mysql-data");
        assert_eq!(template.config().volumes[0].target, "/var/lib/mysql");
    }

    #[test]
    fn test_mysql_connection_string() {
        let template = MysqlTemplate::new("test-mysql")
            .database("testdb")
            .user("testuser")
            .password("testpass")
            .port(13306);

        let conn = MysqlConnectionString::from_template(&template);

        assert_eq!(
            conn.url(),
            "mysql://testuser:testpass@localhost:13306/testdb"
        );

        assert_eq!(
            conn.jdbc(),
            "jdbc:mysql://localhost:13306/testdb?user=testuser&password=testpass"
        );
    }
}