Docker – compose is introduced
Use docker run and other commands to manually start the image, docker stop to stop the image, docker kill to kill the image process. This situation applies only when there are not many mirroring services. However, the reality is that we can start and stop hundreds of services at the same time, and we need to analyze the dependencies between each service before starting it, so it is not practical to operate manually. Docker-compose was born. Next we’ll look at Docker-compose.
What is thedocker-compose
Compose is a tool for defining and running multi-container Docker applications. With Compose, you can use YML files to configure all the services your application needs. Then, with a single command, all services can be created and started from the YML file configuration.
In simple terms, you define, arrange, and configure your mirroring services according to certain rules in a file called YML. Then use the commands provided by Compose to launch the tool for the entire cluster of services in one click.
The installation
Run the following command to install itdocker-compose
This command will match the appropriate one based on your systemdocker-compose
The curl - L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname - s) - $(uname -m)" - o /usr/local/bin/docker-compose
Copy the code
Grant execute permission to the installed script
chmod +x /usr/local/bin/docker-compose
Copy the code
Checking the Installation Result
docker-compose --version
Copy the code
If the following information is displayed, the installation is successful
Docker-compose version 1.26.2, build eeFE0d31
Copy the code
❝
MacOs and Windows users do not need to install Compose separately, because docker-compose already comes with docker for desktop
❞
docker-compose
The command
Docker-compose, like Docker, has its own set of commands, which are often used in the following sections.
- Docker-compose up ‘: start all containers (defined in docker-compose)
❝
Docker-compose up -d: starts all containers in the background
Docker-compose -f docker-compose template up -d: specifies docker-compose and starts it in the background
❞
-
Docker-compose ps: View the containers running in the project
-
Docker-compose stop: Stops a running container
-
Docker-compose start: starts the stopped container
-
Docker-compose Down: stops and deletes containers, networks, volumes, and images
❝
Docker-compose down-v: deletes the anonymous data volumes that are already defined in the compose file and are attached to the container
❞
docker-compose logs
: Displays logs generated by the service containerdocker-compose restart
: Restarts services in the projectdocker-compose rm
: Deletes all (stopped) service containers
❝
Note: docker-compose stop should be executed first, and the docker-compose stop should be executed after the service is stopped
❞
docker-compose config
: View the project configurationdocker-compose version
: Prints the version information
compose
version
Docker is composed and docker is composed. Docker is composed and docker is composed. Docker is composed and docker is composed. This will be useful in writing the docker-comemage. yml file.
What is Service Orchestration
Compose allows users to define a set of associated application containers as a project using a docker-comemage. yml template file (YAML format), that is, different services are assembled into a project from a template file.
The Compose template file is a YAML file that defines services, networks, and volumes
Docker-compose standard template file should contain version, Services, and Networks
Writing a template file
Docker-compose: docker-compose: docker-compose: docker-compose: Docker-compose: Docker-compose: Docker-compose: Docker-compose The following example is with the help of the geek time column es author Ruan Yiming teacher’s template, I slightly modified. In the next article we will write our own docker-comemage. yml file to orchestrate our own services.
version: '3.8'
services:
cerebro:
image: Lmenezes/cerebro: 0.8.3
container_name: cerebro
ports:
- "9000:9000"
command:
- -Dhosts.0.host=http://elasticsearch:9200
networks:
- es7net
kibana:
image: Docker. Elastic. Co/kibana/kibana: 7.8.0
container_name: kibana7
environment:
- I18N_LOCALE=zh-CN
- XPACK_GRAPH_ENABLED=true
- TIMELION_ENABLED=true
- XPACK_MONITORING_COLLECTION_ENABLED="true"
ports:
- "5601:5601"
networks:
- es7net
elasticsearch:
image: Docker. Elastic. Co/elasticsearch/elasticsearch: 7.8.0
container_name: es7_01
environment:
- cluster.name=triumphxx
- node.name=es7_01
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- discovery.seed_hosts=es7_01,es7_02
- cluster.initial_master_nodes=es7_01,es7_02
ulimits:
memlock:
soft: - 1
hard: - 1
volumes:
- es7data1:/usr/share/elasticsearch/data
ports:
- 9200: 9200
networks:
- es7net
elasticsearch2:
image: Docker. Elastic. Co/elasticsearch/elasticsearch: 7.8.0
container_name: es7_02
environment:
- cluster.name=triumphxx
- node.name=es7_02
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- discovery.seed_hosts=es7_01,es7_02
- cluster.initial_master_nodes=es7_01,es7_02
ulimits:
memlock:
soft: - 1
hard: - 1
volumes:
- es7data2:/usr/share/elasticsearch/data
networks:
- es7net
volumes:
es7data1:
driver: local
es7data2:
driver: local
networks:
es7net:
driver: bridge
Copy the code
❝
The main body of the template file contains: version: docker-compatible version of the Compose file format, services: the service to choreographer volumes: data volume mounting path Settings, networks: network definition.
Under service, you need to specify the name of the service, the image used by the service, the name of the startup container, the mapping port, the command executed after the container is started, and the network to be connected.
The elasticSearch service has a slightly more complex configuration, including cluster name, node name, JVM parameters, ES cluster configuration, and restrictions. We will discuss this in the ES article.
Volumes defines two data volume mounting paths for two ES services
Networks Defines a network with bridge name ES7net as the network mode. The four services that are started are connected to this network
❞
Start the project
Docker-compose. Yml file: docker-compose. Yml file: docker-compose.
❝
During startup, Compose checks to see if there are any local images used in the project. If not, Compose will automatically go to the Docker Hub and pull the images.
❞
summary
Ok, friends, in this paper, we introduced, that is the docker – compose and commonly used commands, and analyze how a compose documents should be written. I hope you found it interesting. In the next article, a SpringCloud service will be choreographed. Welcome to keep watching.