Python > Deployment and Distribution > Deployment Platforms > Container Orchestration (e.g., Kubernetes, Docker Swarm)
Docker Swarm Compose File (docker-compose.yml)
This snippet provides a Docker Compose file for deploying a Python application using Docker Swarm. It defines a service for your Python application, specifies the Docker image, ports, and environment variables. Docker Swarm provides a simpler alternative to Kubernetes for orchestrating containerized applications.
Understanding the Docker Compose Structure
version: '3.8': Specifies the Docker Compose file version.
services:: Defines the services that will be deployed.
my-python-app:: The name of the service.
image: your-dockerhub-username/my-python-app:latest: The Docker image to use for the service (replace with your image).
ports:: Maps ports between the host and the container.
"8000:8000": Maps port 8000 on the host to port 8000 in the container.
environment:: Defines environment variables for the container.
- DEBUG=True: Sets the DEBUG
environment variable to True
.
deploy:: Specifies deployment configuration for Docker Swarm.
replicas: 3: Specifies that we want 3 replicas (instances) of the service running.
resources:: Defines resource limits for the container.
limits:: The maximum amount of resources the container can use.
cpu: '0.5': Limits CPU usage to 0.5 CPU cores.
memory: 512M: Limits memory usage to 512MB.
restart_policy:: Specifies when to restart the container.
condition: on-failure: Restarts the container if it fails.
version: '3.8'
services:
my-python-app:
image: your-dockerhub-username/my-python-app:latest
ports:
- "8000:8000"
environment:
- DEBUG=True
deploy:
replicas: 3
resources:
limits:
cpu: '0.5'
memory: 512M
restart_policy:
condition: on-failure
Real-Life Use Case
You have a small to medium-sized Python application (e.g., a REST API) and you want to deploy it to a cluster of servers. Docker Swarm allows you to easily deploy and manage the application across multiple nodes, providing scalability and high availability. This Docker Compose file simplifies the deployment process by defining all the necessary configuration in a single file.
Best Practices
:latest
and use specific version tags for your Docker images.
When to use Docker Swarm
Docker Swarm is a good choice for:
Memory footprint
The memory footprint of Docker Swarm itself is minimal. The memory usage of the services deployed with Docker Compose depends on the resource limits specified in the Compose file and the actual memory consumption of the Python application within the container. Setting reasonable resource limits is key for optimal performance.
Alternatives
Pros
Cons
FAQ
-
How do I deploy this Docker Compose file to Docker Swarm?
First, initialize a Docker Swarm cluster usingdocker swarm init
. Then, deploy the Compose file usingdocker stack deploy -c docker-compose.yml my-app
, wheremy-app
is the name of your application stack. -
How do I update the application image?
Update theimage
field in the Docker Compose file and then redeploy the stack usingdocker stack deploy -c docker-compose.yml my-app
. Docker Swarm will automatically perform a rolling update to replace the old containers with new ones using the updated image.