“This is the 13th day of my participation in the November Gwen Challenge. See details: The Last Gwen Challenge 2021”.

In actual business use, we usually need to manage and orchestrate a large number of containers, that is, to achieve the blueprint of container cluster, today we will learn about docker-compose, the official Docker orchestration tool.

The installation

Docker-compose is a binary file

apt-get install docker-compose
Copy the code

Check the version information after the installation

root@phyger-xubuntu:/home/phyger/docker-cps# docker-compose version
docker-compose version 1.171., build unknown
docker-py version: 2.51.
CPython version: 2.717.
OpenSSL version: OpenSSL 1.11.  11 Sep 2018
root@phyger-xubuntu:/home/phyger/docker-cps#
Copy the code

The preparatory work

Prepare an image with PYTHon3 installed, but you can also implement it in Dockerfile

The project to create the working directory, docker-compose

mkdir /home/phyger/docker-cps
Copy the code

Create an app in your working directory

#! /usr/bin/python3

from flask import Flask

app = Flask(__name__)
count = 0

@app.route('/')
def hello() :
    global count
    count += 1
    return 'Hello World! This page has been accessed {} times. \n'.format(count)


if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)
Copy the code

Create a Dockerfile in the working directory

FROM alpine_py:v1
ADD . /code
WORKDIR /code
ENTRYPOINT ["python", "app.py"]
Copy the code

Create docker-comemage.yml in your working directory

version:'3'
services:
  web:
    build: .
    ports:  
- "5000:5000"
Copy the code

Start the project

docker-compose up  <-d>
Copy the code

Testing services

Docker-compose project launch analysis

Docker-compose calls Dockerfile to build the image needed for the project, and then uses the Docker-compose file to orchestrate and manage the project. That is, image builds use Dockerfile functionality and focus on business container choreography themselves.

Docker – compose and k8s

Docker-compose is a tool for orchestrating and managing multiple containers, while K8S can orchestrate multiple containers across services and machines. At the same time, it has a variety of controller types, which can achieve more reliable container services.

They are both common service bearing modes, and we can choose them according to actual needs. Generally, we choose docker-compose, which is simple and saves resources, for small-sized stand-alone service, and K8S, which is large distributed service.