Compose is a tool for defining and running multiple containers at once. For example if you want to create a wordpress website with a MySql database you can do this very easily with compose.
Previously we learned that we could use docker files to define containers. With compose you use compose-yaml files to define your multi container environment.
Both Dockerfiles and compose-yaml files work hand in hand to create your containerized environment.
Set up folder to start your compose session
First create a directory to put your compose project in. This directory will contain everything your project requires but nothing more.
For this example we be using just a yaml file but later we will also use Dockerfiles to build our images.
Create a directory called mywordpress using mkdir mywordpress then enter your directory using cd mywordpress.
Next create a yaml file called docker-compose.yaml using touch docker-compose.yaml
YAML YAML YAML
YAML is an acronym that stands for “YAML Ain’t Markup Language”. This is recursive wordplay is weird but we programmers are weird anyway :).
It is a human readable data serialization language where consitent indentation is required. You can use any number of spaces to indent your Yaml as long as it is consistent :).
The Docker compose file below uses four and two space indentation. This file is used for configuring docker-compose.
Edit your yaml file using vim docker-compose.yaml and press i for insert. Copy and paste the text below and press esc and :wq to save.
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
Now, run docker-compose up -d to build the project.
docker-compose up -d Creating network "mywordpress_default" with the default driver Creating volume "mywordpress_db_data" with default driver Pulling db (mysql:5.7)... 5.7: Pulling from library/mysql d121f8d1c412: Already exists f3cebc0b4691: Pull complete 1862755a0b37: Pull complete 489b44f3dbb4: Pull complete 690874f836db: Pull complete baa8be383ffb: Pull complete 55356608b4ac: Pull complete 277d8f888368: Pull complete 21f2da6feb67: Pull complete 2c98f818bcb9: Pull complete 031b0a770162: Pull complete Digest: sha256:14fd47ec8724954b63d1a236d2299b8da25c9bbb8eacc739bb88038d82da4919 Status: Downloaded newer image for mysql:5.7 Pulling wordpress (wordpress:latest)... latest: Pulling from library/wordpress d121f8d1c412: Already exists 58b3577b786a: Already exists 60538287851f: Already exists c53ff72fe225: Already exists 79b018c8773f: Already exists fbe3e00ac4b0: Already exists ff35226e1df8: Already exists ab3b1d46dd82: Already exists b29cdd230d9a: Already exists d466b05cf627: Already exists 771f930f6d23: Already exists b89a2786f2a3: Already exists c35594c34f69: Already exists 8e3c480bd8bf: Pull complete 2d3e26ca1157: Pull complete cc0d53b93bc3: Pull complete dbcd12305020: Pull complete 90356c70a472: Pull complete ceb2ac363e49: Pull complete 202d7e2f6c6c: Pull complete Digest: sha256:7b67b32c076e9463a804182e0356966829d5533184c5064e22a7b97686e2154b Status: Downloaded newer image for wordpress:latest Creating mywordpress_db_1 ... done Creating mywordpress_wordpress_1 ... done
Now navigate to http://localhost:8000


Log in and you are in your wordpress site pretty cool!

More to learn
There is so many things you can do with compose. The following is a list of some of the common commands. To discover more you can find the Docker compose file reference here.
docker-compose up // will run the yaml file docker-compose up -d // will detach the process docker-compose logs // will show the logs docker-compose ps // will list the running containers docker-compose down //will stop the servers docker-compose down --volumes //will stop the servers and delete the database and network it creates
Before we go use docker-compose down --volumes to shut everything down. Happy composing!
docker-compose down --volumes Stopping mywordpress_wordpress_1 ... done Stopping mywordpress_db_1 ... done Removing mywordpress_wordpress_1 ... done Removing mywordpress_db_1 ... done Removing network mywordpress_default Removing volume mywordpress_db_data