Docker Compose principle

Source: Internet
Author: User
Tags docker compose

The advantages of Docker are obvious, especially for developers, which provides a new mechanism for software release. This means using Docker mirroring as a vehicle for software products, using Docker containers to provide a separate software runtime context, and using Docker hubs to provide a central management of mirroring, most importantly using Dockerfile Defines the internal behavior and key properties of the container to support software operation. But the actual production environment often needs to define a large number of Docker containers, and the containers are intricately interconnected. Manually documenting and configuring these complex container relationships is inefficient and error-prone. So, we urgently need an orchestration and deployment tool that defines a container cluster as Dockerfile defines a Docker container . As a result, the Docker Compose appeared (in fact, the fig appeared, Docker acquired the Fig and renamed Compose).
Dockerfile reproduces a container, and compose is used to reproduce the container's cluster.
Description: The demo environment for this article is Ubuntu 16.04.

Orchestration and Deployment

Orchestration (orchestration)
Orchestration refers to the coupling between the objects being deployed, the dependencies of the deployed objects on the environment, the order in which the actions are executed in the deployment process, the storage location and access of the dependent files and deployed files required by the deployment process, and how to verify the success of the deployment. This information will be in the Orchestration tool in the specified format (such as configuration files or specific code) to require operators to define and save, so that the process can be in a new environment at any time reliably and orderly reproduction.

Deployment (Deployment)
Deployment is the execution of the environment initialization on the target machine, the specified dependent files, the specified deployment actions, and finally the implementation of the rules in the orchestration to confirm the deployment success, according to the content and process specified in the orchestration.

So, the choreography is a conductor, his brain stores the entire music of the playing process, for each section of music to play the way to clear the chest. The deployment is the entire band, they are strictly in accordance with the intention of the conductor with musical instruments to complete the execution of the score. In the end, the two work together to create a high-grade symphony by combining, overlapping, and bridging each player's indie performance. This is the mission that Docker compose to accomplish.

Compose principle

The author demonstrates the official example in the previous article "Docker Compose Introduction", this article does not repeat, next we explore the Compose work principle. Start by understanding the concepts that are often mentioned in the two compose:

Project
An artifact managed by Docker compose is abstracted as a project, which is a complete business unit consisting of a set of associated application containers. The simple point is that a docker-compose.yml file defines a project.
We can specify the project name with the-p option when executing the docker-compose command, or, if not specified, the directory name where the Docker-compose.yml file resides.

Service
A container that runs an app can actually be one or more containers that run the same image. You can specify the number of containers that a service runs by using the--scale option of the docker-compose up command, such as:

$ docker-compose up-d--scale redis=2

Having learned the basic concepts above, let's take a look at the compose invocation process:

The docker-compose in the upper-right corner defines a set of service to form a project, establishing a relationship with the container (service-to-container correspondence) through the definition of service in DOCKER-COMPOSE.YML, and finally using Container to complete the call to Docker-py (the Python version of the Docker client) to initiate an HTTP request to Docker daemon. Note that the project, service, and container here correspond to the data structures in the Docker-compose implementation. Let's combine to introduce the general process of docker-compose work.

first , the docker-compose up command executed by the user invokes the Start method in the command line, which is very simple. A docker-compose.yml file defines a command-line parameter that project,docker-compose up provides as the startup parameter for this project to be processed by the project module.

then , if the current host already has a container corresponding to the application, Docker-compose makes a behavioral logical decision. If the user specifies that the existing service can be restarted, Docker-compose executes the container restart method for the Service module, or the existing container is started directly. The difference between these two operations is that the former will stop the old container, create and start a new container, and remove the old container. Each custom parameter that creates a container in this procedure is passed in from the Docker-compose up command and the DOCKER-COMPOSE.YML.

next , the method of launching the container is also very concise, which completes the encapsulation of the main parameters required for a Docker container to start, and executes the boot in the container module.

finally , the Contaier module invokes the Docker-py client to perform a POST request to the Docker daemon to create the container.

This shows docker-compose work of the overall process is very clear, concise!

Restart Services

Before we mentioned that the current host already has a container corresponding to the application, Docker-compose will determine whether or not to restart the existing service. Here's a demo to illustrate a few common scenarios (we still use the official demo described earlier).

Mandatory Recreate
Recreate is to delete the existing container and recreate the new container, specifying the--force-recreate option for the Docker-compose Up command can force the recreate container:

Create individual containers
If the container for the individual service in the app is deleted, the Docker-compose up command creates a new related container:

Start individual containers
Similarly, if the container for the individual service in the app is stopped (stop), the docker-compose up command restarts the associated container:

Summary

The overall sense of docker-compose is not complicated. This article simply combs through Docker-compose's overall execution process from an overview point of view, the main purpose of which is to understand how it works. As for the details of the relevant usage techniques, I will introduce them in the next article.

Reference:
Docker Container and Container cloud

Docker Compose principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.