Using Docker Compose for Multiple Containers

Configure Docker Compose with the Django application in this lesson.

Docker Compose is a tool developed and created by the Docker team to help define configurations for multi-container applications. Using Docker Compose, we just need to create a YAML file to define the services and the command to start each service. It also supports configurations—such as container name, environment setting, volume, and a lot more. Once the YAML file is written, we just need a command to build the images and spin all the services.

Dockerfile vs. Docker Compose

The key difference between a Dockerfile and Docker Compose: is that a Dockerfile describes how to build the image and run the container, while Docker Compose is used to run Docker containers. At the end of the day, Docker Compose still uses Docker beneath the surface, and we will—most of the time—need at least a Dockerfile. Let’s integrate Docker Compose into our workflow.

Writing the docker-compose.yaml file

Before writing the YAML file, we have to make some changes to the Dockerfile. Because we’ll be launching the Django server from the docker-compose file, we can remove the lines where we expose the port, run the migrations, and start the server. Inside the Dockerfile, remove the following lines of code:

Get hands-on with 1400+ tech skills courses.