-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtemplate_usage.rs
More file actions
153 lines (130 loc) · 5 KB
/
template_usage.rs
File metadata and controls
153 lines (130 loc) · 5 KB
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
//! Example demonstrating Docker template usage for common containers
//!
//! This example shows how to use pre-configured templates to quickly
//! spin up development environments with Redis, Redis Sentinel, PostgreSQL,
//! MySQL, MongoDB, and Nginx.
#[cfg(feature = "templates")]
use docker_wrapper::{
MongodbConnectionString, MongodbTemplate, MysqlConnectionString, MysqlTemplate, NginxTemplate,
PostgresConnectionString, PostgresTemplate, RedisTemplate, Template,
};
#[cfg(feature = "templates")]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Docker Template Examples\n");
// Example 1: Redis with persistence
println!("1. Starting Redis with persistence...");
let redis = RedisTemplate::new("example-redis")
.port(16379)
.password("secure_password")
.with_persistence("redis-data")
.memory_limit("256m");
let redis_id = redis.start().await?;
println!(" Redis started with ID: {}", redis_id);
// Example 2: PostgreSQL with custom configuration
println!("\n2. Starting PostgreSQL...");
let postgres = PostgresTemplate::new("example-postgres")
.database("myapp")
.user("appuser")
.password("apppass")
.port(15432)
.with_persistence("postgres-data");
let postgres_id = postgres.start().await?;
println!(" PostgreSQL started with ID: {}", postgres_id);
// Get connection string
let conn_str = PostgresConnectionString::from_template(&postgres);
println!(" Connection URL: {}", conn_str.url());
println!(" Connection string: {}", conn_str.key_value());
// Example 3: MySQL with initialization scripts
println!("\n3. Starting MySQL...");
let mysql = MysqlTemplate::new("example-mysql")
.database("testdb")
.user("testuser")
.password("testpass")
.root_password("rootpass")
.port(13306)
.character_set("utf8mb4")
.collation("utf8mb4_unicode_ci");
let mysql_id = mysql.start().await?;
println!(" MySQL started with ID: {}", mysql_id);
let mysql_conn = MysqlConnectionString::from_template(&mysql);
println!(" Connection URL: {}", mysql_conn.url());
println!(" JDBC URL: {}", mysql_conn.jdbc());
// Example 4: MongoDB with authentication
println!("\n4. Starting MongoDB...");
let mongodb = MongodbTemplate::new("example-mongodb")
.root_username("admin")
.root_password("adminpass")
.database("appdb")
.port(27018)
.with_auth()
.with_persistence("mongo-data");
let mongodb_id = mongodb.start().await?;
println!(" MongoDB started with ID: {}", mongodb_id);
let mongo_conn = MongodbConnectionString::from_template(&mongodb);
println!(" Connection URL: {}", mongo_conn.url());
// Example 5: Nginx web server
println!("\n5. Starting Nginx...");
let nginx = NginxTemplate::new("example-nginx")
.port(8080)
.https_port(8443)
.memory_limit("128m");
let nginx_id = nginx.start().await?;
println!(" Nginx started with ID: {}", nginx_id);
println!(" Access at: http://localhost:8080");
// Check if containers are running
println!("\n6. Checking container status...");
if redis.is_running().await? {
println!(" Redis is running");
}
if postgres.is_running().await? {
println!(" PostgreSQL is running");
}
if mysql.is_running().await? {
println!(" MySQL is running");
}
if mongodb.is_running().await? {
println!(" MongoDB is running");
}
if nginx.is_running().await? {
println!(" Nginx is running");
}
// Get logs from a container
println!("\n7. Fetching Redis logs...");
let logs = redis.logs(false, Some("10")).await?;
println!(" Last 10 lines of Redis logs:");
for line in logs.stdout.lines().take(10) {
println!(" {}", line);
}
// Execute command in container
println!("\n8. Testing Redis connection...");
let exec_result = redis.exec(vec!["redis-cli", "ping"]).await?;
println!(" Redis PING response: {}", exec_result.stdout.trim());
// Clean up
println!("\n9. Cleaning up containers...");
println!(" Stopping Redis...");
redis.stop().await?;
println!(" Stopping PostgreSQL...");
postgres.stop().await?;
println!(" Stopping MySQL...");
mysql.stop().await?;
println!(" Stopping MongoDB...");
mongodb.stop().await?;
println!(" Stopping Nginx...");
nginx.stop().await?;
// Remove containers
println!(" Removing containers...");
redis.remove().await?;
postgres.remove().await?;
mysql.remove().await?;
mongodb.remove().await?;
nginx.remove().await?;
println!("\nAll containers cleaned up successfully!");
Ok(())
}
#[cfg(not(feature = "templates"))]
fn main() {
eprintln!("This example requires the 'templates' feature to be enabled.");
eprintln!("Run with: cargo run --features templates --example template_usage");
std::process::exit(1);
}