Docker-compose
Compose是用于定义和运行多容器 Docker应用工具
通过Compose 可以使用YML文件来配置应用程序所需要的所有服务
Docker Compose使用的三步:
1.使用docker file 定义应用程序的环境
2.docker-compose定义构成应用程序的服务
3.启动Compose,就相当于启动应用
版本的区别
compose文件版本:version: '2.3'
docker-compose程序版本:
[root@harbor harbor]# docker-compose --version
docker-compose version 1.18.0, build 8dd22a9
docker版本:
[root@harbor harbor]# docker --version
Docker version 26.1.4, build 5650f9b
安装docker-compose
# 1.方法一:
[root@docker01 ~]# yum install -y docker-compose
[root@docker01 ~]# docker-compose --version
docker-compose version 1.18.0, build 8dd22a9
# 2.方法二:
[root@docker02 bin]# curl -L
"https://github.com/docker/compose/releases/download/v2.29.2/dockercompose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
curl -L "https://github.com/docker/compose/releases/download/2.29.2/dockercompose-Linux-x86_64" -o /usr/local/bin/docker-compose
[root@docker02 bin]# chmod +x /usr/local/bin/docker-compose
[root@docker02 bin]# docker-compose --version
Docker Compose version v2.29.2
docker-compose操作和语法
###### 操作
# 第一次启动创建容器并前台输出日志
[root@docker01 mysql]# docker-compose -f mysql-compose.yml up
# 如果日志没有报错,直接后台启动
[root@docker01 mysql]# docker-compose -f mysql-compose.yml up -d
# 停止容器
[root@docker01 mysql]# docker-compose -f mysql-compose.yml stop
# 启动容器
[root@docker01 mysql]# docker-compose -f mysql-compose.yml start
# 重启容器
[root@docker01 mysql]# docker-compose -f mysql-compose.yml restart
# 查看该compose中的所有容器运行状态
[root@docker01 mysql]# docker-compose -f mysql-compose.yml ps
# 删除compose中的容器
[root@docker01 mysql]# docker-compose rm
# 查看日志
[root@docker01 mysql]# docker-compose logs
# 指定服务启停
[root@harbor harbor]# docker-compose start proxy
####### yml 语法严格缩进
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: 自定义网络名称
实战docekr-compose操作
##### mysql
docker run \
--name mysql80 \
-p 3306:3306 \
-v /data/mysql:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123 \
-e MYSQL_DATABASE=zabbix \
-e MYSQL_USER=zabbix \
-e MYSQL_PASSWORD=zabbix \
--restart=always \
-d mysql:8.0 \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_bin \
--log_bin_trust_function_creators
----------------------------------------------------------------------------
mkdir /mysql
vim docker-compose.yml
version: '2.0'
services:
mysql80:
image: mysql:8.0
container_name: mysql80
environment:
- MYSQL_ROOT_PASSWORD='123'
- MYSQL_DATABASE='zabbix'
- MYSQL_USER='zabbix'
- MYSQL_PASSWORD='zabbix'
volumes:
- /data/mysql:/var/lib/mysql
ports:
- 3306:3306
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_bin
- --log_bin_trust_function_creators
restart: always
##### zabbix-server
docker run \
--link mysql80 \
--name zabbix-server \
-p 10051:10051 \
-e DB_SERVER_HOST="mysql80" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-d zabbix/zabbix-server-mysql
-------------------------------------------------------
version: '3.0'
services:
zabbix-server:
image: zabbix/zabbix-server-mysql
container_name: zabbix-server
environment:
- DB_SERVER_HOST=mysql80
- MYSQL_USER=zabbix
- MYSQL_PASSWORD=zabbix
- MYSQL_DATABASE=zabbix
depends_on:
- mysql80
links:
- mysql80
ports:
- 10051:10051
restart: always
##### zabbix-web
docker run \
--link zabbix-server \
--link mysql80 \
--name zabbix-web \
-p 80:8080 \
-p 443:8443 \
-e DB_SERVER_HOST="mysql80" \
-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
-----------------------------------------------------------
version: '3.0'
services:
zabbix-web:
image: zabbix/zabbix-web-nginx-mysql
container_name: zabbix-web
environment:
- DB_SERVER_HOST=mysql80
- MYSQL_USER=zabbix
- MYSQL_PASSWORD=zabbix
- ZBX_SERVER_HOST=zabbix-server
- PHP_TZ=Asia/Shanghai
depends_on:
- mysql80
links:
- mysql80
- zabbix-server
ports:
- 80:8080
- 443:8443
restart: always
整合docker-compose(zabbix服务)
mkdir /zabbix
vim docker-compose.yml
version: '2.0'
services:
mysql80:
image: mysql:8.0
container_name: mysql80
environment:
- MYSQL_ROOT_PASSWORD='123'
- MYSQL_DATABASE='zabbix'
- MYSQL_USER='zabbix'
- MYSQL_PASSWORD='zabbix'
volumes:
- /data/mysql:/var/lib/mysql
ports:
- 3306:3306
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_bin
- --log_bin_trust_function_creators
restart: always
zabbix-server:
image: zabbix/zabbix-server-mysql
container_name: zabbix-server
environment:
- DB_SERVER_HOST=mysql80
- MYSQL_USER=zabbix
- MYSQL_PASSWORD=zabbix
- MYSQL_DATABASE=zabbix
depends_on:
- mysql80
links:
- mysql80
ports:
- 10051:10051
restart: always
zabbix-web:
image: zabbix/zabbix-web-nginx-mysql
container_name: zabbix-web
environment:
- DB_SERVER_HOST=mysql80
- MYSQL_USER=zabbix
- MYSQL_PASSWORD=zabbix
- ZBX_SERVER_HOST=zabbix-server
- PHP_TZ=Asia/Shanghai
depends_on:
- mysql80
links:
- mysql80
- zabbix-server
ports:
- 80:8080
- 443:8443
restart: always
整合docekr-compose(CICD服务)
docker run \
--name gitlab \
--hostname 10.0.0.102 \
--env GITLAB_OMNIBUS_CONFIG="external_url 'http://10.0.0.102'" \
-p 443:443 \
-p 80:80 \
-p 2222:22 \
--restart always \
-v /data/gitlab/config:/etc/gitlab \
-v /data/gitlab/logs:/var/log/gitlab \
-v /data/gitlab/data:/var/opt/gitlab \
-d gitlab/gitlab-ce
docker run \
--name jenkins \
--privileged \
--user=root \
--restart=always \
-v /data/jenkins/:/var/jenkins_home \
-v /usr/bin/docker:/usr/bin/docker \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /root/.ssh/:/root/.ssh/ \
-v /root/.docker/config.json:/root/.docker/config.json \
-p 8080:8080 \
-p 50000:50000 \
-d jenkins/jenkins:2.422
version: '3.6'
services:
gitlab:
image: gitlab/gitlab-ce
container_name: gitlab
restart: always
hostname: '10.0.0.102'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://10.0.0.102'
gitlab_rails['gitlab_shell_ssh_port'] = 2222
alertmanager['enable'] = false
node_exporter['enable'] = false
redis_exporter['enable'] = false
postgres_exporter['enable'] = false
ports:
- '80:80'
- '443:443'
- '2222:22'
volumes:
- '/data/gitlab/config:/etc/gitlab'
- '/data/gitlab/logs:/var/log/gitlab'
- '/data/gitlab/data:/var/opt/gitlab'
jenkins:
image: jenkins/jenkins:2.422
container_name: jenkins
restart: always
privileged: true
user: root
ports:
- '8080:8080'
- '50000:50000'
volumes:
- '/data/jenkins:/var/jenkins_home'
- '/usr/bin/docker:/usr/bin/docker'
- '/var/run/docker.sock:/var/run/docker.sock'
- '/root/.ssh/:/root/.ssh/'
- '/root/.docker/config.json:/root/.docker/config.json'