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. The following syntax avoids the unauthorized error that occurs when using sudo.
Let's 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@kvmde67-19464:~# 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 1898 0 --:--:-- --:--:-- --:--:-- 1898
100 16.7M 100 16.7M 0 0 5709k 0 0:00:03 0:00:03 --:--:--:-- 9427k
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@kvmde67-19464:~# 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, we 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@kvmde67-19464:~/hello-world# docker-compose up
Pulling my-test (hello-world:)
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76
Status: Downloaded newer image for hello-world:latest
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@kvmde67-19464:~/hello-world# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cfaaf998ecd6 hello-world "/hello" 45 seconds ago Exited (0) 43 seconds ago hello-world_my-test_1