Dcoker compose stand-alone container cluster orchestration management

Table of contents

I. Overview

 2. Compose deploys lnmp

1. Docker Compose environment installation

2.YAML file format and writing considerations

 3.Docker Compose configures common fields

4.Docker Compose common commands

5. Configure lnmp cluster dependency files

6. Modify the docker-compose.yml file

7. Create lnmp container based on yml file


I. Overview

  • The Docker compose project is the official open source project of docker. It is responsible for implementing the docker compose project. It is written in Python and calls the API provided by the docker service to manage the container. Therefore, as long as the platform you are operating on supports the Docker API, you can use compose for orchestration management.
  • Docker compose is a tool for defining and running multi-container Docker applications. It allows users to use YAML files to define the configuration of an application, including the number of containers, dependencies between containers, environment variables, port mappings, and other settings. Users can then use the docker-compose command to start and manage these containers.
  • You can use Docker compose to easily manage multiple containers. For example, you can start a web server container and a database container at the same time, and establish a network connection between them. Docker compose also supports custom networks, allowing containers to communicate across different networks.
  • Docker compose files typically contain one or more services, each of which is composed of one or more containers. Services define the tasks that containers should perform, as well as the dependencies between containers. For example: a Web server service can contain one or more Web server containers, and these containers depend on a database container.
  • Docker compose divides the managed containers into three layers, namely project, service and container. All files in the docker-compose running directory form a project. If there is no special project name, it is the current directory name. A project can contain multiple services, and each service contains a name, image, port mapping, environment variables, mount point and other information.
  • The default project configuration file of Docker-compose is docker-compose.yml. The configuration file can be customized through the environment variable COMPOSE_FILE or the -f parameter, which defines multiple dependent services and the containers in which each service runs.
  • Compose allows users to define a set of associated application containers as a project through a separate docker-compose.yml template file (YAML format).

Summary: docker compose uses a template file to define the startup parameters and dependencies of multiple application containers, and uses docker compose to start the containers according to the configuration of the template file. 

 2. Compose deploys lnmp

1. Docker Compose environment installation

Docker Compose is an independent product of Docker, so you need to install Docker and then install Docker Compose separately.

#下载
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#安装
chmod +x /usr/local/bin/docker-compose
#查看版本
docker-compose --version

2.YAML file format and writing considerations

YAML is a markup language that can display data serialization format very intuitively and is highly readable. Similar to json data description language, the syntax is much simpler than json. YAML data structures are represented by indentation, consecutive items are represented by minus signs, key-value pairs are separated by colons, arrays are enclosed in square brackets [], and hashes are enclosed in curly braces {}.

You need to pay attention to the following things when using YAML:

  • Case Sensitive
  • Represent hierarchical relationships through indentation
  • Tab key indentation is not supported, only spaces can be used for indentation.
  • The number of indented spaces does not matter, as long as the same level is left aligned, usually 2 spaces are indented at the beginning.
  • Comment with #
  • Indent 1 space after the symbol character, such as colon: , comma, , horizontal bar- 
  • If it contains special characters enclosed in single quotation marks (''), it will be treated as an ordinary string. Double quotation marks (""): the special characters are used as the meaning they want to express.

 3.Docker Compose configures common fields

字段                               		描述
build                              		指定 Dockerfile 文件名,
										要指定Dockerfile文件需要在build标签的子级标签中使用dockerfile标签指定
dockerfile                         		构建镜像上下文路径
context                            		可以是 dockerfile 的路径,或者是指向 git 仓库的 url 地址
image                              		指定镜像
command                            		执行命令,覆盖容器启动后默认执行的命令
container_name                     		指定容器名称,由于容器名称是唯一的,如果指定自定义名称,则无法scale指定容器数量
deploy                             		指定部署和运行服务相关配置,只能在 Swarm 模式使用
environment                        		添加环境变量
networks                           		加入网络,引用顶级networks下条目
network_mode							设置容器的网络模式,如 host,bridge,...
ports                              		暴露容器端口,与 -p 相同,但端口不能低于 60
volumes                            		挂载一个宿主机目录或命令卷到容器,命名卷要在顶级 volumes 定义卷名称
volumes_from							从另一个服务或容器挂载卷,可选参数 :ro 和 :rw,仅版本 '2' 支持
hostname                           		容器主机名
sysctls									在容器内设置内核参数
links									连接到另外一个容器,- 服务名称[:服务别名]
privileged								用来给容器root权限,注意是不安全的,true | false
restart                            		设置重启策略,no,always,no-failure,unless-stopped

