Docker is a good tool for automatically deploying Linux applications in software containers, but to fully utilize its potential, each component of the application should be in its own Run in a separate container. For complex applications with a large number of components, orchestrating all containers to start, communication and shutdown can quickly become difficult to handle.
The Docker community has proposed a popular solution called Fig, which allows you to use a single YAML file to orchestrate all Docker containers and configurations. This became so popular that the Docker team decided to make Docker Compose based on Fig sources, which is now deprecated. Docker Compose makes it easier for users to orchestrate the process of Docker containers, including starting, closing and setting up links and volumes within the container.
In this tutorial, we will show you how to install the latest version of Docker Compose to help you manage multi-container applications.
To read this article, you need an Ubuntu 18.04 server with the following:
sudo
command has been set up, and the firewall has been turned on. Students who don’t have a server can buy it from here, but I personally recommend you to use the free Tencent Cloud Developer Lab for experimentation, and then buy server.Once these are in place, you can follow up at any time.
**Note: **Although the prerequisites provide instructions for installing Docker on Ubuntu 18.04, as long as Docker is installed, the docker
command in this article can be run on other operating systems.
Although we can install Docker Compose from the official Ubuntu repository, it is a few minor versions behind the latest version, so we will install Docker Compose from Docker's GitHub repository. The following commands are slightly different from the commands you found on the "Version" page. By using the -o
flag to specify the output file first instead of redirecting the output, this syntax can avoid the permission denied error caused by using sudo.
We will check the current version, if necessary, please update in the following command:
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m`-o /usr/local/bin/docker-compose
Next we will set permissions:
sudo chmod +x /usr/local/bin/docker-compose
Then we will verify the success of the installation by checking the version:
docker-compose --version
This will print out the version we installed:
docker-compose version 1.21.2, build a133471
Now that we have installed Docker Compose, we are ready to run the "Hello World" example.
The public Docker registry Docker Hub contains a Hello World image for demonstration and testing. It illustrates the minimum configuration required to run a container with Docker Compose: call the YAML file of a single image:
First, we will create a directory for YAML files and move into it:
mkdir hello-world
cd hello-world
Then, we will create the YAML file:
nano docker-compose.yml
Put the following into the file, save the file, and exit the text editor:
my-test:
image: hello-world
The first line in the YAML file is used as part of the container name. The second line specifies the image used to create the container. When we run the docker-compose up
command, it will find the local mirror hello-world
by the name we specified. With this, we will save and exit the file.
We can use the following docker images
command to manually view the images on the system:
docker images
When there is no local mirror at all, only column headings are displayed:
REPOSITORY TAG IMAGE ID CREATED SIZE
Now, while still in the ~/hello-world
directory, we will execute the following command:
docker-compose up
When we run the command for the first time, if the local image hello-world
is not specified, Docker Compose will extract it from the Docker Hub public repository:
Pulling my-test(hello-world:latest)...
latest: Pulling from library/hello-world
c04b14da8d14: Downloading [==================================================>] c04b14da8d14: Extracting [==================================================>] c04b14da8d14: Extracting [==================================================>] c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for hello-world:latest
...
After pulling the image, docker-compose
creates a container, attaches and runs the hello program, and then confirms that the installation seems to be working:
...
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 |...
Then it prints out the explanation it did:
1. The Docker client contacted the Docker daemon.2. The Docker daemon pulled the "hello-world" image from the Docker Hub.3. The Docker daemon created a newcontainerfrom that image which runs the executable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
The Docker container only runs when the command is active, so once hello
finishes running, the container will stop. Therefore, when we look at the active process, the column headings will be displayed, but hello-world
will not list the container because it is not running.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
By using -a
to display the flags of all containers, we can see the container information, we will use it in the next step, not just the active container:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06069 fd5ca23 hello-world "/hello"35 minutes ago Exited(0)35 minutes ago drunk_payne
This will display the information we need to delete the container when we are done.
To avoid using unnecessary disk space, we will delete the local image. To do this, we need to use the docker rm
command to delete all containers referencing the image, and then delete the CONTAINER ID or NAME. Below, we use the CONTAINER ID in the docker ps -a
command we just ran. Be sure to replace the ID of the container:
docker rm 06069fd5ca23
Once all the containers referencing the image are deleted, we can delete the image:
docker rmi hello-world
We have now installed Docker Compose, tested our installation by running the Hello World example, and removed the test image and container.
Although the Hello World example confirms our installation, the simple configuration does not show one of the main advantages of Docker Compose-the ability to add and drop a group of Docker containers at the same time.
For more Ubuntu tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.
Reference: "How To Install Docker Compose on Ubuntu 18.04"
Recommended Posts