Using Docker for dummies

In a previous article we discussed how to launch a Docker container from the image. The basic idea of Docker is that for each separate process should be created a separate Docker container with the environment, need this process. But for complex apps here the problem lies.

For example, for a web application already need a database, web server, and perhaps even the PHP interpreter. That’s three container, configure, and launch them by hand is not convenient, so was invented a utility docker compose, which allows you to manage groups of containers, create, configure, and remove one command. In this article we will understand how to use docker for dummies. A detailed look at docker-compose as well as actual use of the utility.

  • Using Docker for dummies
    • 1. Install docker-compose
    • 2. The creation of the project
    • 3. Adding containers
    • 4. Starting containers
    • 5. Container ports
    • 6. Mounting folders
    • 7. Storage configuration
    • 8. Network setup
    • 9. Modification container
    • 10. The connection to the container
  • Insights

Using Docker for dummies

Since this tutorial about Docker for beginners, I recommend you read the article about the running container, you will understand some fundamentals of Docker, who can you useful. They told how to do everything manually, but here we will talk how to automate this process.

1. Install docker-compose

If the program is not already installed, install it. To do this, simply download the executable file from the official website of the developers:

sudo curl -L "$(uname-s)$(uname -m)" -o /usr/local/bin/docker-compose

And give it execution rights:

sudo chmod +x /usr/local/bin/docker-compose

After that you will be able to see her version:

docker-compose --version

2. The creation of the project

If you’ve already seen projects that use docker, you’ve probably noticed that the project folder is a file called docker-compose.yaml. In this file configures the containers that we need to create for your project, then they will be created automatically using docker-compose. The file uses the YAML syntax and must contain the following data:

version: 'version'

Version indicates the version of the file syntax in the different versions available for different keywords, this is done for backward compatibility. We will use version 3.5. Next you need to list storage (volumes), networking (networks) and the containers.

The syntax of YAML is similar to JSON, here too there are a pair key: value separated by a colon, then the value can be null, can contain other keys, and it can be an array of values, then each array element begins with a dash “-“. But unlike JSON, it is very important indentation to show the nesting of values, so do not waste them.

Let’s create a folder losst-docker will create a file docker-compose.yaml:

mkdir losst-docker

vi losst-docker/docker-compose.yaml

version: '3.5'

3. Adding containers

Examine the contents of a very simple settings container:

 image: image:version

Here we have to specify the name of the future container, as well as the image on the basis of which it is created. After the colon, you can specify the version of the container. Version can be found on Dockerhub they’re marked as tags. If the version is not specified is used latest, latest.

For example, let’s add a container for the web server Nginx:

 image: nginx

Already having this data in a configuration file, you can start the container.

4. Starting containers

When configuring docker is completed, it is necessary to run the resulting containers. To run a group of containers configured in the docker-compose.yaml need to go to the directory where this configuration file to execute the command docker-compose up. For example:

cd losst-docker

docker-compose up

After that, the containers are launched, their output streams are merged into one and you will be displays information to the terminal. To stop the container just press Ctrl+C. If you want to run containers in the background use the-d option:

docker-compose up-d

To stop the containers running in the background with the command stop:

docker-compose stop

Command down does not just stop all running containers, but removes them:

docker-compose down

Stop until this container, we will continue customizing it.

5. Container ports

The container is currently running, but until we sense from it a little. By using docker we can map port 80 of the container to the host operating system to access it. To do this, use the Directive ports. The syntax is:

– vneshnimi:internal port

For example, probrosit port 80 as 8094:

 image: nginx 
 - '8094:80'

Now you can restart containers and see in the browser the page stating that Nginx is running at http://localhost:8094:

But it’s still not interesting because we cannot place there your files. Now it is correct.

6. Mounting folders

To mount a storage folder or an external host, use volumes. The syntax is very similar to working with the ports:

– /path/to/external/directory:/path/to/internal/folder

For example, let’s create in the current folder of the project file index.html and mount this folder instead of /usr/share/nginx/html/ containers:

vi index.html

<html>It's works!</html>

vi docker-compose.yaml

 image: nginx 
 - '8094:80' 
 - ./:/usr/share/nginx/html/

After restarting the container, you will see in the browser is your page. This is a very powerful tool, with which you can push in the container, all starting from source and ending with the configuration files. Very convenient.

