Deploying Python Apps with Docker Compose
After playing with Docker and building our own image using Docker build, you understand that Docker is all about creating, deploying, and running applications by using containerization, right? Docker Compose builds on this concept and simplifies the process of managing multiple Docker containers at once.
Introduction to Docker Compose
Docker Compose is a tool for defining and managing multi-container Docker applications. But what does multi-container means? Do we even need multi-container?
Docker Compose is a tool that allows you to orchestrate multiple Docker containers at the same time. It lets you manage the lifecycle of multiple containers as a single unit, which is incredibly useful when you have an application that consists of multiple services running in separate containers.
For example, consider a Python web application that interacts with an Elasticsearch database. In this scenario, you may have two Docker containers: one for your Python application and one for your Elasticsearch database. Without Docker Compose, you would have to individually manage these containers, which can be tedious. With Docker Compose, you can start, stop, and scale both these containers together by writing a simple YAML configuration file named docker-compose.yml
.
Understanding docker-compose.yml
The YAML file (by default, docker-compose.yml
) configures the application’s services and creates and starts all the services from your configuration.
Here’s a breakdown of a python app example:
version: "3"
services:
web:
build: .
command: python myapp.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
version
: Specifies the Docker Compose file format version.services
: This section is where you define your services (containers). In this example, you have one service calledweb
.build: .
: This tells Docker to build an image using the Dockerfile in the current directory.command
: This is the command that gets executed inside of your container. In this case, it runs your Python app on port 8000.volumes
: The.
refers to the current directory on your host machine, and/code
is the path inside the container. This line is essentially syncing your host machine’s current directory with the/code
directory inside the container.ports
: This section is for port mapping. It maps port 8000 of the container to port 8000 of your host machine.
What if we want to add another container, for example Elasticsearch?
In your docker-compose.yml
, you would add a new service for Elasticsearch. Here’s an example of what that might look like:
version: "3"
services:
web:
build: .
command: python myapp.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
depends_on:
- elasticsearch
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.6.2
environment:
- discovery.type=single-node
ports:
- 9200:9200
In this example, we’ve added the elasticsearch
service to our Docker Compose file. It uses an official Elasticsearch image (docker.elastic.co/elasticsearch/elasticsearch:7.6.2
). We’ve also specified an environment variable (discovery.type=single-node
) to run Elasticsearch in single-node mode.
The depends_on
option under the web
service ensures that the web
service only starts once the elasticsearch
service is up and running.
The ports
section under the elasticsearch
service maps the port 9200 of the container (the default port for Elasticsearch) to port 9200 on your host machine, allowing you to interact with Elasticsearch via localhost:9200.
Remember to replace the image version (7.6.2
in this case) with the version of Elasticsearch you want to use.
Scaling and Managing with Docker Compose
Docker Compose not only takes care of starting and stopping multi-container applications, but it also provides the ability to scale services. Using the --scale
service command, you can easily scale up or down the number of containers running a particular service.
For instance, if you want to run three instances of the “web” service, you would use:
docker-compose up --scale web=3
This command tells Docker Compose to run three containers for the “web” service.
In a nutshell, Docker Compose enhances the Docker experience by managing the complexity of dealing with multi-container applications. It allows you to define, manage, and scale containers easily using just a single YAML configuration file.
Managing Your Docker Containers
With Docker Compose, you can manage your Docker containers effectively. For example, to start your application, you would navigate to the directory containing your docker-compose.yml
file and use the command docker-compose up
. To stop your application, you would use docker-compose down
.
With the understanding of Docker Compose, you can easily manage multi-container Python applications. Docker Compose simplifies the process of dealing with applications that consist of multiple containers, allowing you to define, build and manage them as a single entity.