Docker compose – установка и настройка

Шаг 1-й установка docker

Устанавливаем от суперпользователя

wget -qO- | sh

Шаг 2-й установка docker-compose

apt-get -y install python-pip

После установки необходимых пакетов мы установим сам docker комопозер

pip install docker-compose


Шаг 3-й запуск docker контейнеров через docker-compose

The public Docker registry, Docker Hub, includes a simple Hello World image. Now that we have Docker Compose installed, let’s test it with this really simple example.

First, create a directory for our YAML file:

mkdir hello-world
Then change into the directory:

cd hello-world
Now create the YAML file using your favorite text editor (we will use nano):

nano docker-compose.yml
Put the following contents into the file, save the file, and exit the text editor:

image: hello-world
The first line will be used as part of the container name. The second line specifies which image to use to create the container. The image will be downloaded from the official Docker Hub repository.

While still in the ~/hello-world directory, execute the following command to create the container:

docker-compose up
The output should start with the following:

Output of docker-compose up
Creating helloworld_my-test_1…
Attaching to helloworld_my-test_1
my-test_1 |
my-test_1 | Hello from Docker.
my-test_1 | This message shows that your installation appears to be working correctly.
my-test_1 |
The output then explains what Docker is doing:

The Docker client contacted the Docker daemon.
The Docker daemon pulled the “hello-world” image from the Docker Hub.
The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
If the process doesn’t exit on its own, press CTRL-C.

This simple test does not show one of the main benefits of Docker Compose — being able to bring a group of Docker containers up and down all at the same time. The How To Install WordPress and PhpMyAdmin with Docker Compose on Ubuntu 14.04 articles show how to use Docker Compose to run three containers as one application group.

Step 4 — Learning Docker Compose Commands

Let’s go over the commands the docker-compose tool supports.

The docker-compose command works on a per-directory basis. You can have multiple groups of Docker containers running on one machine — just make one directory for each container and one docker-compose.yml file for each container inside its directory.

So far we’ve been running docker-compose up on our own and using CTRL-C to shut it down. This allows debug messages to be displayed in the terminal window. This isn’t ideal though, when running in production you’ll want to have docker-compose act more like a service. One simple way to do this is to just add the -d option when you up your session:

docker-compose up -d
docker-compose will now fork to the background.

To show your group of Docker containers (both stopped and currently running), use the following command:

docker-compose ps
For example, the following shows that the helloworld_my-test_1 container is stopped:

Output of `docker-compose ps`
Name Command State Ports
helloworld_my-test_1 /hello Exit 0
A running container will show the Up state:

Output of `docker-compose ps`
Name Command State Ports
nginx_nginx_1 nginx -g daemon off; Up 443/tcp, 80/tcp
To stop all running Docker containers for an application group, issue the following command in the same directory as the docker-compose.yml file used to start the Docker group:

docker-compose stop
Note: docker-compose kill is also available if you need to shut things down more forcefully.

In some cases, Docker containers will store their old information in an internal volume. If you want to start from scratch you can use the rm command to fully delete all the containers that make up your container group:

docker-compose rm
If you try any of these commands from a directory other than the directory that contains a Docker container and .yml file, it will complain and not show you your containers:

Output from wrong directory
Can’t find a suitable configuration file in this directory or any parent. Are you in the right directory?

Supported filenames: docker-compose.yml, docker-compose.yaml, fig.yml, fig.yaml
Step 5 — Accessing the Docker Container Filesystem (Optional)

If you need to work on the command prompt inside a container, you can use the docker exec command.

The Hello World! example exits after it is run, so we need to start a container that will keep running so we can then use docker exec to access the filesystem for the container. Let’s take a look at the Nginx image from Docker Hub.

Create a new directory for it and change into it:

mkdir ~/nginx && cd $_
Create a docker-compose.yml file in our new directory:

nano docker-compose.yml
and paste in the following:

image: nginx
Save the file and exit. We just need to start the Nginx container as a background process with the following command:

docker-compose up -d
The Nginx image will be downloaded and then the container will be started in the background.

Now we need the CONTAINER ID for the container. List of all the containers that are running:

docker ps
You will see something similar to the following:

Output of `docker ps`
e90e12f70418 nginx “nginx -g ‘daemon off” 6 minutes ago Up 5 minutes 80/tcp, 443/tcp nginx_nginx_1
Note: Only running containers are listed with the docker ps command.

If we wanted to make a change to the filesystem inside this container, we’d take its ID (in this examplee90e12f70418) and use docker exec to start a shell inside the container:

docker exec -it e90e12f70418 /bin/bash
The -t option opens up a terminal, and the -i option makes it interactive. The /bin/bash options opens a bash shell to the running container. Be sure to use the ID for your container.

You will see a bash prompt for the container similar to:

From here, you can work from the command prompt. Keep in mind, however, that unless you are in a directory that is saved as part of a data volume, your changes will disappear as soon as the container is restarted. Another caveat is that most Docker images are created with very minimal Linux installs, so some of the command line utilities and tools you are used to may not be present.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *


Этот сайт использует Akismet для борьбы со спамом. Узнайте, как обрабатываются ваши данные комментариев.