Docker-Docker Compose orchestration (theory + practical operation)

1. Introduction to Docker Compose

  • Docker Compose is great forCombine multiple containers for developmentScene
  • The predecessor of Docker Compose is Fig, which is a tool for defining and running multiple containers
  • Use Docker ComposeNo need to use shell scripts anymoreTo start the container
  • Available through Docker ComposeYML fileTo configure all the services required by the application

What is a YML file

YAML is a very intuitive data serialization format with a markup language. It is very suitable for expressing or editing data structures, various configuration files, file outlines, etc. For example, many email header formats are very similar to YAML

File format and writing notes:

  • Case Sensitive
  • Use indentation to indicate hierarchical relationships
  • Tab key is not allowed when indenting, only spaces are allowed to indent, and indentation is used to indicate hierarchical relationships
  • Usually the beginning is indented by 2 spaces, the number of spaces indented is not important, as long as the elements of the same level are aligned to the left
  • Indent a space after the character, such as colon, comma, bar
  • Comment with # sign
  • Use single quotes if it contains special characters
  • Boolean values ​​must be enclosed in quotes

Second, use Docker Compose to explain in detail

Steps for usage

  • Use Dockerfile to define the environment of the application
  • Use docker-compose.yml to define the services that make up the application so that they can run together in an isolated environment
  • Finally execute the docker-compose up command to start and run the entire application

Environmental preparation

curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose	
'##在Linux上我们可以从GitHub上下载它的二进制包来使用,此命令是下载Docker Compose的当前稳定版本'
chmod +x /usr/local/bin/docker-compose
docker-compose -v

Insert picture description here

Detailed format

Example of Docker Compose file structure: docker-compose.yml

  • Compose version number and service identifier must be written in the top box
  • The attribute name and attribute value are separated by: (colon plus space)
  • Use two spaces to indicate the level
  • Service attributes are represented by-(space space-space)
version: '2'	 '##compose版本号'
services:	    '##服务标识符'
  web:	     '##子服务名'
    image: dockercloud/hello-world	 '##服务依赖镜像属性'
    ports:	  '##服务端口属性'
      - 8080
    networks:	  '##网络服务属性'
      - front-tier
      - back-tier
  redis:
    image: redis
    links:	  '##容器间的连接设置'
      - web
    networks:
      - back-tier
  lb:
    image: dockercloud/haproxy
    ports:
      - 80:80
    links:
      - web
    networks:
      - front-tier
      - back-tier
    volumes:    	'##挂载一个目录或者一个已存在的数据卷容器'
      - /var/run/docker.sock:/var/run/docker.sock 
networks:
  front-tier:
    driver: bridge
  back-tier:
    driver: bridge

Three, Docker Compose configuration common fields

Common fields have some explanations in the above yml file format, the following is a detailed explanation

Field description
build dockerfile context Specify the Dockerfile file name to build the mirror context path
image Specified mirror
command Execute the command, overwrite the default command
container name Specify the container name, because the container name is unique, if you specify a custom name, you cannot scale
deploy Specify the deployment and operation service related configuration, which can only be applied in swarm mode
environment Add environment variables
networks Join the network
ports Expose the container port, the same as -p, but the port cannot be lower than 60
volumes Mount the host path or command volume
restart Restart strategy, default no,always,no-failure,unless-stoped
hostname Container hostname

Docker Compose commonly used commands

基本的命令格式:docker-compose [选项] [命令] [参数]

docker-compose options:

  • --Verbose: output more debugging information
  • --Version: print the version and exit
  • -f, –file FILE: use a specific compose final file, the default is docker-compose.yml
  • -p, -project-name NAME: specify the project name, the default directory name is used
Field Explanation
build Rebuild the service
ps List containers
up Create and start the container在这里插入代码片
exec Execute commands in the container
scale Specify the number of service containers to start
top Show container process
logs View container output
down Delete containers, networks, data volumes and mirrors
stop/start/restart Stop/start/restart service

Four, simulation experiment

Deploy all hosts to install the docker environment

yum install -y docker-ce tree

Create a Docker Compose directory, which needs to be configured:

  • The yml file required by Docker Compose
  • The Dockerfile file that needs to be arranged (here nginx is taken as an example)
  • nginx web document
    Insert picture description here
mkdir /root/compose_nginx
cd /root/compose_nginx
tree ./
./
├── docker-compose.yml         #创建模板脚本
├── nginx                      
│   ├── Dockerfile             #创建容器脚本
│   ├── nginx-1.12.0.tar.gz    #源码包
│   └── run.sh                 #运行脚本
└── wwwroot                    #站点
    └── index.html             #网页文档

Insert picture description here
Configure yml file

vim /root/compose_nginx/docker-compose.yml

version: '3'
services:
  nginx:
    hostname: nginx
	bui1d:
      context: ./nginx
      dockerfile: Dockerfile
	ports:
     - 1216:80
	 - 1217:443
	networks:
	 - cluster
	volumes:
     - ./wwwroot:/usr/local/nginx/html
networks:
  cluster:

Execute the docker-compose up command to start and run the entire application

docker-compose -f docker-compose.yml up -d

Insert picture description here

test
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51613313/article/details/115286786