Docker-compose usage
Docker-compose is a Docker container tool, which defines multiple docker container applications through yml files, making it easy to start and stop N multiple containers quickly. Use the docker-compose command to create or manage multiple containers as defined in the yml file, but only for a single host. If you manage containers across hosts, you can use Docker Smarm or popular K8S.
Docker compose installation
Install docker-compose through pip.
# pip install docker-compose# docker-compose-v # View version docker-compose version 1.24.1, build 4667896
If pip install docker-compose takes a long time to install, download it from the official website (for all reasons), you can replace the mirror source.
# pip install-I https://pypi.tuna.tsinghua.edu.cn/simple docker-compose #-I specify the source address of the image
Docker compose implementation example
The company happens to have a small project, named partner, based on spring boot. Need to visit, plus Nginx, plan to do it with docker-compose.
The directory structure is as follows:
[root@docker-qa docker-compose] # drwxr-xr-x 2 root root 4096 Sep 12 16:29 b2blog root root Sep 12 15:28 nginx-1 root root Sep 12 15:52 docker-compose.ymldrwxr-xr-x 3 root root 4096 Sep 12 15:04 logs # where all containers are stored, container application log directory mapping drwxr-xr-x 3 root root 4096 Sep 12 15:28 nginx [root@docker-qa docker-compose] # pwd/data/docker/docker-compose
The Nginx and partner directories are two application containers that store the basic configuration, jar package, and Dockerfile, respectively.
# ll b2b-partner/ Total usage 61672 root root Sep-1 root root 63145378 Sep 12 12:05 b2bMutur partner.jarkay RWMUR-1 root root Sep 12 14:50 Dockerfile# ll nginx/ Total usage 20drwxr-xr-x 2 root root 4096 Sep 12 16:42 conf.d-rw-r--r-- 1 root root 349 Sep 12 12:10 Dockerfile-rw-r--r-- 1 root root 705 Sep 12 11:41 nginx.conf-rw -1 root root 1679 Sep 12 11:26 xmbaby.com.cn.key-rw- 1 root root 3973 Sep 12 11:26 xmbaby.com.cn.pem
Dockerfile of container partner:
# This is b2b-partner# Version: 1.The Author: yuhuanghui# Base imageFROM registry.cn-hangzhou.aliyuncs.com/xmbaby-pre/ms-jdk8# MaintainerMAINTAINER b2b-partner-qa yuhuanghui@alaxiaoyou.com# Set timezomeENV TZ=Asia/ShanghaiRUN ln-snf / usr/share/zoneinfo/$TZ / etc/localtime & & echo $TZ > / etc/timezone#EXPOSE 8081ADD b2b-partner.jar / data/httpd/WORKDIR / data/httpd/ENTRYPOINT java-Xmx256m-Xss512k-jar b2b-partner.jar
Dockerfile of container Nginx:
# This is b2b-nginx# Version: 1. Access Author: yuhuanghui# Base imageFROM nginx:1.12# MaintainerMAINTAINER b2b-nginx yuhuanghui@alaxiaoyou.com# Set timezomeENV TZ=Asia/ShanghaiRUN ln-snf / usr/share/zoneinfo/$TZ / etc/localtime & & echo $TZ > / etc/timezoneRUN mkdir-p / data/certs/COPY. / xmbaby.com.cn.* / data/certs/ # https access, SSL certificate is required
Docker-compose.yml services writing rules, which mainly include version, services and network
The key docker-compose.yml is as follows:
Version: '2.2'services:#db:# image:mysql:5.7 # image if there is no image locally, the corresponding image nginx:build:. / nginx# build will be pulled in the public image warehouse based on the specified Dockerfile path, which is used by compose to build container_name: b2b-nginx#depends_on: # depends_on container relies on #-b2b-partner#links: # links to link to containers in other servers Using the alias alias creates a #-db:mysql5.7 #-b2b-partner:b2b-partnervolumes: # volumes mount directory (Host:Container) in the container's / etc/hosts file Ro means read-only -. / nginx/nginx.conf:/etc/nginx/nginx.conf:ro -. / nginx/conf.d:/etc/nginx/conf.d:ro -. / logs/nginx:/var/log/nginx/ports: # ports mapped port (Host:Container)-"80:80"-"443 ro" restart: alwayscommand: nginx-g 'daemon off '# command overrides the commands that are executed by default after the container is started. B2b-partner:build:. / b2b-partnercontainer_name: b2b-partnerports:-"8091 restart: always
There is also a network network model in docker-compose.yml, which includes:
Network_mode: "bridge" network_mode: "host" network_mode: "none" network_mode: "service: [service name]" network_mode: "container: [container name/id]"
Network is not specified in docker-compose.yml here, but "Creating network" docker-compose_default "with the default driver" is displayed when the docker-compose up-d command starts, and a default network is created.
The main configuration of server virtual host for nginx configuring parner is as follows:
Server {listen 443 / serverroomname aaa;index index.html index.htm index.php;ssl on;ssl_certificate / data/certs/aaa;ssl_certificate_key / data/certs/aaa;ssl_session_timeout 5m accessorial log / var/log/nginx/aaa-ssl.access.log main;error_log / var/log/nginx/aaa-ssl.error.log;location / {proxy_next_upstream http_502 http_504 error timeout invalid_header Proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; proxy_buffer_size 64k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_max_temp_file_size 512m; proxy_connect_timeout 1800 Proxy_read_timeout 1800; proxy_send_timeout 1800; # proxy_pass http://aaa:8091; proxy_pass http://172.17.0.1:8091;}}
The reverse proxy configuration above starts with "not found in # proxy_pass http://b2b-partner:8091;", followed by docker0 virtual bridge IP (gateway to other docker containers), that is, proxy_pass http://172.17.0.1:8091;" test passed."
The next day, it was suddenly enlightened, and the test found that the reverse proxy proxy_pass port was not a mapped port, but a port in the container, which was replaced by "proxy_pass http://b2b-partner:8081;"" and passed the test. The format of the Nginx container reverse proxy is: "proxy_pass http:// container name: container Port;".
Run Docker compose and docker-compose common commands
Common commands:
Docker-compose up-d # build starts the container in docker-compose.yml and runs in the background
Docker-compose down # Delete all containers
Docker-compose config-Q # verifies that the docker-compose.yml file is configured correctly and does not output anything
Docker-compose ps # shows all containers
Docker-compose build nginx # build nginx image
Docker-compose rm nginx # Delete nginx container
Docker-compose start nginx # start the nginx container
Docker-compose stop nginx # stop nginx Container
Docker-compose logs-f nginx # View nginx container real-time log
Start docker-compose according to the above command. Entering docker-compose up-d for the first time will mirror the build file. You need to enter the docker-compose directory to perform up, down commands and other operations.
[root@docker-qa docker-compose] # docker-compose up-dCreating network "docker-compose_default" with the default driverCreating b2b-partner... DoneCreating b2b-nginx... Done [root@docker-qa docker-compose] # docker-compose downStopping b2b-nginx... DoneStopping b2b-partner... DoneRemoving b2b-nginx... DoneRemoving b2b-partner... DoneRemoving network docker-compose_ default [root @ docker-qa docker-compose] # docker-compose psName Command State Ports #-- -- b2b-nginx nginx-g daemon off Up 0.0.0.0 java 443-> 443/tcp, 0.0.0.0 80/tcpb2b-partner 80-> bin/sh-c java-Xmx256m -. Up 0.0.0.0 8091-> 8081/tcp test run
Enter the domain name configured by Nginx, https://partner-test.xmbaby.com.cn/app/verification/send?telephone=13505182554
The test passed. If there are other container applications later, you can configure the corresponding server for the corresponding service,Nginx configuration under docker-compose.yml.