Docker Compose: A Mini Orchestration Tool for Local Development
Understanding Docker Compose and How it Works
Docker Compose is already included when you install Docker (or Docker Desktop). You can check if you have it installed by running:
docker compose -h
Docker Compose works by defining a multi-container application in a single file called docker-compose.yml. This file abstracts container management and allows you to define services, networks, and volumes in a declarative way. A service, in development environments, typically represents a container that runs a specific part of your application, such as a web server, database, or cache. To better understand how this tool works, let's create a simple WordPress application using Docker Compose.
Let's start by creating a new folder and a new file called docker-compose.yml:
# Create a new folder
mkdir $HOME/wordpress && cd $HOME/wordpress
# Create a new file called docker-compose.yml
cat << EOF > $HOME/wordpress/docker-compose.yml
services:
db:
image: mysql:9.6.0
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: mypassword
MYSQL_DATABASE: wordpress
MYSQL_USER: user
MYSQL_PASSWORD: mypassword
wordpress:
image: wordpress:6.9.0-apache
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: user
WORDPRESS_DB_PASSWORD: mypassword
volumes:
db_data:
EOF
Now, let's start the application:
# Make sure you are in the wordpress folder
cd $HOME/wordpress
# Start the application
docker compose up
You should start seeing the logs of the containers:
- db
- wordpress
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:
