Docker Compose In Practice

Continue to advance to Kubernetes, the last article Docker Swarm In Action, After understanding Swarm, it is necessary to get familiar with Docker Compose. Docker Swarm forms a cluster of Docker hosts. As long as the Manager node is notified when the service is deployed, it will automatically find the corresponding node to run containers. Compose is another concept entirely. It organizes multiple associated containers into a whole for deployment, such as a load balance container, multiple web containers, and a Redis cache container to make up a whole bundle.

Revealed by its name, Compose, the concept of container orchestration was officially established. Later, Kubernetes, which we will learn, it's a tool for higher-level organization, operation and management of containers. Because Compose organizes the containers, it can start multiple associated containers with one command, instead of starting one container separately.

Regarding the installation of Docker Compose, Docker Desktop under Mac OS X comes with docker-compose; since Docker Compose is written in Python, we can use  pip install docker-compose  to install it, and use its commands after installation docker-compose.

The differences between Docker Container, Swarm, and Compose show in this picture.

Compose consists of multiple containers, which can be deployed as a whole to a single Docker host or Swarm host cluster.

Let’s experience the functions of Docker Compose and design a service with the following containers

  1. The front-end load balancing server is played by HAProxy, and request will be forwarded to the next following two web containers
  2. Two web containers, demonstrated with Python Flask
  3. A Redis cache container, the Web will access the cached data in the container

The files involved in defining Docker Compose are Dockerfile and docker-compose.yml. We need to create a Dockerfile for each custom Docker container. If one container directly use the Docker image, the no Dockerfile required. In other words, there can be one or more Dockerfiles in a Compose. Here is an example of directory structure of one compose

myapp/
    - Dockerfile
    - docker-compose.yml

If we need to use multiple Dockerfiles, the Dockerfile must be placed in a different directory. In this case, the directory structure needs to be adjusted as follows (plus several auxiliary files that will be used later)

myapp/
    - proxy/
        - Dockerfile
        - haproxy.cfg
    - web/
        - Dockerfile
        - app.py
        - requirements.txt
    - docker-compose.yml

Next, look at the content of each file

myapp/proxy/Dockerfile

myapp/proxy/haproxy.cfg

This is the simplest haproxy.cfg configuration file. It just forwards the request to web_a and web_b in a rotating manner. We will see web_a and web_b in the docker-compose.yml file later.

myapp/web/Dockerfile

myapp/web/requirements.txt

myapp / web / app.py

Use a simple Flask App to demonstrate a web application. The access count is saved in Redis, and responds the hostname of the machine where the request is currently processed.

myapp/docker-compose.yml

We have built two Docker images. Redis:alpine is used directly for the image. The port and mapping are defined here, so EXPOSE is needed to define the port number in the Dockerfile. We will start two web services, web_a, web_b, and the haproxy.cfg in front will forward the request to these two containers.

Now we can go to the myapp directory under the command line and execute the command

$ docker-compose up -d

If first time run, this command will also build Docker images for myapp_proxy, myapp_web_a, and myapp_web_b, and then they will be started. If those images already exist locally, start those containers directly. After the startup is complete, run docker ps to check

Four containers are all started, one haproxy, two web, and one redis, which is exactly what we need. Let’s verify the whole service behavior now:

Load balancing, web services, and Redis are working together as our intention.

If we run docker kill 56 to get myapp_web_a killed, it can't be automatically recover. But running docker-compose run -d again will just save myapp_web_a back, won't impact other containers.

please run docker-compose -h to learn the detailed usage of this command, and a lot of them are similar to docker command, following are only for  docker-compose command

  1. docker-compose images: Display the docker image used by current compose
  2. docker-compose logs: Display the running logs of the current compose
  3. docker-compose down: stop all containers involved in compose
  4. docker-compose kill: kill all the containers involved in compose without using docker kill to kill them one by one
  5. docker-compose restart: restart the entire compose service (all containers)

There is a simpler way to support HAProxy + multiple dynamic web containers

Modify the previous docker-compose.ymlcontents of the file are as follows:

proxy selected dockercloud/haproxy image, so, we can remove myapp/proxy folder along with Dockerfile and haproxy.cfg.

Now start with the same command docker-compose up -d

There is only one web container, and now it can be dynamically expanded, using docker-compose up --scale web=d -d

The three requests were processed by three different web containers.

Note:

  1. docker-compose scale web=2: The command is not recommended, we should run docker-compose up --scale web=2 -d instead
  2. docker-compose.yml: The configuration deploy can only work with Swarm mode. While deploy in replicas works with docker stack deploy command, and is ignored by docker-compose up and docker-compose run

About running Compose on the Swarm cluster

It seems a bit troublesome, see the official Use Compose with Swarm . We need to use docker stack to deploy Compose. We won't do the actual operation for now, and not sure if  there are many such combination scenarios in a real production environment. The key is to understand how the containers are distributed among the Swarm nodes after deployment. Is Compose still kept as a whole? or extract containers from Compose to deploy to Swarm cluster?

From  a picture in Deploy Docker Compose (v3) to Swarm (mode) Cluster

From above picture, Swarm does extract container from a Compose, then deploy each container to Swarm cluster instead of treating Compose as a whole.

link:

  1. How to use Docker Compose to run complex multi container apps on your Raspberry Pi
  2. Docker Compose multi-container deployment (5)
  3. Introduction to Docker-Compose Installation and Use

本文链接 https://yanbin.blog/docker-compose-in-practice/, 来自 隔叶黄莺 Yanbin Blog

[版权声明] Creative Commons License 本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments