In the previous article, Docker primer for the backend: DockerFile command in detail, we explain the use and command of docker image construction script DockerFile, the emergence of DockerFile makes the process of building docker image more intuitive and efficient, but, and MOST of my previous articles mentioned that question.

Is that all?

Of course not

This article is the fifth part of the Docker introductory tutorial. In the first four parts, the basic concept of the actual practice and DockerFile, we go step by step from what Docker is, to using DockerFile to build their own image. I believe that those who complete these tutorials have a good understanding of Docker, and can deal with some of the practical problems encountered in reality, but there are still many problems with better solutions. In this article, I will introduce a Docker automatic deployment artifact, Docker-compose, It can enable us to automate the traditional tedious Docker operation instructions, and can control multiple containers to realize the batch start of multiple containers.

No nonsense, just look at things.

What kind of problem does Docker-Compose solve?

If you are familiar with the micro-service architecture before, the automatic deployment of Docker-compose is absolutely new. Here, it is briefly mentioned that the micro-service architecture is to split the traditional single service into multiple single small services, thus realizing the horizontal expansion of the application. Take a large e-commerce platform as an example. Micro service is applied before a huge monomer split into multiple services, such as the warehouse system separately as a service, ordering system as an application separately to provide services, the benefits of this is we don’t need to like the traditional way to upgrade the server, and only need according to the pressure of the specific business situation to upgrade the corresponding server is good, For example, there is a lot of pressure on the ordering system on November 11, so I just upgrade the server of the ordering system without upgrading the hardware configuration of the whole system.

However, there are certain dependencies between microservices. For example, Eureka, as the registry in SpringCloud, must be started first. Otherwise, the following services cannot be connected to the registry when they are started, and it is ok if they are not registered. O&m personnel need to enter many commands in sequence to ensure that each service starts in the correct order:

Docker run -d service A Docker run -d service B, must be started after ACopy the code

Two services would be fine, ten or eight would be fine, but dozens or hundreds of services? According to the order of the start, in case which is not good, in return is the operation and maintenance staff a fuck. Docker-compose and DockerFile have similar advantages, but DockerFile encapsulate the construction process of the image into a script. Docker-compose can encapsulate the operation process of the image into a specific script, which means that we can organize the startup sequence of each container and write it into the script. The operation and maintenance engineers only need to run the script each time, without executing the run command to start the container successively.

Docker-compose is called a “killer” because it really solves the pain points, guys.

Docker – compose installation:

About docker – compose this installation, still has a lot of online tutorials, but all three words, too much trouble, this is still the continuation of traditional, said only the simplest one, to ensure the computer installed python3 and docker

Don’t use yum?

A: Not this time, PIP installation works better

Why don’t you use python2? Love python2 users strongly condemn

A: I tried piP2, but it didn’t work, so I recommend pip3.

Note: can not install python3 friends, forget, I also incidentally write up. Also, make sure your server has Docker installed.

Open a Linux terminal and type the following command:

# yum install python3 # yum install python3 # docker-compose Pip3 install Docker-compose pip3 install Docker-compose pip3 install Docker-compose pip3 install Docker-composeCopy the code

To check whether the installation is successful, enter:

docker-compose version
Copy the code

Docker-compose is successful on our machine if it displays a published docker-compose. By the way, my version is 1.24.1.

Docker – compose of actual combat:

First create a new folder, built also, to prevent a moment can’t find oneself put yml file which, by the way, docker – compose script format is yaml file format, do not understand friend can go down a lot, The default file name is docker-comemess. yml or docker-comemess. yaml

Create a docker-comemess. yml file in the new folder and enter the following content. Here we will use Tomcat as an example:

mytomcat:
    image: tomcat
    ports:
       - "8086:8080"
Copy the code

Yml: docker-compose: docker-compose: docker-compose: docker-compose:

Docker-compose up ## start the container from the yML fileCopy the code

The docker-compose up command is composed by default, and the docker runit command is composed by default. The docker-compose up command is composed by default, and the docker runit command is composed by default

If you want the container to start in the background, just add -d to the end, as follows:

docker-compose up -d
Copy the code

If the startup is successful, it is displayed

[root@iZbp1d7upppth01hp demo]# docker-compose up -d
Starting demo_mytomcat_1 ... done
Copy the code

When docker ps is executed, it will be found that tomcat has started normally. The name is DEMO_myTomcat_1, corresponding to folder, container name, and number respectively. If we start it again, the new Tomcat container name will be demo_myTomcat_2

