Docker-Compose

一、单机版容器编排

docker-compose文件名称必须为docker-compose,否则不能被识别。
version: "数字" #指定Docker-Compose版本
services:

服务名称1 #指定服务名称
image: 镜像名称:版本 #指定镜像名称:版本
restart: always #指定Docker重启自动拉起容器
environment: #声明环境指定环境变量
变量名1: 变量
变量名2: 变量
变量名3: 变量
......
command: 初始命令 #指定初始命令
服务名称2
depends_on: #指定依赖服务
- 服务名称1
- 服务名称2
image: 镜像名称:版本
restart: always
environment:
变量名: 变量
......
ports: #指定端口映射
- "宿主机端口:容器端口"
- "宿主机端口:容器端口"
command: 初始命令

......

# 多容器运行Zabbix实例
version: '3'
services:
  mysql-server:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: zabbix
      MYSQL_USER: zabbix
      MYSQL_PASSWORD: zabbix_pwd
      MYSQL_ROOT_PASSWORD: root_pwd
    command: --character-set-server=utf8 --collation-server=utf8_bin
  zabbix-java-gateway:
    image: zabbix/zabbix-java-gateway:latest
    restart: always
  zabbix-server:
    depends_on:
      - mysql-server
      - zabbix-java-gateway
    image: zabbix/zabbix-server-mysql:latest
    restart: always
    environment:
      DB_SERVER_HOST: mysql-server
      MYSQL_DATABASE: zabbix
      MYSQL_USER: zabbix
      MYSQL_PASSWORD: zabbix_pwd
      MYSQL_ROOT_PASSWORD: root_pwd
      ZBX_JAVAGATEWAY: zabbix-java-gateway
    ports:
      - "10051:10051"
  zabbix-web-nginx-mysql:
    depends_on:
      - zabbix-server
      - mysql-server
    image: zabbix/zabbix-web-nginx-mysql:latest
    restart: always
    environment:
      DB_SERVER_HOST: mysql-server
      MYSQL_DATABASE: zabbix
      MYSQL_USER: zabbix
      MYSQL_PASSWORD: zabbix_pwd
      MYSQL_ROOT_PASSWORD: root_pwd
    ports:
      - "80:80" 

step 1 安装 docker-compose

docker-compose依赖EPEL源

yum install -y docker-compose
[root@docker01 ~]# yum install -y docker-compose
......
Complete!

step 2 docker-compose文件编撰

docker-comppose本身可实现容器间解析

[root@docker01 ~]# mkdir -p /opt/docker-compose/zabbix
[root@docker01 ~]# cd /opt/docker-compose/zabbix/
[root@docker01 /opt/docker-compose/zabbix]# ls
docker-compose.yaml

step 3 使用docker-compose

docker-compose up

-d #后台启动
[root@docker01 /opt/docker-compose/zabbix]# docker-compose up -d
Creating zabbix_mysql-server_1           ... done
Creating zabbix_zabbix-java-gateway_1    ... done
Creating zabbix_mysql-server_1           ... 
Creating zabbix_zabbix-server-mysql_1    ... done
Creating zabbix_zabbix-server-mysql_1    ... 
Creating zabbix_zabbix-web-nginx-mysql_1 ... done
628a29d353        zabbix/zabbix-web-nginx-mysql:latest   "/bin/bash /run_zabb…"   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp, 443/tcp         zabbix_zabbix-web-nginx-mysql_1
0a28da08d156        zabbix/zabbix-server-mysql:latest      "/bin/bash /run_zabb…"   2 minutes ago       Up 2 minutes        162/udp, 0.0.0.0:10051->10051/tcp   zabbix_zabbix-server-mysql_1
176b61749b85        mysql:5.7                              "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        3306/tcp                            zabbix_mysql-server_1
e526046a645d        zabbix/zabbix-java-gateway:latest      "/bin/bash /run_zabb…"   2 minutes ago       Up 2 minutes        10052/tcp                           zabbix_zabbix-java-gateway_1

停用docker-compose

docker-compose down
[root@docker01 /opt/docker-compose/zabbix]# docker-compose down
Stopping zabbix_zabbix-web-nginx-mysql_1 ... done
Stopping zabbix_zabbix-server-mysql_1    ... done
Stopping zabbix_mysql-server_1           ... done
Stopping zabbix_zabbix-java-gateway_1    ... done
Removing zabbix_zabbix-web-nginx-mysql_1 ... done
Removing zabbix_zabbix-server-mysql_1    ... done
Removing zabbix_mysql-server_1           ... done
Removing zabbix_zabbix-java-gateway_1    ... done
Removing network zabbix_default