Docker Compose: A Mini Orchestration Tool for Local Development
Understanding Docker Compose Syntax
If we want to run a WordPress site using Docker, we need to run two containers: one for the database and one for the WordPress application. We can do this using the following commands:
# Create a network for the containers
docker network create wordpress
# Create a volume for the database
docker volume create db_data
# Create a container for the database
docker run -d \
--name db \
-v db_data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=mypassword \
-e MYSQL_DATABASE=wordpress \
-e MYSQL_USER=user \
-e MYSQL_PASSWORD=mypassword \
--restart always \
--network wordpress \
mysql:9.6.0
# Create a container for the Wordpress application
docker run -d \
--name wordpress \
-p 8000:80 \
-e WORDPRESS_DB_HOST=db:3306 \
-e WORDPRESS_DB_USER=user \
-e WORDPRESS_DB_PASSWORD=mypassword \
--restart always \
--network wordpress \
wordpress:6.9.0-apache
As you can see, we need to run four commands to create the two containers. The idea of Docker Compose is to simplify this process by allowing us to define the configuration of the containers in a YAML file.
What a user should do here is "translate" the commands above into a YAML file.
Another advantage of using Docker Compose is that we are "describing" the desired state of the application, not the steps to achieve it. This is called declarative configuration as opposed to imperative configuration (the commands above are imperative). If you run an imperative command and it fails halfway through, you need to figure out what went wrong and fix it. With declarative configuration, you just need to fix the configuration file and run docker compose up again. Docker Compose will take care of the rest. Also, if you run an imperative command twice, you may end up with duplicate resources or an error saying that the resource already exists. With declarative configuration, you can run docker compose up multiple times without any issues. Docker Compose will ensure that the desired state is achieved: if the resources already exist, it will not create them again, and if they don't exist, it will create them. This is called idempotency and it's a key feature of declarative configuration.
Let's start with the first command:
docker network create wordpress
Here, we are creating a network called wordpress. In Docker Compose, there is no need to define a network. We can let Docker Compose create a default network for us. All services in a single Compose file will be connected to the same network that will be created automatically.
Now, let's move to the second command:
docker volume create db_data
Here, we are creating a volume called db_data. In Docker Compose, to define a volume, we need to use volumes:
volumes:
db_data:
Under the name of the volume, we can add other options such as the driver, the driver options, etc. In our case, we are using the default driver, so we don't need to specify it.
Now, let's move to the third and fourth commands. Both create containers, so we need to use services. Here are the equivalent definitions in Docker Compose:
services:Painless Docker - 2nd Edition
A Comprehensive Guide to Mastering Docker and its EcosystemEnroll now to unlock all content and receive all future updates for free.
Hurry! This limited time offer ends in:
To redeem this offer, copy the coupon code below and apply it at checkout:
