docker-compose配置zabbix环境
docker-compose文件
version: "3"
services:
zabbix-mysql:
image: mysql:5.7
container_name: zabbix-mysql
ports:
- '3306'
environment:
- MYSQL_ROOT_PASSWORD="123456"
volumes:
- ./mysql_data:/var/lib/mysql
zabbix-web-apache-mysql:
image: zabbix/zabbix-web-apache-mysql
container_name: zabbix-web-apache-mysql
environment:
- DB_SERVER_HOST=zabbix-mysql
- MYSQL_USER=root
- MYSQL_PASSWORD="123456"
- ZBX_SERVER_HOST=zabbix-server-mysql
- PHP_TZ=Asia/Shanghai
ports:
- '80:80'
- '443:443'
links:
- zabbix-mysql
- zabbix-server-mysql
depends_on:
- zabbix-mysql
zabbix-server-mysql:
image: zabbix/zabbix-server-mysql
container_name: zabbix-server-mysql
environment:
- DB_SERVER_HOST=zabbix-mysql
- MYSQL_USER=root
- MYSQL_PASSWORD="123456"
ports:
- '10051:10051'
links:
- zabbix-mysql
depends_on:
- zabbix-mysql
配置文件中zabbix-mysql挂载了当前路径下的mysql_data文件夹到容器中的/var/lib/mysql,这样容器中的mysql的数据文件可以在linux主机中查看,即使容器停止并删除,数据文件任然保存在linux主机上
注意:搭建前相应的容器停止并删除,否则可能不会在linux主机上显示容器内的mysql数据文件
[root@centos71 zabbix]# docker-compose -f docker-compose up -d
Creating zabbix-mysql ... done
Creating zabbix-server-mysql ... done
Creating zabbix-web-apache-mysql ... done
[root@centos71 zabbix]# ls -lah
total 16K
drwxr-xr-x. 3 root root 58 Jul 26 15:24 .
dr-xr-x---. 26 root root 4.0K Jul 26 15:13 ..
-rw-r--r--. 1 root root 972 Jul 26 15:13 docker-compose
-rw-r--r--. 1 root root 13 Jul 25 14:32 .env
drwxr-xr-x. 6 systemd-bus-proxy ssh_keys 4.0K Jul 26 15:25 mysql_data
[root@centos71 zabbix]# cd mysql_data/
[root@centos71 mysql_data]# ls -alh
total 185M
drwxr-xr-x. 6 systemd-bus-proxy ssh_keys 4.0K Jul 26 15:25 .
drwxr-xr-x. 3 root root 58 Jul 26 15:24 ..
-rw-r-----. 1 systemd-bus-proxy ssh_keys 56 Jul 26 15:24 auto.cnf
-rw-------. 1 systemd-bus-proxy ssh_keys 1.7K Jul 26 15:24 ca-key.pem
-rw-r--r--. 1 systemd-bus-proxy ssh_keys 1.1K Jul 26 15:24 ca.pem
-rw-r--r--. 1 systemd-bus-proxy ssh_keys 1.1K Jul 26 15:24 client-cert.pem
-rw-------. 1 systemd-bus-proxy ssh_keys 1.7K Jul 26 15:24 client-key.pem
-rw-r-----. 1 systemd-bus-proxy ssh_keys 1.4K Jul 26 15:24 ib_buffer_pool
-rw-r-----. 1 systemd-bus-proxy ssh_keys 76M Jul 26 15:31 ibdata1
-rw-r-----. 1 systemd-bus-proxy ssh_keys 48M Jul 26 15:31 ib_logfile0
-rw-r-----. 1 systemd-bus-proxy ssh_keys 48M Jul 26 15:24 ib_logfile1
-rw-r-----. 1 systemd-bus-proxy ssh_keys 12M Jul 26 15:25 ibtmp1
drwxr-x---. 2 systemd-bus-proxy ssh_keys 4.0K Jul 26 15:24 mysql
drwxr-x---. 2 systemd-bus-proxy ssh_keys 8.0K Jul 26 15:24 performance_schema
-rw-------. 1 systemd-bus-proxy ssh_keys 1.7K Jul 26 15:24 private_key.pem
-rw-r--r--. 1 systemd-bus-proxy ssh_keys 451 Jul 26 15:24 public_key.pem
-rw-r--r--. 1 systemd-bus-proxy ssh_keys 1.1K Jul 26 15:24 server-cert.pem
-rw-------. 1 systemd-bus-proxy ssh_keys 1.7K Jul 26 15:24 server-key.pem
drwxr-x---. 2 systemd-bus-proxy ssh_keys 8.0K Jul 26 15:24 sys
drwxr-x---. 2 systemd-bus-proxy ssh_keys 12K Jul 26 15:25 zabbix
挂载路径写相对路径,可以将整个文件夹整体拷贝到其他机器,方便迁移
当重新使用docker-compose加载镜像启动容器时,不会覆盖之前映射的linux主机文件夹,而是直接使用,如下进行测试
在mysql_data文件夹下新建一个test.txt文件,写入一些字符串
[root@centos71 mysql_data]# touch test.txt
[root@centos71 mysql_data]# vim test.txt
[root@centos71 mysql_data]# ls
auto.cnf client-cert.pem ibdata1 ibtmp1 private_key.pem server-key.pem zabbix
ca-key.pem client-key.pem ib_logfile0 mysql public_key.pem sys
ca.pem ib_buffer_pool ib_logfile1 performance_schema server-cert.pem test.txt
停止并删除容器,新建的test.txt文件仍然存在
[root@centos71 zabbix]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5990d74a7f47 zabbix/zabbix-web-apache-mysql "docker-entrypoint.sh" 22 minutes ago Up 22 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp zabbix-web-apache-mysql
70f3037cacdf zabbix/zabbix-server-mysql "docker-entrypoint.sh" 22 minutes ago Up 22 minutes 0.0.0.0:10051->10051/tcp zabbix-server-mysql
7bbdd78149de mysql:5.7 "docker-entrypoint.s…" 22 minutes ago Up 22 minutes 0.0.0.0:32782->3306/tcp zabbix-mysql
[root@centos71 zabbix]# docker stop 5990d74a7f47 70f3037cacdf 7bbdd78149de
5990d74a7f47
70f3037cacdf
7bbdd78149de
[root@centos71 zabbix]#
[root@centos71 zabbix]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
5990d74a7f476568a779ed2e5566c09259724a86bbdfc98533640c13d83286aa
70f3037cacdfaee7a2455c47a4b8c64d0319291c919027ee1fd49e0eb0e5e414
7bbdd78149dead951bb111f87cb6627941bf2756cec5cdd80031a9fe5e657fb8
Total reclaimed space: 39.06kB
[root@centos71 zabbix]# ls
docker-compose mysql_data
[root@centos71 zabbix]# cd mysql_data/
[root@centos71 mysql_data]# ls
auto.cnf client-cert.pem ibdata1 mysql public_key.pem sys
ca-key.pem client-key.pem ib_logfile0 performance_schema server-cert.pem test.txt
ca.pem ib_buffer_pool ib_logfile1 private_key.pem server-key.pem zabbix
[root@centos71 mysql_data]# cat test.txt
if this text exists,not override
重新使用docker-compose文件启动容器(因为之前的容器已经删除,所以从image启动新容器),test.txt文件仍然存在
[root@centos71 zabbix]# docker-compose -f docker-compose up -d
Creating zabbix-mysql ... done
Creating zabbix-server-mysql ... done
Creating zabbix-web-apache-mysql ... done
[root@centos71 zabbix]#
[root@centos71 zabbix]#
[root@centos71 zabbix]# ls
docker-compose mysql_data
[root@centos71 zabbix]# cd mysql_data/
[root@centos71 mysql_data]# ls
auto.cnf client-cert.pem ibdata1 ibtmp1 private_key.pem server-key.pem zabbix
ca-key.pem client-key.pem ib_logfile0 mysql public_key.pem sys
ca.pem ib_buffer_pool ib_logfile1 performance_schema server-cert.pem test.txt
[root@centos71 mysql_data]# cat test.txt
if this text exists,not override
[root@centos71 mysql_data]#