Create a Docker Compose application
How do I create an application with multiple Docker containers using Docker Compose?
A basic use case for Docker Compose is to pair an application container with a database container. With Docker Compose, you can define how each container runs using a docker-compose.yml
file. This article will teach you how to use Docker Compose to run different containers at the same time.
What do I need to do first?
Install Docker and Docker Compose.
Read the following articles to learn how to do this.
What are we going to run?
Drupal is a popular content management system that has an application container that requires a separate database container. In this article, you will learn how to run both containers with a single Docker Compose file.
Download the example file
You can use curl
to download the example Docker Compose file. There are two service blocks defined — one for postgres
and one for drupal
.
curl -lo docker-compose.yml "https://gist.githubusercontent.com/nonanom/ec00eebd21841672e1d00e9a4aac0dc9/raw"
Create and start the containers
Run the Docker Compose up
command to create and start the containers defined by your Docker Compose file.
If the Docker Compose file is located in a different directory, you can specify the file location using the Docker Compose -f
option. Read about the other options in Docker Compose documentation.
docker compose up
Complete the Drupal installation
Now that both the Drupal application container and the Postgres database container are started, navigate to https://localhost:8080 in your browser to access the Drupal web interface.
Use the following options we defined in our Docker Compose file. We’ll use postgres
for the hostname of our database since that’s the name we used to define the service in our Docker Compose file.
Database type: PostgreSQL
Database name: drupaldb
Database user: drupaluser
Database password: drupalpassword
Advanced Options > Host: postgres
Temporarily stop the containers
You can stop the containers, and start them again.
Because we did not use docker compose up
with the -d
option, we must interrupt the running process.
CTRL-C
You can restart everything using the start
command.
docker-compose start
Stop and remove the containers
But what if you don’t want to stop and resume?
What if you want to stop and remove all traces of the running containers?
In order to stop and remove the containers, use the down
command.
docker-compose down
“That’s all Folks!”
As your application evolves, you may opt to use Kubernetes to run both containers in a pod using a Kubernetes manifest. The Docker Compose file is very similar and uses similar concepts.