You’re working on a new website project with WordPress. To simplify management of the infrastructure, you want to deploy all of your components in Docker containers. To do this, we’re going to need two containers:
A MySQL container
And a WordPress container
Install Docker Compose
If you’ve used Docker for Mac or Docker for Windows, you already have the latest version of Docker Compose installed on your system.
On a Linux computer, that won’t be the case, so you’ll have to download and then install it with this command line (yes, it’s a bit long!):
sudo curl -L https://github.com/docker/compose/releases/download/v2.12.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose &&chmod +x /usr/local/bin/docker-compose
You can check which version of Docker Compose is installed on your computer with:
> docker-compose --version
docker-compose version v2.12.2, build 1110ad01
Discover the Docker Compose CLI
To use the Docker Compose CLI (Command-Line Interface), we’ll need a docker-compose.yml
file, which we’ll create in the next chapter. But before we do, let’s start by taking a look at the CLI that lets us use docker-compose.yml
.
The Docker Compose and Docker CLIs are pretty similar. For instance, if you want to pull all images described in your docker-compose.yml
file and download them from Docker Hub, you’ll run docker-compose pull
. For Docker, the command would be docker pull
.
However, we’re still going to take a look over the key commands that you might need to use and know when it comes to Docker Compose.
Start a Docker Compose Stack
If you want to run the creation of all of your containers, you need to run the docker-compose up
command (remember, you use docker run
to run a single container). You can add the -d
argument to run the containers as a background task.
See the Status of a Docker Compose Stack
After starting a Docker Compose stack, you’re going to have to check whether all of the containers are operating as they should and are ready to provide a service.
To do this, use the docker-compose ps
command, which will return the following:
Name Command State Ports
026848e7b4da_app flask run Exit 255 0.0.0.0:5000->5000/tcp
See the Logs for a Docker Compose Stack
Your Docker Compose stack is now operational and all services are responding well, but you may at some point need to see your container logs. To do this, you’ll need the following command: docker-compose logs -f --tail 5
.
This will give you a continuous display of the logs for the different containers, limiting the display to just the first five lines.
This way, if our containers have been operating for a long time, we won’t have any waiting time or have to look at lots of logs that we’re not interested in.
Stop a Docker Compose Stack
If you want to stop a Docker Compose stack, you’ll need to use the docker-compose stop
command. However, this will not delete the different resources created by your stack.
This means that if you run docker-compose up -d
again, your whole stack will immediately return to being operational.
If you want to delete a whole Docker Compose stack, you’ll need to use the docker-compose down
command, which will destroy all of the resources created.
Check a Docker Compose Stack
When writing a docker-compose file, there’s always the possibility that errors may arise. To avoid them as much as possible, you’ll need to use the docker-compose config
command to check your file’s syntax and be sure it’ll work properly.
If we introduce an error into our stack, by replacing “image” with “images,” for instance, we’ll get the following result:
➜ docker-compose config
ERROR: The Compose file './docker-compose.yml' is invalid because:
Unsupported config option for services.db: 'images'
Let’s Recap!
You now know the key commands for using a Docker Compose stack. Here’s a recap of the most important commands:
docker-compose up -d
for starting all of your containers in the backgrounddocker-compose ps
for seeing the status of your whole stackdocker-compose logs -f --tail 5
for seeing the logs for your stackdocker-compose stop
for stopping all of a stack’s servicesdocker-compose down
for destroying all of a stack’s resourcesdocker-compose config
for checking the syntax of yourdocker-compose.yml
file
Without further ado, let’s get started on a more concrete example, creating our very first Docker Compose stack!