Docker Compose,
the basics of YAML

Docker Compose in a few simple steps

Docker <span class='purple'>Compose</span>, <br/>the basics of <span class='purple'>YAML</span>

Introduction to Docker Compose

Docker Compose is a tool that allows you to define and run multi-container applications using a YAML file. In short, with one file we are able to run the entire necessary environment to operate our application. In today's example, we will go through the process of running the most popular CMS in the world, Wordpres.

You are probably asking yourself why we have to use Docker Compose and not just the basic Docker command?

The answer to this question is simpler than you think. In order to run any Wordpress application, you must have both the Wordpress CMS Panel and the MySql database. In the case of the basic Docker command, it would be necessary to run the CMS panel and the MySql database separately. Thanks to Docker Compose, we will be able to quickly run the entire Wordpress application, regardless of the host system, with one command.

1. What is Docker Compose?

Docker Compose is a tool that allows you to run applications consisting of multiple Docker containers. It allows you to define services, networks and volumes in a YAML file, which simplifies the management and execution of complex applications. Thanks to Docker Compose, we can easily define, run and manage test and production environments.

Docker Compose is a basic tool for both production and testing, but it is not as advanced as e.g. Docker Swarm or Kubernetes . Nevertheless, its knowledge is necessary in order to further comprehensively explore container environments.

2. What is YAML?

YAML is a text file format that is easy to write and read. It is often used to configure applications due to its simplicity. YAML files consist of keys and values ​​that are organized in a hierarchical structure. You can view them as JSON files, only on steroids.

Depending on the interpretation program, the structure of the YAML file may vary, but the general principles are always the same. The entire difference will only be based on the use of different commands.

What does it mean that YAML is Indent Sensitive?

YAML, like e.g. Python, is very sensitive to all types of white characters (e.g. space, tab). Incorrect use of spaces and tabs will prevent the file from being read correctly. Pay attention to the indentation structure in the example below.

Sample YAML file - Docker Compose

version: '3.8'services: app:  image: my_app_image  ports:   - "8080:80"  environment:   - APP_ENV=production

Docker Compose - Example

Before you start...

This tutorial will present you with a basic example of configuring the docker-compose.yaml file, in which we will configure Wordpress with the MySql database. Remember that you must have Docker Engine installed and running.

You can run containers on your local machine, virtual machine or VPS. If you choose the last option (i.e. VPS), you will need to use SSH tunneling to access the container via a web browser. Tunneling in this case will be necessary due to the fact that we will not connect the domain to our container at this stage.

How to perform SSH tunneling?

Tunneling via SSH allows us to redirect traffic from our local port to the target port on our VPS server. To do this, execute the following command:

Syntax:
ssh -N - p 22 {user}@{vps_ip} -L {local_port}:{vps_internal_ip}:{vps_internal_port}

For example:
ssh -N -p 22 noob@123.123.12.12 -L 8080:127.0.0.1:8080

The above command will try to connect via SSH to user 'noob' at '123.123.12.12' and then redirect traffic from your local port 8080 to the internal address of your VPS 127.0.0.1:8080.

This way you will be able to connect to your container from your local web browser.
If the above information is too complicated for you at the moment, run Docker Compose on your local computer.

1.How to run Wordpress from MySQL using Docker Compose?

First, go to the target directory where you want to create the file docker-compose.yaml.

Create file in target directory

touch docker-compose.yaml

Complete the file as follows

version: '3.8'services: wordpress:  image: wordpress:latest  ports:   - "8080:80"  environment:   WORDPRESS_DB_HOST: db   WORDPRESS_DB_USER: wordpress   WORDPRESS_DB_PASSWORD: wordpress   WORDPRESS_DB_NAME: wordpress  volumes:   - wordpress_data:/var/www/html db:  image: mysql:5.7  environment:   MYSQL_ROOT_PASSWORD: rootpassword   MYSQL_DATABASE: wordpress   MYSQL_USER: wordpress   MYSQL_PASSWORD: wordpress  volumes:   - db_data:/var/lib/mysqlvolumes: wordpress_data: db_data:

2. Docker-compose.yaml file for Wordpress + MySQL explained

When you were copying the content of the above file, you were probably holding your head and asking yourself "what is this all about?" At first glance, the structure of the above file docker-compose.yaml may seem complicated and incomprehensible, but in a moment we will explain the code line by line.

Section "version"
Specifies the version of the syntax that Docker Compose will use. In this case we are using version '3.8'. Always use the latest version unless your project requires an older version. At this point, it is also possible to omit this information from the file, which will automatically result in using the latest version of Docker Compose.

Section "services"
Defines two services (containers): "wordpress" and "db" (you can use any names, as long as they are unique)


  1. Wordpress container
    • "image" - We use the official latest version of the WordPress image
    • "ports" - Forward port 8080 of the host to port 80 of the container
    • "environment" - We define environment variables for the WordPress container
    • "volumes" - We create the "wordpress_data" volume to store WordPress data
  2. DB Container
    • "image" - We are using the official MySQL version 5.7 image
    • "environment" - We define environment variables for the container hosting the MySQL database
    • "volumes" - Create the "db_data" volume to store MySQL data

Section "volumes"
Defines the volumes used by both of our containers/services.

Why do we use Volumes?

If you carefully read the previous lesson in the "DevOps Course for Juniors" course series, you must have remembered the fact that Docker containers are by nature like the HTTP protocol, i.e. "stateless".

What does this mean in practice?

This means that a given container will store the information until it is deleted or upgraded to a higher version of the image. When using Wordpress technology with a MySQL database, this is an unacceptable situation!

By creating Volumes, we allow our Docker containers to save data on the host system, and not in RAM. Thanks to this procedure, your data will not be lost after redeploying the image.

If you forgot to do this, any configurations you made in Wordpress, such as page creation, updates, etc., would be unstable.

Do not include sensitive data in Compose files!

In the above example, sensitive information such as MySQL login data was saved directly in the docker-compose.yaml file to clarify the example.

This approach is UNACCEPTABLE, in a production environment use e.g. Docker Secrets .

3. How to start/stop containers with Docker Compose?

In order to start/stop containers, it is necessary to go to the folder in the terminal where our docker-compose.yaml file is stored.

To run containers from the docker-compose.yaml file, run the command (deatached mode)

docker compose up -d

Make sure your containers are working properly

dockerps

If everything went well, you should be able to launch the Wordpress admin panel from your web browser. To do this, go to http://localhost:8080.

To stop containers from the docker-compose.yaml file, run the command (deatached mode)

docker compose down

Docker Compose - Example

Good job!

Congratulations, you've successfully launched your first full-fledged Docker container cluster. As you can see, in the example above, it is not as difficult as it may seem. At this point, all you need to do is transfer the above file docker-compose.yaml to another server or machine to be able to enjoy a new instance of the Wordpress application with a configured MySQL database. There is no need to download Wordpress files yourself or configure the MySQL database yourself. Everything is done on the fly by Docker Compose.

Even though you may feel proud at this point, you cannot rest on your laurels because you managed to run your first system on your own. . Wordpress is the most popular CMS in the world, which has both advantages and disadvantages. Exposing the container to the world in the state it is in now may turn out to be an invitation to a hacker to our server, due to the countless number of malicious crawlers looking for unsecured Wordpress instances. In the following lessons, I will take a closer look at this problem and before we put our application out into the world, we will use a basic WAF (Web Application Firewall), which is Modsecurity.