Run Docker container

Technology Docker is gaining popularity among developers and DevOps professionals in our days, because it allows you easily to configure various environment once, and then play them on, it would seem, is not compatible hardware. But we’ll start our series of articles on Docker with the basics.

In today’s article we will talk about creating containers and, respectively, on their launch. In a Docker container is an environment for the execution of any one process. It is an isolated environment that has everything required to perform the desired process, and there is nothing superfluous. Create a Docker container running at the time of its launch, and these processes are run using the command docker run. Let’s first examine the syntax and options.

  • The syntax and options of the docker run
  • Creating a Docker container
    • 1. A normal container
    • 2. The connection to the container
    • 3. Environment variables
    • 4. Mount folders and repositories
    • 5. Container ports
    • 6. Linking containers
    • 7. Network for containers
  • Insights

The syntax and options of the docker run

The syntax of the command docker run similar to the syntax of other Linux commands and looks like this:

$ docker run options, the image of the team

Utility, it is necessary to transmit the image, based on which container will be created. The image can be a local or point to the image you want to upload from the network. We will consider this in the examples below. Options allow you to configure the container and its settings in more detail. The command allows you to override the program that runs when starting the container. For example, running /bin/bash, you can connect to the container.

Consider the main options of the tools we will use. Options very much, so I can’t list them all:

  • -d – runs the container in the background;
  • -t – attaches to the container pseudo-TTY console;
  • -i – displays to the terminal the STDIN stream of the container;
  • –name – the name of the container, which can then be refer to it;
  • –dns – sets the DNS servers for the container;
  • –network – the network type for the container, can take the following values: bridge (default), none, host. You can also pass the ID of the Docker network to which it should connect;
  • –add-host – adds an entry in /etc/hosts;
  • –restart – indicates when it is necessary to restart the container. Possible values: no, on-failure, always, unless-stopped;
  • –rm removes the container after completion of its work;
  • -m, –memory – amount of RAM available to the Docker container;
  • –memory-swap – memory swap space available in the container;
  • –cpus – number of CPU cores available to the container;
  • –shm-size – size of the file /dev/shm;
  • –device – allows you to mount the device in the folder /dev in the container;
  • –entrypoint – enables you to override the script that runs when the container is started before running the main command.
  • –expose – allows you to forward a few ports from the container to the host system;
  • -P – all ports are forwarding the container to the host system;
  • -p – migrates all container ports to the host system without changing the port number;
  • –link – allows you to set up the Docker containers;
  • -e – adds an environment variable in the container;
  • -v, –volume – allows you to mount a host folder into the container;
  • -w – changes the working directory of the container.

These are the basic options that we will use in this article, and now let’s look at some examples of how to create a Docker container in Linux.

Creating a Docker container

1. A normal container

To create and start the container with the parameters set in the default image, simply run the command without any parameters. Let’s take a container hello-world, which as designed to do it:

docker run hello-world

After starting the Docker container will display a message with a greeting, and the process in the container is complete.

To search ready-made images for containers you can use the web site DockerHub. Here there are images for most distros, and system services, such as Nginx, Apache, PHP-FPM, PhpMyAdmin, and many others.

At the top displays the name of the container and slightly below – available version. When the container is created, the version is written after the colon. For example, let’s create and start a container with Ubuntu 18.04. To the container was easy to access then, ask him the name with the option –name:

docker run --name ubuntu Ubuntu1804:18.04

2. The connection to the container

Image Ubuntu 18.04, based on which we created the container, above, contains the team that would constantly run, so if you try to connect to it using the command docker exec, you’ll get an error: You cannot attach to a stopped container, start it first:

docker attach Ubuntu1804

This is because the environment can’t work without the underlying process for which you created the container. While running the process will work and the container. Once the process is completed, the container is completed, and we created earlier as the container is completely useless. You will not be able to join him because he will fall immediately after the start, and the fallen container will be connected cannot. You can delete it:

docker rm Ubuntu1804

