Docker Compose is a tool for defining and running multi-container Docker applications. It allows developers to define the services that make up their application and how they interact with each other, all in a single file. In this article, we’ll cover everything you need to know to get started with creating a Docker Compose file, including examples of how to define services and configure networking.
Some Prerequisites:
Before we dive into creating a Docker Compose file, let’s make sure you have the necessary prerequisites:
- Have Docker installed
- Have a basic knowledge of Docker: Familiarize yourself with Docker and its concepts
How to Create a Docker Compose File
A Docker Compose file is a YAML file that defines the services, networks, and volumes for your Docker application.
The beginning of a Compose file always starts with the version
key. This key is used to specify the version of the Docker Compose syntax being used in the file. This is important because Docker Compose has gone through several major revisions, and each revision introduces new features or changes to the syntax.
Currently, the latest version of the Docker Compose syntax is version 3.9. When you specify the version tag as “3.9”, Docker Compose will use the latest syntax and recognize all of the latest features and syntax changes.
After defining the version of the Compose file it’s time to define the services that will be run in the containers.
Each service is defined as a block under the services
key. In this example, we will define two services: a web server, using nginx; and a database, using MySQL.
version: "3.9"
services:
web:
image: nginx:latest
ports:
- "8080:80"
db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: example
In the example above, we defined a service called web
and used the nginx
image with the latest
tag. The tag is a custom, human-readable identifier that typically denotes a specific version or variant of an image. Using latest
ensures you’re always using the most recent version of the specified image, but this may lead to issues if an update to the image breaks something in your configuration.
We also exposed port 80 in the container and mapped it to port 8080 on the host machine. This means that when we access http://localhost:8080
on our local machine, we will be accessing the web server running in the container.
We also defined a service called db
and used the mysql
image. We also set the environment variable MYSQL_ROOT_PASSWORD
to example
, which will be used to set the root password for the MySQL instance.
Defining Volumes:
Next, we need to define any volumes that we want to use in our containers. Volumes allow us to persist data between container restarts and also allow us to share data between containers.
We can define volumes as a block under the volumes
key. In this example, we will define a volume called db_data
that will be used to store the data for our MySQL instance.
volumes:
db_data:
Defining Networks:
Finally, we need to define any networks that we want to use in our containers. Networks allow us to isolate our containers and control the flow of network traffic between them.
We can define networks as a block under the networks
key. In this example, we will define a network called webnet
.
networks:
webnet:
Let’s put it all together!
Now that we have defined our services, volumes, and networks, we can put them all together in a single docker-compose.yml
file.
version: "3.9"
services:
web:
image: nginx:latest
ports:
- "8080:80"
networks:
- webnet
db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- db_data:/var/lib/mysql
networks:
- webnet
volumes:
db_data:
networks:
webnet:
In the example above, we defined two services: web
and db
. We also defined a volume called db_data
, which we mounted to our mysql
container and created a network called webnet
.
With this knowledge, you should be able to create your own Docker Compose files and start running your applications in containers!
Some additional resources: