In the previous article, we used docker run command to start a container, and as a real online business environment, we must serve more than one container, which means that there must be more than one container, and if we still manually start the container one by one, it would be a bit hairy. Fortunately, docker Compose, A tool for defining and running multi-container Docker applications, with which we can launch multiple containers at once, is also a good fit with continuous integration tools (Jenkins).

Install the Docker Compose

  • Install the latest version according to the official
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Copy the code
  • Add execution permission to the installation script
chmod +x /usr/local/bin/docker-compose
Copy the code
  • Note: Docker-compose does not take effect after adding the script execution permission to the /usr/local/bin/ directory. Script execution permission is not added to this file
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

chmod +x /usr/bin/docker-compose
Copy the code
  • Docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose
# docker-compose --versionDocker-compose version 1.24.0, build 0AA59064Copy the code
  • Command completion tool
Sudo curl - https://raw.githubusercontent.com/docker/compose/1.24.0/contrib/completion/bash/docker-compose - L o /etc/bash_completion.d/docker-composeCopy the code
  • version

Docker – compose. Yml command

  • Docs.docker.com/compose/com…

Docker – compose command

  • Docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose
The command meaning The sample
build Build or rebuild the service build [options] [–build-arg key=val…] [SERVICE…] (Use docker-compose help build for details)
help View the docker-compose command help documentation Docker-compose help COMMAND docker-compose help COMMAND
up Build, create, recreate, start, and connect the relevant containers of the service. All connected services will be started unless they are already running (assuming the docker-comematery.yml file already exists in the directory) Docker-compose up docker-compose up -d docker-compose up
kill Sends SIGKILL to stop the container for the specified service Docker-compose kill apI-feign docker-compose kill apI-feign docker-compose kill apI-feign
start Starts an existing container for the specified service docker-compose start api-feign
stop Stops an existing container for the specified service docker-compose stop api-feign
logs View log output of the service Docker-compose logs –tail=”all” api-feign docker-compose logs –tail=”all” api-feign
ps List all containers Docker-compose docker ps-a, docker ps-a, docker ps-a, docker ps-a, docker ps-A, docker ps-A, docker ps-A, docker ps-A
rm Deletes the container for the specified service docker-compose rm api-feign
  • For more docker-compose commands, see the docker-compose official documentation

Write docker-comemage. yml to start multiple services

  • Server compose file docker-comemess.yml in any directory
version: '3.4'
services:
  configerver:  Specify a service nameImage: MAO/configserver: 0.0.1 - the SNAPSHOT# mirror name
    ports:
      - 8666:8666  # Specify port mapping
  eureka:
    image: test1/ Eureka1 :0.0.1 -snapshot ports: -8805:8805 server-admin: image: MAO /server-admin:0.0.1 -snapshot ports: -8806:8806 API-Feign: image: MAO/API-Feign :0.0.1 -Snapshot ports: -8840:8840 ribbon- Consumer: image: MAO /ribbon- Provider :0.0.1-SNAPSHOT Ports: -8830 ribbon- Provider: MAO /ribbon- Provider :0.0.1-SNAPSHOT ports: -8830 Ribbon - Provider: MAO /ribbon- Provider :0.0.1-SNAPSHOT ports: - 8820:8820Copy the code
  • Start multiple services by executing commands in docker-comemess. yml (docker compose installed)
Docker compose compose compose compose compose compose compose compose compose compose
docker-compose up -d 
Copy the code

  • The configuration files of all services are obtained from the configuration center. The configuration center is used to access the configuration files of all services. The configuration center is used to access the configuration files. Docker-compose starts up at the same time, so the docker-compose service will start up before other services are provided, which will cause other services to report errors. Therefore, in the production environment, we must control the startup sequence of services, that is, start the configuration center first, then start the registry, and finally start other services.

Docker Compose controls the service startup sequence

  • Docker Compose Compose controls the startup sequence of the Docker Compose service. This article uses the wait-for-it solution, including dockerize and wait-for solutions.
  • Wait-for-it is a bash script that waits for the availability of hosts and TCP ports. It can be used to synchronize the startup of interdependent services, such as linked Docker containers

How to use

  • Next I’ll show you how to use this script to control our service startup order
  • First we can package the script into our image and modify the Dockerfile file
The # Dockerfile. COPYwait-for-it.sh /wait-for-it.sh # copy wait-for-it-sh in the root directory of the project module to the image/directory
RUN chmod +x /wait-for-it.sh # change script permissions.Copy the code
  • Package the image again using the docker-Maven-plugin
  • Re-write the docker-comemage. yml file and add entryPoint to execute the wait-for-it-sh script that we packaged as a mirror to monitor whether the configuration center has provided the service. Note that the entrypoint directive will not be overwritten. Sh configerver:8666 Whether the configuration center provides services to enable other services by obtaining configurations from the configuration center
version: '3.7'
services:
  configerver:  Specify a service nounImage: MAO/configserver: 0.0.1 - the SNAPSHOT# mirror name
    ports:
      - 8666:8666  # Specify port mappingDepends_on: -Eureka Eureka: Image: MAO/EUreka :0.0.1-SNAPSHOT Ports: -8805 :8805 EntryPoint:"./wait-for-it.sh configerver:8666 -- java -jar /app.jar"Server-admin: image: MAO /server-admin:0.0.1-SNAPSHOT ports: -8806 :8806 depends_on: -Eureka - Configerver EntryPoint:"./wait-for-it.sh configerver:8666 -- java -jar /app.jar"Api-feign: Image: MAO/API-FEign :0.0.1-SNAPSHOT ports: -8840:8840 dependS_on: -Eureka - Configerver EntryPoint:"./wait-for-it.sh configerver:8666 -- java -jar /app.jar"Depends_on: Ribbon - Consumer: Image: MAO/Ribbon - Consumer :0.0.1-SNAPSHOT Ports: -8830 :8830 depends_on: - eureka - configerver entrypoint:"./wait-for-it.sh configerver:8666 -- java -jar /app.jar"Depends_on: Ribbon Provider: Image: MAO/Ribbon Provider :0.0.1-SNAPSHOT Ports: -8820:8820 depends_on: - eureka - configerver entrypoint:"./wait-for-it.sh configerver:8666 -- java -jar /app.jar"Depends_on: -Eureka - Configerver EntryPoint: - Image: MAO/Gateway :0.0.1-SNAPSHOT ports: -8081 :8081 - Eureka - Configerver EntryPoint:"./wait-for-it.sh configerver:8666 -- java -jar /app.jar"
Copy the code
  • Then we execute docker-compose command in the server to start the service with one click in the background, and all the services can be started without any problems
docker-compose up -d
Copy the code

The last

  • Docker Compose: Docker Compose: Docker Compose: Docker Compose: Docker Compose Kubeadm: Kubernetes 1.14.2 cluster deployment: Kubernetes 1.14.2

Refer to the link

  • Docker is installed but Docker Compose is not ? why?
  • wait for it Usage with Docker