preface

When we want to start a series of interdependent services at the same time, it becomes tedious to start them one by one in strict order. Docker Compose is now available to perform these operations.

Compose

Compose is a tool for defining and running multi-container Docker applications. With Compose you can use the YML file to configure all the services your application needs, and then create and start all the services from the YML file configuration with a single command.

Compose the use of

  • Use Dockerfile to define your application’s environment.
  • Use docker-comemage.yml to define the services that make up the application so that they can run together in an isolated environment.
  • Finally, execute the docker-compose up command to get the entire application up and running.

demo Dockerfile

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
COPY.
CMD ["flask"."run"]
Copy the code
  • The FROM python: 3.7 - alpine: Build an image from the Python 3.7 base image.
  • WORKDIR /code: Sets the working directory to /code.
  • ENV FLASK_APP app.py,ENV FLASK_RUN_HOST 0.0.0.0: Sets environment variables.
  • COPY requirements.txt requirements.txt,RUN pip install -r requirements.txtCopy requirements.txt and install Python dependencies.
  • COPY . .: Copies the current directory in the. Project to the working directory in the. Mirror.
  • CMD ["flask", "run"]Flask run: The container provides the default command: flask run.

docker-compose.yml

version: "3"
services:
  mongo:
    image: Mongo: 3.4.24 - xenial
    command: mongod --port 27017
    ports:
      - "27017:27017"
  demo:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - mongo
Copy the code

Build and run the service

Docker-compose up, which is composed with -d if you want to run it in the background.

Yml configuration reference

version

Version: “3” specifies which version of compose this YML compliant is composed for.

build

Specify build parameters

version: "3"
services:
  demo:
    build:
      context: ./dir
      dockerfile: demo/Dockerfile
      args:
        buildno: 1
      labels:
        - "com.example.description=yeqiongzhou demo"
        - "com.example.department=China Shanghai"
        - "com.example.label-with-empty-value"
      target: prod
Copy the code
  • Context: indicates the context path.
  • Dockerfile: Specifies the dockerfile name to build the image.
  • Args: Add build parameters, which are environment variables that can only be accessed during the build process.
  • Labels: Sets the labels of the build image.
  • Target: Multi-layer build, which can specify which layer to build.

command

Command: [“go”, “run”, “main.go”] overrides the default commands specified in the Dockerfile container.

container_name

Container_name: yeqiongzhou-demo Specifies the custom container name instead of the generated default name.

depends_on

Set up dependencies.

  • Docker-compose up: starts services in dependency order. In the following example, DB and Redis are started before demo is started.
  • Docker-compose up SERVICE: automatically contains SERVICE dependencies. In the following example, docker-compose up demo will also create and start db and Redis.
  • Docker-compose stop: Stops services in dependency order. In the following example, demo stops before DB and Redis.
version: "3"
services:
  demo:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres
Copy the code

entrypoint

Entrypoint: /code/ entryPoint. sh overrides the container’s default entryPoint.

environment

Add environment variables. You can use arrays or dictionaries, and any Boolean value, which needs to be enclosed in quotes to ensure that the YML parser does not convert it to True or False.

environment:
  RACK_ENV: development
  SHOW: 'true'
Copy the code

expose

Exposed ports, but not mapped to the host, only accessible by the connected service, and only the internal port can be specified as a parameter.

expose:
  - "6666"
  - "8888"
Copy the code

healthcheck

Used to check whether docker service is running healthily.

healthcheck:
  test: ["CMD"."curl"."-f"."http://localhost:8888/health"] Set up the detection program
  interval: 60s # Set detection interval
  timeout: 10s Set the detection timeout
  retries: 3 Set the number of retries
  start_period: 30s The number of seconds to start the detection program after startup
Copy the code

image

Specifies the image that the container runs on. The following formats are acceptable:

  • image: golang
  • Image: ubuntu 18.04
  • image: yeqiongzhou/demo
  • image: example-registry.com:5000/redis
  • Image: a4bc65fd

logging

Logging configuration for the service. Driver: Specifies the logging driver of the service container. The default value is json-file. There are three options:

  • driver: “json-file”
  • driver: “syslog”
  • driver: “none”

With the JSON-file driver only, you can use the following parameters to limit the number and size of logs. When the file limit is reached, old files are automatically deleted.

logging:
  driver: json-file
  options:
    max-size: "200k" The single file size is 200K
    max-file: "10" # maximum 10 files
Copy the code

In the syslog driver, you can specify the syslog receiving address by using syslog-address.

logging:
  driver: syslog
  options:
    syslog-address: "TCP: / / 192.168.0.1:1024"
Copy the code

restart

  • No: is the default restart policy. The container will not be restarted under any circumstances.
  • Always: The container is always restarted.
  • On-failure: Restarts containers only if the container exits abnormally (exit status is non-zero).
  • Unless -stopped: The container is always restarted when the container exits, but does not consider containers that were stopped when the Docker daemon started.

volumes

Mount the host data volume or file to the container.

version: "3"
services:
  db:
    image: postgres:latest
    volumes:
      - "/localhost/postgres.sock:/var/run/postgres/postgres.sock"
      - "/localhost/data:/var/lib/postgresql/data"
Copy the code