If your Docker application includes more than one container (for example, a webserver and database running in separate containers), building, running, and connecting the containers from separate Dockerfiles is tough and messy. DockerCompose solves this problem by allowing you to create a YAML file to define multi-container apps. You can configure as many containers as you want, how they should be built and connected, and where data should be stored. Once you have all the defination in the YAML, you can run a single command to build, run, and configure all of the containers.
$ curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)"
-o docker-compose
$ sudo install docker-compose /usr/local/bin
$ docker-compose --version
$ mkdir WordpressDemo && cd WordpressDemo
$ touch docker-compose.yml
version: '3.7'
volumes:
wp-data:
networks:
wp-backend:
services:
db:
image: mysql:5.7
volumes:
- wp-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: N0t53cur3d
MYSQL_DATABASE: wordpress
MYSQL_USER: wp-user
MYSQL_PASSWORD: wp-pass
ports:
- 8889:3306
networks:
- wp-backend
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- 8888:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wp-user
WORDPRESS_DB_PASSWORD: wp-pass
volumes:
- ./wordpress-files:/var/www/html
container_name: wordpress-site
networks:
- wp-backend
Some key items in the file are:
docker-compose will start the service in dependency order. So by saying depends_on, the db service will be started first. Only when this one has started the wordpress service will come up.
use the wordpress:latest for this demo or a specific image to build this service from which is the best practice.
Sets the container environment variables, telling the container who is the database host and the credentials. Here we are referencing our database service ‘db’.
We want to be able to access the wordpress tool. In the container it is running on port 80. We will map this port in the container to port 8888 on our host, so that we can access wordpress. You can define multiple ports e.g 443 like “- 443:443” in the “ports” section.
Here you indicate the name of a network you wish to set up. When you configure your services you can tell them to use this network. You should think of this as the way that all your services are connected and know each other. Docker-compose will create a network by default if you do not explicitly state it here. But you are not able to chose the name. This service should use the same network as the db mysql service, namely ‘wp-backend’.
Volumes are the most used way to persist data in Docker containers and services. Even when your service goes down, the data will still be there the next time you start your service. Docker will place this volume in the default directory that Docker uses for named volumes.
Now you have already played around with Docker Compose. Lets do a little test :)
$ docker-compose up -d
N.B -d helps you to run the application in the background. If you would like to run it in foreground then omit -d flag.
Please download the below code or clone it from github. This is a flask and MySQL based application. Please create a docker-compose and run the application.