And we need to create a container with the command that will be executed. To do this, simply pass the command after the image name, e.g. /bin/bash. To the container was available interactively, create it with the options -i and -t:

docker run -it --name Ubuntu1804 ubuntu:18.04 /bin/bash

You are now in a container and can perform actions directly in a chrooted environment.

3. Environment variables

Very often to change the settings container used environment variables. You set some environment variable and then its value is used by your program in the container to perform various actions. To set environment variables, use the option -e. Run the Docker container:

docker run -it-e "FOO=bar" --name Ubuntu1804_2 ubuntu:18.04 /bin/bash

echo $FOO

4. Mount folders and repositories

When you need to get the container to work with the source code of the program or with frequently changed data, I recommend to make a special setting – it will be uncomfortable every time to copy the information into the container. Much easier to mount a host folder into the container, and all changes to source are immediately available in the container. It is very convenient. To mount a folder, you can pass it the-v option. For example, Trimontium folder ~/test_docker in container:

docker run -it-v "~/test_docker:/mnt" --name Ubuntu1804_3 ubuntu:18.04 /bin/bash

Store allow to mount in the container virtual disk that is not removed when you delete the container. This repository will be automatically created, if transfer is not an existing folder and the name for the new storage:

docker run -it-v docker_volume:/mnt --rm --name Ubuntu1804_4 ubuntu:18.04 /bin/bash

I recall that the option –rm removes the container as soon as it completes its work. View the list of available repositories with the command:

docker volume list

5. Container ports

If you need to access any service of the container at the port, such as a web interface, this port need to traverse the host system. To do this, use the option -p. Let’s install Nginx and probrosit port in the host system:

docker run --name Nginx -p 8080:80-d nginx

6. Linking containers

Linking containers allows you to configure the interaction between them. A linked container will be available on the network by name. The corresponding line is automatically added to the /etc/hosts file of the container. To link, use the option –link. To consolidate the knowledge obtained above, let’s create a container with a MySQL database, and then bind it with PhpMyAdmin.

First create the MySQL container with a permanent repository in /var/lib/mysql. In the environment variables it is necessary to transmit the root password. What environment variables, expects container – this information can usually be found on the page of the container on DockerHub. Use option -dto start the container in the background:

docker run-v mysql_volume:/var/lib/mysql --name MySQL -e MYSQL_ROOT_PASSWORD=passwd -d mysql:8.0

Then create a container with PhpMyAdmin to access the database and associate with a container under the name of the MySQL db:

docker run --name PhpMyAdmin -d --link MySQL:db-p 8080:80 phpmyadmin/phpmyadmin

Also here we probressive port 80 to the host system to access the web interface. Now you can check in the browser. Everything works.

7. Network for containers

Containers can not be linked. If you need to combine three or more containers among themselves, it is much more convenient to link them in one network than to create multiple connections for each of these containers. All the objects in one network will have access to each other by their first name. You must first create the network:

network docker create-d bridge docker_network

View the list of created networks with the command:

docker network list

Now it is possible to use it. Combine with our network MySQL and PhpMyAdmin server. For this we need to stop and delete:

docker stop MySQL

docker stop PhpMyAdmin

docker rm MySQL

docker rm PhpMyadmin

Then create:

docker run-v mysql_volume:/var/lib/mysql --network docker_network --name MySQL -e MYSQL_ROOT_PASSWORD=passwd -d mysql:8.0

And PhpMyAdmin now you have to pass the host where the database is located in the environment variable PMA_HOST:

docker run --name PhpMyAdmin -d --network docker_network -e PMA_HOST=MySQL -p 8080:80 phpmyadmin/phpmyadmin


In this article, we discussed how to start the Docker container and configure its settings. As you can see, Docker can be very useful for fast expanding working environment. But rarely used for this purpose itself the command docker run. Usually use a tool called docker-compose. I wonder if you read about this tool? Write in the comments!


(Visited 10 times, 1 visits today)