同一个docker镜像,如何拉起多个docker container?


环境: centos7,zabbix3.0

关键步骤:

1. 安装docker
yum install epel-release
yum install docker

2. 安装docker-compose
sudo yum install -y python-pip
sudo pip install docker-compose

3. 拉取docker镜像
docker pull million12/zabbix-agent
注:docker镜像来自 https://hub.docker.com/r/million12/zabbix-agent/

4. 关键配置文件
2个关键配置文件,一个是zabbix_agentd.conf文件(参考:https://hub.docker.com/r/million12/zabbix-agent/),一个是docker-compose.yml

业务配置文件:

LogFile=/tmp/zabbix_agentd.log
Server=10.1.1.1
ListenPort=10050
ServerActive=10.1.1.1:10051
AllowRoot=1

docker-compose配置文件:
1个container时:

version: '1'
services:
  zabbix-agent000:      
    image: million12/zabbix-agent
    container_name: zabbix-agent
    restart: always
    network_mode: "bridge"
    ports:
      - "8000:10050"   
    volumes:
      - ./zabbix-agentd.conf:/etc/zabbix-agent/zabbix_agentd.conf
      - /proc:/data/proc
      - /sys:/data/sys
      - /dev:/data/dev
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - ZABBIX_SERVER=10.1.1.1
      - CONFIG_FILE=/etc/zabbix-agent/zabbix_agentd.conf
      - HOST_METADATA=vir_sysquery
      - HOSTNAME=vir

2个container时:

version: '1'
services:
  zabbix-agent000:      
    image: million12/zabbix-agent
    container_name: zabbix-agent000
    restart: always
    network_mode: "bridge"
    ports:
      - "8000:10050"   
    volumes:
      - ./zabbix-agentd.conf:/etc/zabbix-agent/zabbix_agentd.conf:ro
      - /proc:/data/proc
      - /sys:/data/sys
      - /dev:/data/dev
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - ZABBIX_SERVER=10.1.1.1
      - CONFIG_FILE=/etc/zabbix-agent/zabbix_agentd.conf
      - HOST_METADATA=vir000_sysquery
      - HOSTNAME=vir000


  zabbix-agent001:     
    image: million12/zabbix-agent
    container_name: zabbix-agent001
    restart: always
    network_mode: "bridge"
    ports:
      - "8001:10050"   
    volumes:
      - ./zabbix-agentd.conf:/etc/zabbix-agent/zabbix_agentd.conf:ro
      - /proc:/data/proc
      - /sys:/data/sys
      - /dev:/data/dev
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - ZABBIX_SERVER=10.1.1.1
      - CONFIG_FILE=/etc/zabbix-agent/zabbix_agentd.conf
      - HOST_METADATA=vir001_sysquery
      - HOSTNAME=vir001

5. 执行命令
在docker-compose.yml 文件所在的目录执行

docker-compose up -d

如果要查看启动详情,使用 docker-compose up 命令

至此,在docker所在的服务器应该能够看到有2个docker镜像启动。zabbix_server上也能看到有2个zabix_agent连接成功

FAQ:
问题:
起大批量的docker时,可能会出现以下问题:
ERROR: for zabbix-agent00103 UnixHTTPConnectionPool(host=’localhost’, port=None): Read timed out. (read timeout=60)
解决方法:
在起docker镜像的服务器上执行:
export COMPOSE_HTTP_TIMEOUT=3600


参考:
https://blog.smoker.cc/docker/zabbix-in-docker.html
https://hub.docker.com/r/million12/zabbix-agent/

你可能感兴趣的:(运维,docker)