Docker-compose construct script for docker-compose

Now that the container is running successfully, let’s take a closer look at the format that the docker-comemage.yml file should follow.

First layer:

  • Mytomcat: We declare the name of the container to build. A YAML file can define multiple containers.

And then:

  • Image: the source of the image we built. Here is the Tomcat image. If you need to specify the version, you can write it in tomcat:8 format

    At this point one might ask, what if I want to use my own mirror image? It is also possible, just write it in the following format:

    mytomcat:
        bulid: . Docker-compose is composed for DockerFile, which is composed for.docker-compose, which is composed for.docker-compose, which is composed for.docker-compose
        ports:
           - "8086:8080"
    Copy the code
  • Ports: corresponds to the -p parameter of docker run, which is used to map ports. Ports can be listed without quotation marks, but YAML will parse them as sexadecimal numbers in base 60 in cases like 56:56, so it is highly recommended to use quotation marks.

Is that all? Didn’t? No, we can also perform operations such as setting environment variables, container volumes, links, commands, etc. in yML scripts.

  • Environment: equivalent to the -e parameter of the docker run command, used to set environment variables.

  • Volumes: equivalent to the -v parameter of the docker rum command, used to configure volumes as follows:

    mytomcat:
        image: tomcat
        ports:
           - "8086:8080"
        volumes: 
           - ./data:/data Mount the data folder in the current directory to the data folder in the container
    Copy the code
  • ** Links :** Is equivalent to the –link argument in the docker run command, which is used to link two containers. Links supports linking multiple containers, as follows:

    mytomcat:
        image: tomcat
        ports:
           - "8086:8080"
        links:
           -redis # link to the Redis container
           -mysql If you only need to link to one container, delete it
        volumes: 
           - ./data:/data Mount the data folder in the current directory to the data folder in the container
    Copy the code
  • Command: Use command to override the commands that are executed by default after the container is started.

  • Container_name: If you do not want to use the default generated < project name >< service name >< serial number > format name, you can use the Container_name option to customize the container name.

Of course, docker-compose certainly supports more than these commands, but the above commands are all commonly used by us. As for the other commands, such as log, I will not list them one by one here, and you can just search online when you need them.

As mentioned earlier, a YML script can define more than one container at a time. If you need to define more than one container, just write it on a separate line, but be careful about the indentation of the YAML file itself.

mytomcat01:
    image: tomcat
    ports:
       - "8086:8080"
       
mytomcat02:
    image: tomcat
    ports:
       - "8087:8080"
Copy the code

Yml file must be docker-comemess. yml. If I want to use a different name like xswl.yml, I can add -f and specify the path of the yML file.

docker-compose -f xswl.yml up -d
Copy the code

Docker – compose the command:

Docker-compose is composed for docker container, and docker-compose, like Docker, also provides many commands for us to use:

  • Up: Starts all the containers defined in the compose file and aggregates their log information together, usually with -d
  • Ps: Get state information for the container managed by Compose.
  • Run: Starts a container and allows a one-time command to be run simultaneously by linked containers.
  • Bulid: rebuild the image built by the DockerFile. The up command will not execute the command to build an existing image unless the image does not exist. Build is usually used when the image needs to be updated.
  • Logs: Aggregates the log information generated by the Compose managed container and outputs it in color.
  • Stop: Stops the container.
  • Rm: Delete stopped containers. Don’t forget to add -v to delete any docker-managed volumes.

If I suddenly don’t want to use Docker-compose, I can do that

Docker-compose stop # stop docker-composeCopy the code

If I suddenly want to use it again, I can do:

Docker-compose start docker-compose up # restart the same containerCopy the code

As for more detailed operations, you can visit and learn from the official website of Docker. I really can’t finish writing so many commands (I have no time to write them, and I have never seen some commands).

Let’s start with the technical summary:

Docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose Docker-compose will be used more and more in the next tutorial, so I hope you can take a good look at this article, because it is too slow to start docker-compose one by one, and because I am using a student server, I cannot start many awesome containers at all, so I can’t demonstrate this. In the next article, I will walk you through the step-by-step process of building a logging center using ELK.

Finally, thank you very much for reading this article, it is a very happy thing for me to help you, if you have any questions or criticism, please leave a comment at the bottom of this article, I will reply one by one if I have time.

Hansu’s study notes have all been open source to Github, please click a star

Long march always feeling, to a star line

Hanshu development notes

Welcome to like, follow me, have good fruit to eat (funny)