06走进云计算-Docker compose单机编排工具

docker-compose介绍

Compose是用于定义和运行多容器 Docker应用工具
通过Compose 可以使用YML文件来配置应用程序所需要的所有服务

Docker Compose使用的三步:

1.使用docker file 定义应用程序的环境
2.docker-compose定义构成应用程序的服务
3.启动Compose,就相当于启动应用

Docker Compose官方版本说明:

https://docs.docker.com/compose/compose-file/compose-versioning

docker-compose安装部署

## yum安装docker Compose
yum install -y docker-compose(需要epel源)

## 官方下载方式
curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

## 加执行权限
[root@db02 bin]# chmod +x /usr/local/bin/docker-compose

## 查看版本
[root@db02 ~]# docker-compose version

Docker-compose语法

YAML语法

1.冒号后面加空格, 冒号结尾不加空格
2.列表数据用-
3.下一级缩进,两个空格

version: '版本号'
services:
  服务名称1:
    image: 容器镜像
    container_name: 容器名称
    environment:
      - 环境变量1=值1
      - 环境变量2=值2
    volumes:
       - 宿主机数据目录:容器内数据目录
    ports:
      - 宿主机端口:容器内映射端口
    networks:
      - 自定义网络名称
    links:
      - namenode 
    command:
      - 数据库使用字符集变量时可以用
    restart: always

  服务名称2:
    image: 容器镜像
    container_name: 容器名称
    environment:
      - 环境变量1=值1
      - 环境变量2=值2
    user: 宿主机用户:容器用户
    volumes:
      - 宿主机数据目录:容器内数据目录
    ports:
      - 宿主机端口:容器内映射端口
    networks:
      - 自定义网络名称
    links:
      - namenode 
    depends_on:
      - 依赖服务
    restart: always

networks:
  default:
  externnal: true
  name: 自定义网络名称

手动启动zabbix

# 1.启动MySQL
[root@db02 ~]# mkdir -p /data/mysql/data

docker run \
--name mysql57 \
-p 3306:3306 \
-v /data/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123 \
-e MYSQL_DATABASE=zabbix \
-e MYSQL_USER=zabbix \
-e MYSQL_PASSWORD=zabbix \
-d mysql:5.7 \
--character-set-server=utf8 \
--collation-server=utf8_bin

docker run \
--name zabbix-server \
-p 10051:10051 \
--link mysql57 \
-e DB_SERVER_HOST="mysql57" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-d zabbix/zabbix-server-mysql

docker run \
--name zabbix-web \
--link zabbix-server \
--link mysql57 \
-p 80:8080 \
-e DB_SERVER_HOST="mysql57" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e ZBX_SERVER_HOST="zabbix-server" \
-e PHP_TZ="Asia/Shanghai" \
-d zabbix/zabbix-web-nginx-mysql

docker compose启动zabbix

[root@db02 zabbix-server]# cat docker-compose.yml
version: '3'
services:
  mysql5.7:
    image: mysql:5.7
    container_name: mysql5.7
    environment:
      - MYSQL_ROOT_PASSWORD=123
      - MYSQL_DATABASE=zabbix
      - MYSQL_USER=zabbix
      - MYSQL_PASSWORD=zabbix
    volumes:
      - /data/mysql/data:/var/lib/mysql
    user: 1000:999
    ports:
      - 3306:3306
    command:
      --character-set-server=utf8
      --collation-server=utf8_bin
    restart: always

  zabbix-server:
    image: zabbix/zabbix-server-mysql
    container_name: zabbix-server
    environment:
      - DB_SERVER_HOST=mysql5.7
      - MYSQL_USER=zabbix
      - MYSQL_PASSWORD=zabbix
    ports:
      - 10051:10051
    links:
      - mysql5.7
    depends_on:
      - mysql5.7
    restart: always

  zabbix-web:
    image: zabbix/zabbix-web-nginx-mysql
    container_name: zabbix-web
    environment:
      - DB_SERVER_HOST=mysql5.7
      - MYSQL_USER=zabbix
      - MYSQL_PASSWORD=zabbix
      - ZBX_SERVER_HOST=zabbix-server
      - PHP_TZ=Asia/Shanghai
    ports:
      - 80:8080
    links:
      - mysql5.7
      - zabbix-server
    depends_on:
      - mysql5.7
      - zabbix-server
    restart: always

官方MySQL compose

version: '3.1'

services:

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 123

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

官方es compose

version: '2.2'
services:
  es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.5.2
    container_name: es01
    environment:
      - node.name=es01
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es02
      - cluster.initial_master_nodes=es01,es02
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - elastic
  es02:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.5.2
    container_name: es02
    environment:
      - node.name=es02
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es01
      - cluster.initial_master_nodes=es01,es02
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data02:/usr/share/elasticsearch/data
    networks:
      - elastic

volumes:
  data01:
    driver: local
  data02:
    driver: local

networks:
  elastic:
    driver: bridge

docker-compose常用命令

## 第一次启动
docker-compose up

## 放后台启动
docker-compose up -d

## 指定文件启动
docker-compose -f 文件名.yml up -d

## 查看进程
docker-compose ps

## 查看日志
docker-compose logs

## 查看单个服务日志
docker-compose logs container_name

## 停止
docker-compose stop

## 停止单个服务
docker-compose stop container_name

## 启动单个服务
docker-compose start container_name

## 重启
docker-compose restart

## 单个服务
docker-compose restart container_name

山林不向四季起誓 荣枯随缘