7. Storage configuration

We can mount to the container not only the appearance of the folder but the repository created in docker. For this you first need to add your repository to the main section of the volumes. For example losst-vl:


Most web applications need a database, for example MySQL. We will add another container for the database and add it to our repository. The store is added as an external folder, but instead of folders, you specify the name of the repository:

 image: mysql 
 - losst-vl:/var/lib/mysql 
 - MYSQL_DATABASE=database 
 - MYSQL_USER=user 
 - MYSQL_PASSWORD=password

Here we have added a paragraph environment in which he asked the environment variables for the container. They are necessary in order to specify the database name and the root password that will be used by default. As you can see, environment variables, nothing complicated.

8. Network setup

Containers should interact with each other. We already have Nginx and MySQL, they don’t need to turn to each other, but as soon as we have a container for PhpMyAdmin, which you have to apply to MariaDB the situation will change. For communication between the containers using a virtual network, they are added in a similar way to the vault. First, add the network to the global networks section:


Then for each container that will have access to this network, it is necessary to add a network to partition networks.

Further, the containers will be available on the network by their name. For example, add PhpMyAdmin and give him access to the database:

 image: phpmyadmin/phpmyadmin:latest 
 - "8095:80" 
 - PMA_HOST=docker-mysql 
 - losst-network

Here in the variable PMA_HOST we refer to the host docker-mariadb, which is due to the total network of these containers available. Similarly, in the container docker-mariadb, our container with PhpMyAdmin is available as docker-phpmyadmin. You can open the address http://localhost:8095 and login to verify that the database is running:

9. Modification container

It is a more difficult thing, but you will understand with Docker in practice. In most cases you can do without modifying the container, I need to copy specific configuration files or install additional software for such cases, docker compose lets you create containers on the basis of already existing images. For this we need to create the Dockerfile file on the basis of which to create the container. Let’s add a container php-fpm and install a few php extensions using the Dockerfile.

First, create a folder for files container:

mkdir php-fpm

vi php-fpm/Dockerfile

FROM php:7.2.26-fpm-stretch
RUN docker-php-ext-install pdo_mysql pdo pcntl

Here are basic guidelines that you can use in this file:

  • FROM the image on the basis of which will be created the way;
  • RUN – execute a command in the environment image;
  • COPY – copy the file to the image;
  • WORKDIR – set the working folder for the image;
  • ENV – set environment variable of the image;
  • CMD – set, the main process of the image;

Now we need to add a new section in our docker-compose.yaml. Here is the image we use a Directive to build, which you need to pass the path to the folder containing the configuration images:

 context: ./php-fpm 
 - ./:/var/www/ 
 - losst-network

Further, we’ve added php-fpm is necessary to mount the correct config for Nginx to support php-fpm. Create a folder with nginx and put this config file:

mkdir nginx

vi nginx/site.conf

server {
listen 80;
server_name _ !default;
root /var/www/;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
error_page 404 /index.php;
location ~ .php$ {
location docker-php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;

There is not much to correct the nginx configuration. To mount this configuration file and correct folder for web documents by default:

 image: nginx 
 - '8094:80' 
 - ./:/var/www/html/ 
 - losst-network

We have to create the file index.php and you can test:

vi index.php

<?php phpinfo(); ?>

Now you can run the containers:

docker-compose up

Unlike previous times, now before the launch will be built a new container based on the Dockerfile file. Such containers are only collected for the first time if they do not exist. To rebuild, use the option –build:

docker-compose up --build

Now you have a complete web app, which is available on your computer, you may use a database, PHP and much more. You can add any missing you containers.

10. The connection to the container

Docker-compose you can connect to any container from the group. To do this, simply use the exec command. For example, run the project in the background:

docker-compose up-d

And use docker-compose exec. Connect to the container with Nginx:

docker-compose exec docker-nginx /bin/bash

You will see the Bash shell of this container. To install something manually is not recommended, as everything will be erased after removal of the container, but to test that any kind of opportunity will be very useful.


In this article, we discussed using docker for dummies. The topic is quite extensive and complex, but very interesting. Expanding thus, the project on one machine, you can deploy it wherever you need just by copying back the configuration. Therefore, this technology has gained so much popularity among developers and DevOps.


(Visited 6 times, 1 visits today)