11.1 docker compose Introduction
Based on the knowledge learned earlier shows, Docker want to use to deploy the application, we must first write Dockerfile in the application file to build the image. Similarly, in the micro-services project, we also need to write Dockerfile file for each service to build the image. After the build is complete, you can use a mirror according to each docker run or docker service to create a create command and launch container, so that we can access the service container. Micro-service architecture: the number of services related to the huge number. Although the above manner can deploy micro-services projects, but considering the micro-services project may have multiple sub-service composition, and each service startup process will need to configure additional parameters (such as -e configuration environment variable, - network specified network network , mount the disk and so on). In this case, after each micro-updating service, to be run manually command to restart the vessel, which appeared to be quite troublesome. For the case of such multi-service deployments, Docker provides Docker Compose layout tools for unified deployment of multi-service applications. Compose is Docker service orchestration work tools, primarily for complex applications built on Docker, Compose managed by a plurality of profile Docker containers, it is very suitable in combination of a plurality of scenes vessel development. With this arrangement tool, you can use yml (or yaml) file to configure the application services, and then only need a simple single service deployment instructions can be created from the configuration and start all services.
11.2 docker compose the installation and uninstallation
Installation docker compose
1. Environmental requirements: Docker Compose Docker is dependent on the engine, so before installing Docker Compose
make sure that the machine has been installed the Docker. https://github.com/docker/compose/releases
(see docker compose version)
2, download docker-compose tool
curl -L
https://github.com/docker/compose/releases/download/1.24.0/docker -
Compose-`uname -s` -o-`uname -m` / usr / local / bin / Docker-Compose
. 3, the executable file permissions set docker compose
the chmod + X / usr / local / bin / Compose Docker-
R & lt: W Read: Write X: EX
. 4, see docker compose version
Docker---version Compose
the PS: unloading docker compose
the sudo RM / usr / local / bin / Docker-Compose
11.3 docker compose use
11.3.1 examples
Step: three steps (executed in an empty directory created)
1, write Dockerfile file (for each service needs to build a mirror, easy migration - is not necessary)
2, write docker-compose.yml file (written deploy services related instructions)
3, run docker-compose up (start yml file service)
case:
1, to prepare: two mirrors (this demonstration is not constructed by Dockerfile a)
Docker pull MySQL: 5.7
Docker pull WordPress
2, need to create a blank directory, for example rwtest. Create a docker-compose.yml, edit the file:
Version: '. 3'
Services:
DB:
Image: MySQL: 5.7
Volumes:
- DB_DATA: / var / lib / MySQL
the restart: Always
Environment:
MYSQL_ROOT_PASSWORD: WordPress
MYSQL_DATABASE: WordPress
mysql_user: WordPress
MYSQL_PASSWORD: WordPress
WordPress:
depends_on:
- db
Image: wordpress: Latest
the ports:
- "8001: 80"
restart: Always
Environment:
WORDPRESS_DB_HOST: db: 3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
Volumes:
DB_DATA: the file contents: New wordpress db and container. Is equivalent to: Docker RUN --name 123456 DB = -d -e MYSQL_ROOT_PASSWORD MySQL Docker RUN --name some WordPress --link-DB: MySQL -p 8002: 80 -d WordPress . 3, Compose start Docker Docker Compose-up . 4, browser access: HTTP: // ip: 8001 5, stop / restart services: docker-compose stop / restart
11.3.2 docker compose Instructions
1 version: '3'
2 services:
3 web:
4 image: id/imagename:lable
5 restart: on‐failure
6 container_name: my‐web‐container
7 ports:
8 ‐ 8080:8080
9 networks:
10 ‐ example‐net
11 depends_on:
12 ‐ db
13 deploy:
14 replicas: 2
15 restart_policy:
16 condition: on‐failure
17 db:
18 image: mysql:5.6
19 restart: on‐failure
20 container_name: my‐mysql‐container
21 ports:
22 ‐ 3306:3306
23 volumes:
24 ‐ example‐mysql:/var/lib/mysql
25 networks:
26 ‐ example‐net
27 environment:
28 MYSQL_ROOT_PASSWORD: root
29 MYSQL_DATABASE: mysql_database
30 deploy:
31 replicas: 1
32 restart_policy:
33 condition: on‐failure
34 placement:
35 constraints: [node.role == manager]
36 networks:
37 example‐net:
38 volumes:
39 example‐mysql:
-
version: version usually at the very top of a docker-compose.yml file, the paper used to represent constraint version contents (XML document constraint like) (higher version, support for more instruction)
-
services used to declare service, all applications with indented under the name of both services on behalf of a service, as in the example above web and db. When performing multi-service deployments, how many services need to be deployed, you need to configure the number of services and services parameters in the following statement
-
image: Start containers need to rely on the mirror (if not automatically pull local)
-
restart: restart the service strategy
-
restart: "no" # service default is no, that is, without any action after the service fails
-
restart: always # indicates that the service would have been restarted
-
restart: on-failure # represents a service failure error when prompted to restart
-
restart: unless-stopped # means that only those services will be restarted after stopping
-
-
container_name: the name of the specified container
-
ports: Specifies the service exposed outward port
-
networks: network using the specified container
-
depends_on: service depends on the decision of the dependency services, as in the example of the web rely db, so the db service will first start to web services, but it does not mean db service started successfully complete before starting the web service works, it only decided to start order only
-
deploy: deploy Docker Compose parameter is provided for the Swarm cluster deployment, sub-parameters specifically for deploying and running with a service-related configuration
-
replicas: indicates the number of copies of a service instance
-
restart_policy: estart_policy restart with parameters similar to previously described, are used to configure the service to restart strategy, but this property in the deploy configuration parameters, and only in a clustered environment swells effect. The parameters include a plurality of sub-attributes and attribute values
-
condition: on-failure # represents a service restart conditions, values are none, on-failure and any
-
delay: the wait time between 5s # represents restart the service, the default is 0
-
max_attempts: 3 # represents the number of attempts to restart after failure
-
window: 120s # indicate how long to wait to determine whether the service starts successfully
-
-
placement: placement to configure constraints specified location, while serving in Swarm cluster ring when deployed under the border will be randomly assigned to work on the management node and other nodes. In the above example since the mount to the native data mysql example-mysql data volume, the use of a placement subparameters of constraints: [node.role == manager] specifies the service manager only pipe running management node:
-
-
environment for the necessary environment variables when configuring the service starts. As the above example MYSQL_ROOT_PASSWORD represents the root password database, MYSQL_DATABASE table database is automatically created after starting the database shown.
-
-
networks: for network configuration services
-
volumes: directory is mounted, the above example is to mount the mysql data to a local example-mysql data volumes , if the data volume does not exist, will be created by default when the service starts
Other configurations may refer to: https://docs.docker.com/compose/compose-file/