-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_operations.py
More file actions
45 lines (35 loc) · 2.41 KB
/
docker_operations.py
File metadata and controls
45 lines (35 loc) · 2.41 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
import subprocess # Importing the `subprocess` module for running shell commands
from pathlib import Path # Importing the `Path` class from the `pathlib` module for working with file paths
def start_container_from_compose(compose_file):
try:
# Running the `docker-compose` command to start containers in detached mode (-d flag)
subprocess.run(["docker-compose", "-f", compose_file, "up", "-d"], check=True)
print(f"Containers started successfully using Docker Compose: {compose_file}")
except subprocess.CalledProcessError as e:
# Handling any errors that occur during the process
print(f"An error occurred while starting the containers using Docker Compose: {e}")
def stop_and_remove_container_from_compose(compose_file):
try:
# Listing the images used by the created containers using `docker-compose images` command
images = subprocess.check_output(["docker-compose", "-f", compose_file, "images", "-q"]).decode().splitlines()
# Stopping the containers using `docker-compose down` command
subprocess.run(["docker-compose", "-f", compose_file, "down"], check=True)
print(f"Containers stopped and removed successfully using Docker Compose: {compose_file}")
# Removing the Docker Compose services using `docker-compose rm` command
subprocess.run(["docker-compose", "-f", compose_file, "rm", "-fsv"], check=True)
print(f"Docker Compose services removed successfully: {compose_file}")
# Removing the images using `docker image rm` command
for image in images:
subprocess.run(["docker", "image", "rm", image], check=True)
print("Docker images deleted successfully")
except subprocess.CalledProcessError as e:
print(f"An error occurred while stopping or removing the containers using Docker Compose: {e}")
# Example usage
compose_file = Path("docker-compose.yaml") # Creating a `Path` object for the Docker Compose file path
start = 1 # Set to 1 to start the containers from Docker Compose
if start == 1:
start_container_from_compose(compose_file) # Calling the function to start the containers
else:
stop_and_remove_container_from_compose(compose_file) # Calling the function to stop and remove the containers
# Copies the binary_options directory from the host machine to the /app/binary_options directory inside the Docker image.
# COPY binary_options/ /app/binary_options/