Docker-compose: Docker-compose

Docker-compose is the official open source container creation, multi-container orchestration tool.

We know that a complete application system often consists of multiple containers that provide services mutually. For example, if a Web application needs to contain Web services, databases, etc., docker-compose can be easily used to control and manage multiple containers.

Open source: github.com/docker/comp…


Docker-compose installation

Docker-compose is written in Python and can be installed via PIP package management

PIP install docker - compose # or yum install wget https://repo.huaweicloud.com/docker-ce/linux/centos/docker-ce.repo - O D /docker-ce. Repo yum install docker-compose # clone GitHubreleaseCopy the code

Docker-compose is composed by docker-compose

Docker Compose manages three layers of containers, which are project, Service and Container.

The docker-compose. Yml file resides in the docker-compose project. A project contains multiple services, and each service defines the image, container name, port, network and other parameters of the container. That is, a service can contain multiple container instances.

A docker-comemage. yml file is shown below

version: '2.3'  
services:
  log:
    image: Goharbor/harbor - log: v2.1.5
    container_name: harbor-log
    restart: always
    dns_search: .
    cap_drop:
      - ALL
    cap_add:
      - CHOWN
      - DAC_OVERRIDE
      - SETGID
      - SETUID
    volumes:
      - /var/log/harbor/:/var/log/docker/:z
      - type: bind
        source: ./common/config/log/logrotate.conf
        target: /etc/logrotate.d/logrotate.conf
      - type: bind
        source: ./common/config/log/rsyslog_docker.conf
        target: /etc/rsyslog.d/rsyslog_docker.conf
    ports:
      - 127.0. 01.: 1514-10514
    networks:
      - harbor
  registry:
    image: Goharbor/registry - photon: v2.1.5
    container_name: registry
    restart: always
    .
Copy the code

Docker-compose docker-compose

  • Ps: Lists all running containers
docker-compose ps
Copy the code
  • Logs: View service log output
docker-compose logs
Copy the code
  • Build: Builds or rebuilds the service
docker-compose build
Copy the code
  • Start: starts an existing container for the specified service
docker-compose start nginx
Copy the code
  • Stop: stops a container for running services
docker-compose stop nginx
Copy the code
  • Rm: Delete the container of the specified service
docker-compose rm nginx
Copy the code
  • Up: Builds and starts the container
docker-compose up
Copy the code
  • Kill: Stops a container for a specified service by sending the SIGKILL signal
docker-compose kill nginx
Copy the code
  • Pull: Downloads the service image
Version: '2' services: db: image: postgres Web: build:. Command: bundle exec Rails s-p 3000-b '0.0.0.0' volumes: - .:/myapp ports: - "3000:3000" depends_on: - db $ docker-compose pull db Pulling db (postgres:latest)... latest: Pulling from library/postgres cd0a524342ef: Pull complete 9c784d04dcb0: Pull complete d99dddf7e662: Pull complete e5bff71e3ce6: Pull complete cb3e0a865488: Pull complete 31295d654cd5: Pull complete fc930a4e09f5: Pull complete 8650cce8ef01: Pull complete 61949acd8e52: Pull complete 527a203588c0: Pull complete 26dec14ac775: Pull complete 0efc0ed5a9e5: Pull complete 40cd26695b38: Pull complete Digest: sha256:fd6c0e2a9d053bebb294bb13765b3e01be7817bf77b01d58c2377ff27a4a46dc Status: Downloaded newer image for postgres:latestCopy the code
  • Scale: Sets the number of containers for the specified service luck.
docker-compose scale nginx=3 redis=3
Copy the code
  • Run: Executes a command on a service
docker-compose run web bash
Copy the code

For details about commands and descriptions, see the official documentsDocs.docker.com/compose/ref…


Docker-compose practice

Deploy a Python Web application using Docker-compose.

1. Create a project directory
 mkdir mycompose
 cd mycompose
Copy the code
2. Create a flask application app.py
import time

import redis
from flask import Flask

app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)

def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)

@app.route('/')
def hello():
    count = get_hit_count()
    return 'Hello World! I have been seen {} times.\n'.format(count)
Copy the code
3. Create arequirements.txt
flask
redis
Copy the code
4. CreateDockerfilefile
# syntax=docker/dockerfile:1 FROM Python :3.7-alpine WORKDIR /code ENV FLASK_APP=app.py ENV FLASK_RUN_HOST=0.0.0.0 RUN apk add --no-cache gcc musl-dev linux-headers COPY requirements.txt requirements.txt RUN pip install -r requirements.txt  EXPOSE 5000 COPY . . CMD ["flask", "run"]Copy the code
5. Create yoursdocker-compose.ymlContains two containers, Web and Redis
Version: "3.0" Services: Web: build:. Ports: - "5000:5000" Redis: image: "Redis :alpine"Copy the code
6. Build and run docker-compose project
[root@VM-0-5-centos mycompose]# ls app.py docker-compose. Yml Dockerfile requirements. TXT docker-compose upCopy the code

Start building the container

Docker PS can check that the two containers have been up

Verify the installation http://ip. The following installation is complete.

Update docker-comemage. yml to add mount

Version: "3.0" Services: web: build:. Ports: - "5000:5000" volumes: -.:/code environment: FLASK_ENV: development redis: image: "redis:alpine"Copy the code

Edit app. Py

import time import redis from flask import Flask app = Flask(__name__) cache = redis.Redis(host='redis', port=6379) def get_hit_count(): retries = 5 while True: try: return cache.incr('hits') except redis.exceptions.ConnectionError as exc: if retries == 0: Raise exc retries -= 1 time.sleep(0.5) @app.route('/') def hello(): raise exc retries -= 1 time.sleep(0.5) @app.route('/') def hello(): Get_hit_count () return 'I have been seen {} times.\n'. Format (count) #Copy the code

Rebuild the Web container

[root@VM-0-5-centos mycompose]# docker-compose up -d
Starting mycompose_web_1 ... 
Starting mycompose_web_1 ... done
Copy the code

The web page is as follows:

Docker – compose basis after use, in-depth study refer to official document docs.docker.com/compose/ref…


Feel free to point out any shortcomings in the comments section.

Favorites, likes and questions are welcome. Follow the top water cooler managers and do something other than pipe hot water.