Docker is a software for automating the deployment and management of applications in an operating system-level virtualization environment. It allows you to package an application with all its environment and dependencies into a container that can be ported to any Linux system with kernel cgroups support and provides a container management environment
Compose a tool for creating and running multi-container Docker applications. In Compose, you use a special file to configure your application services
The features of Compose that make it effective are:
- Multiple isolated environments on a single host
- Protection of volume data when containers are created
- Only changed containers are recreated
- Moving variables between environments
Before installing Docker Compose, make sure Docker is installed, see this manual for details on installation.
Installing Docker Compose
Let's install Docker Compose from the Docker repository on GitHub
Check current version and update it if necessary using the following command:
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[root@kvmde54-19861 ~]# sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 638 100 638 0 0 869 0 --:--:-- --:--:-- --:--:-- 869
100 16.7M 100 16.7M 0 0 5008k 0 0:00:03 0:00:03 --:--:--:-- 8196k
Next we adjust the permissions:
sudo chmod +x /usr/local/bin/docker-compose
Then we will check that the installation was successful:
docker-compose --version
You will see a similar output:
[root@kvmde54-19861 ~]# docker-compose --version
docker-compose version 1.25.5, build 8a1c60f6
Starting the container with Docker Compose
The Docker registry, Docker Hub, contains a Hello World image used for demonstration and testing. It demonstrates the minimum configuration parameters needed to start a container using Docker Compose: a YAML file calling a separate image:
Create a directory for the YAML file:
mkdir hello-world
Let's go into it:
cd hello-world
Next, create a YAML file in that directory:
nano docker-compose.yml
Put the following data into the file, save it, and close the text editor:
my-test:
image: hello-world
The first line of the YAML file is used as part of the container name
The second line specifies which image is used to create the container
When you run the docker-compose up command, it will search for the local image by the specified name, i.e. hello-world.
Next, you can view the images on our system using the docker images command:
docker images
When there are no local images, only the column headers will be displayed:
REPOSITORY TAG IMAGE ID CREATED SIZE
Next, while in the ~/hello-world directory, we run the following command:
docker-compose up
After loading the image, docker-compose creates a container, puts it in, and runs the hello program:
[root@kvmde54-19861 hello-world]# docker-compose up
Creating hello-world_my-test_1 ... done
Attaching to hello-world_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 |
my-test_1 | To generate this message, Docker took the following steps:
my-test_1 | 1. The Docker client contacted the Docker daemon.
my-test_1 | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
my-test_1 | (amd64)
my-test_1 | 3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
my-test_1 | 4. The Docker daemon streamed that output to the Docker client, which sent it
my-test_1 to your terminal.
my-test_1 |
my-test_1_1 | To try something more ambitious, you can run an Ubuntu container with:
my-test_1 | $ docker run -it ubuntu bash
my-test_1 |
my-test_1 | Share images, automate workflows, and more with a free Docker ID:
my-test_1 | https://hub.docker.com/
my-test_1 |
my-test_1 | For more examples and ideas, visit:
my-test_1 | https://docs.docker.com/get-started/
my-test_1 |
hello-world_my-test_1 exited with code 0
Docker containers keep running as long as the command remains active, so when the hello container finishes, it stops. Consequently, when we view active processes, the column headers will appear, but the hello-world container will not appear in the list because it is not running.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
We can view the container information by using the -a flag to display all containers, not just the active ones:
docker ps -a
[root@kvmde54-19861 hello-world]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf367c448061 hello-world "/hello" About a minute ago Exited (0) About a minute ago hello-world_my-test_1
Docker Compose Commands*
The docker-compose command works at the directory level. You can have multiple container groups on the same machine; to do this, you need to create a separate directory and a separate docker-compose.yml file for each container.
You have already run the docker-compose up command and stopped the process with CTRL-C. However, in a production environment, the docker-compose tool must run as a service. To do this, simply add the -d option:
docker-compose up -d
This command starts docker-compose in the background.
To view a group of Docker containers (both running and disabled), use the following command:
docker-compose ps
For example, the output might look like this:
[root@kvmde54-19861 hello-world]# docker-compose ps
Name Command State Ports
------------------------------------------------
hello-world_my-test_1 /hello Exit 0
To stop all Docker containers in an application-specific group, run the following command in the directory that contains the docker-compose.yml file that started the Docker group:
docker-compose stop