-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleDockerProjectRH.py
More file actions
94 lines (73 loc) · 2.53 KB
/
Copy pathSimpleDockerProjectRH.py
File metadata and controls
94 lines (73 loc) · 2.53 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
import subprocess
import os
def run_command(command, cwd=None):
print(f"\n🔹 Executing: {command}")
subprocess.run(command, shell=True, check=True, cwd=cwd)
def write_file(filename, content):
with open(filename, "w") as f:
f.write(content)
print(f"✅ Created: {filename}")
def main():
# 🔹 System Preparation
run_command("sudo dnf update -y")
run_command("sudo dnf install -y yum-utils curl")
# 🔹 Install Docker Engine
run_command("sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo")
run_command("sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin")
run_command("sudo systemctl enable --now docker")
run_command("sudo systemctl status docker")
run_command("docker --version")
# 🔹 Docker Compose Plugin
run_command("docker compose version")
# 🔹 Optional: Install legacy docker-compose binary
run_command('sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose')
run_command("sudo chmod +x /usr/local/bin/docker-compose")
run_command("docker-compose --version")
# 🔹 Install Python and Pip
run_command("sudo dnf install -y python3 python3-pip")
run_command("python3 -m pip install --upgrade pip")
# 🔹 Create Project Directory
os.makedirs("docker-projects", exist_ok=True)
os.chdir("docker-projects")
# 🔹 Create Python Web App
write_file("app.py", """from flask import Flask
import redis
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError:
return 1
@app.route('/')
def hello():
count = get_hit_count()
return f"Hello World! You have visited {count} times."
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
""")
# 🔹 Define Dependencies
write_file("requirements.txt", "Flask\nredis\n")
# 🔹 Create Dockerfile
write_file("Dockerfile", """FROM python:3.9
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
""")
# 🔹 Configure Docker Compose
write_file("docker-compose.yml", """version: '3'
services:
web:
build: .
ports:
- "5000:5000"
depends_on:
- redis
redis:
image: "redis:alpine"
""")
# 🔹 Build and Run the Project
run_command("docker compose up --build")
if __name__ == "__main__":
main()