Background of 0.

Docker-compose compose multiple docker containers into a group. So they can run together in an isolated environment.

1. Introduction

Docker-compose: can be used to define a group that is convenient for tools that help run multiple container groups. It uses YAML files to configure the application’s services, which can then be created and started from the configuration with a single command.

Generally speaking, after microservice docker-like, when you want to start microservice containers, you have to manually start each container one by one, which is very troublesome to manage when there are many services. Docker-compose solves some of the multi-container management problems.

Yaml files

Docker-compose tool can directly use a docker-compose. Yaml to organize and manage multiple containers, you can set the environment variables of each container, configure the service configuration item, just like docker run command to start the container. Here’s an example:

Docker command to manage containers:

$ docker run -p 50052:50051 \
  -e MICRO_SERVER_ADDRESS=:50051 \
  -e MICRO_REGISTRY=mdns \
  vessel-service
Copy the code

Equivalent to docker-compose to manage:

Version: '3.1' vessel-service: build:./vessel-service ports: -50052:50051 environment: MICRO_ADRESS: ":50051" MICRO_REGISTRY: "mdns"Copy the code

To add, subtract, and configure microservices, modify docker-comemage. yaml directly.

Steps for using Docker-compose

Using Docker-compose is basically a three-step process:

  • (1) Write Dockerfile for each sub-service
  • (2) Write docker-compose file: define the services that comprise the application in docker-compose. So they can run together in an isolated environment.
  • (3) Run docker-compose up and Compose Started and run the entire application.

Content description

Referring to the example above, you can see:

  • The version keyword: specifies that docker-compose is version 3.1
  • Services: Use Services to define multiple services, with each microservice defining its own container name
  • Build The Dockerfile in the specified directory will be used to compile the image
  • Environment variables specify container port mapping rules, environment variables, and so on.

Commonly used instructions

The docker-compose build can be used to compile and generate three corresponding images.

Run uses docker-compose run to run the specified container

Docker-compose up -d can be composed in the background.

Docker-compose ps [options] [SERVICE…] docker-compose ps [options]

Stop using docker Stop $(docker ps -aq) to stop all running containers. Or simply: docker-compose stop

Docker-compose down [options] Stops and deletes containers, networks, volumes, and images.

Reference 2.

docs.docker.com/compose/