重启策略:
no,默认策略,在容器退出时不重启容器。
on-failure,在容器非正常退出时(退出状态非0),才会重启容器。
on-failure:3,在容器非正常退出时重启容器,最多重启3次。
always,在容器退出时总是重启容器。
unless-stopped,在容器退出时总是重启容器,但是不考虑在 Docker 守护进程启动时就已经停止了的容器。

4.Docker Compose common commands

字段                    					    描述
build                   					重新构建服务
ps                      					列出容器
up                      					创建和启动容器	
exec                    					在容器里面执行命令
scale                   					指定一个服务容器启动数量
top                     					显示容器进程
logs                    					查看容器输出
down                    					删除容器、网络、数据卷和镜像
stop/start/restart      					停止/启动/重启服务

5. Configure lnmp cluster dependency files

mkdir -p /opt/lnmp/{nginx,mysql,php}
ls /opt/lnmp/nginx
Dockerfile  nginx-1.24.0.tar.gz  nginx.conf  www

ls /opt/lnmp/mysql
Dockerfile  my.cnf  mysql-boost-5.7.20.tar.gz

ls /opt/lnmp/php
Dockerfile          php-fpm.conf  www.conf
php-7.1.10.tar.bz2  php.ini       

6. Modify the docker-compose.yml file

version: '3'

services:
  nginx:                                #指定服务名称
   build:                               #创建容器
     context: ./nginx                   #指定dockerfile文件路径:在当前目录下的nginx目录中
     dockerfile: Dockerfile             #文件名为dockerfile
   container_name: nginx                #指定容器名称
   ports:                               #建立端口映射
   - 80:80
   volumes:                             #指定挂载目录
   - ./nginx/www:/usr/local/nginx/html  #宿主机目录共享给容器内目录
   networks:                            #指定容器中的网络模式
     lnmp:                              #指定网络模式为lnmp
       ipv4_address: 172.18.0.10        #指定容器的ip地址  
  
  mysql:
   build:
     context: ./mysql
     dockerfile: Dockerfile
   ports:
   - 3306:3306
   volumes:
   - db-data:/usr/local/mysql           #共享数据卷,数据卷名为db-data
   privileged: true                     #赋予容器root权限
   networks:
     lnmp:
       ipv4_address: 172.18.0.20

  php:
   build:
     context: ./php
     dockerfile: Dockerfile
   container_name: php
   ports:
   - 9000:9000
   volumes:
   - db-data:/usr/local/mysql           #挂载mysql容器中的数据卷
   - ./nginx/www:/usr/local/nginx/html
   depends_on:                          #连接nginx,mysql容器
   - nginx
   - mysql
   networks:
     lnmp:
       ipv4_address: 172.18.0.30

networks:                               #顶级networks
  lnmp:                                 #自定义网络模式名为lnmp
   driver: bridge                       #网桥模式
   ipam:
     config:
     - subnet: 172.18.0.0/16            #自定义网络的ip地址

volumes:                                #顶级volumes
  db-data:                              #指定容器中共享数据卷名称

7. Create lnmp container based on yml file

cd /opt/lnmp
docker-compose -f docker-compose.yml up -d

-f, --file FILE :使用特定的 compose 模板文件,默认为 docker-compose.yml
-p, --project-name NAME :指定项目名称,默认使用目录名称
-d :在后台运行

#必须在docker-compose.yml文件目录下执行
docker-compose down      #删除容器、网络、数据卷和镜像
docker-composw up -d     #启动容器
docker-compose ps        #列出容器

Guess you like

Origin blog.csdn.net/q1y2y3/article/details/131910414