Requirements
YAML is a recursive acronym that stands for YAML Ain’t Markup Language. It is used by a lot of different applications for both configuration and also for defining data in a human-readable structured data format.
Building a Docker Compose YAML files
Step 1 : Create a directory for the application, and within it, create docker-compose.yml to define the app.
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: {}
Step 2 : The docker-compose.yml file we used to launch our multi-container application is split into three separate sections.
version
version: '3.3'
The first section simply specifies which version of the Docker Compose definition language we are using; in our case, as we are running a recent version of Docker and Docker Compose, we are using version 3.3
services
The next section is where our containers are defined; this section is the services section. It takes the following format.
services:
--> container name:
----> container options
--> container name:
----> container options
- image : This tells Docker Compose which image to download and use.
- volumes : Mount host paths or named volumes, specified as sub-options to a service.
- depends_on : is used to help build some logic into the order your containers are launched in. For example, only launch container B when container A has successfully started
- ports : Expose ports.
- restart : no is the default restart policy, and it does not restart a container under any circumstance. When always is specified, the container always restarts. The on-failure policy restarts a container if the exit code indicates an on-failure error.
- environment : Add environment variables
volumes
The final section of our Docker Compose YAML file is where we declare our volumes:
volumes:
db_data: {}
Building images with Docker Compose
We can even build images from a Dockerfile during the compose, and then use it for the app. For example, to build the WordPress image, we can use the corresponding Dockerfile and place it within the application compose directory
Step 1 : Create a Dockerfile.
nano Dockerfile
FROM wordpress:latest
Step 2 : We then need to update the docker-compose.yml file so that we are referencing the Dockerfile from above. This will allow us to change the WordPress image, and make any custom changes that we might need that are not in the official image.
nano docker-compose.yml
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
build: .
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: {}