Docker单机编排工具
1、docker-compose的介绍
1 2 3 4 5 6 7
| Compose是用于定义和运行多容器Docker应用工具通过Compose 可以使用YML文件来配置应用程序所需要的所有服务,用来管理容器的起停,查看容器的日志,比较方便 不像docker run命令比较长,而且是临时执行的
DockerCompose使用的三步: 1.使用docker file 定义应用程序的环境镜像 2.docker-compose定义构成应用程序的服务 3.启动Compose,就相当于启动应用
|
版本
1 2 3 4 5 6 7 8 9
| 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
|
2、安装bocker-compose
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 1、安装bocker-compose [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/docker-compose-$(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@docker01 ~]# mv docker-compose-linux-x86_64\ \(2\) docker-compose [root@docker01 ~]# mv docker-compose /usr/local/bin/ [root@docker01 ~]# chmod +x /usr/local/bin/docker-compose [root@docker01 ~]# docker-compose --version Docker Compose version v2.29.2
scp /usr/local/bin/docker-compose 10.0.0.102:/usr/local/bin/
|
3、docker-compose的模版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| 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: 自定义网络名称
|
4、docker-compose——单机编排mysql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| 1、#找到运行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=123 \ --restart=always \ -d mysql:8.0 \ --character-set-server=utf8mb4 \ --collation-server=utf8mb4_bin \ --log_bin_trust_function_creators
2、#编写docker-compose 如果之前运行有其他版本的mysql,需要清除目录 [root@docker01 mysql]# rm -rf /data/ [root@docker01 ~]# mkdir mysql [root@docker01 ~]# cd mysql/ [root@docker01 mysql]# vim mysql-compose.yml (名字最好叫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=123 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
3、#第一次启动使用up,会创建容器并启动容器,前台输出日志 如果创建的文件名叫docker-compose.yml,启动命令是: docker-compose up 如果创建的文件名不叫docker-compose.yml,启动命令是: [root@docker01 mysql]# docker-compose -f mysql-compose.yml up
检查没问题就ctrl+c
|
docker-compose的操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| [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
[root@docker01 mysql]# docker-compose -f mysql-compose.yml ps
[root@docker01 mysql]# docker-compose rm
[root@docker01 mysql]# docker-compose logs
[root@harbor harbor]# docker-compose start proxy
|
5、docker-compose——单机编排zabbix
1、找到运行zabbix的docker命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| mysql要初始化。所以web-server要后启动 写成=如果报错。就要写成key: value
1、#找到运行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=123 \ --restart=always \ -d mysql:8.0 \ --character-set-server=utf8mb4 \ --collation-server=utf8mb4_bin \ --log_bin_trust_function_creators
2、运行容器化zabbix-server docker run \ --link mysql80 \ --name zabbix-server \ -p 10051:10051 \ -e DB_SERVER_HOST="mysql80" \ -e MYSQL_USER="zabbix" \ -e MYSQL_PASSWORD="123" \ -d zabbix/zabbix-server-mysql
3、容器化zabbix-web docker run \ --link zabbix-server \ --link mysql80 \ --name zabbix-web \ -p 80:8080 \ -p 8443:8443 \ -e DB_SERVER_HOST="mysql80" \ -e MYSQL_USER="zabbix" \ -e MYSQL_PASSWORD="123" \ -e ZBX_SERVER_HOST="zabbix-server" \ -e PHP_TZ="Asia/Shanghai" \ -d zabbix/zabbix-web-nginx-mysql
|
2、编写zabbix的docker-compose
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| [root@docker01 ~]# rm -rf /data/ [root@docker01 ~]# mkdir zabbix [root@docker01 ~]# cd zabbix/ [root@docker01 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=123 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=123 ports: - 10051:10051 links: - mysql80 restart: always zabbix-web: image: zabbix/zabbix-web-nginx-mysql container_name: zabbix-web environment: - DB_SERVER_HOST=mysql80 - MYSQL_USER=zabbix - MYSQL_PASSWORD=123 - ZBX_SERVER_HOST=zabbix-server - PHP_TZ=Asia/Shanghai ports: - 80:8080 - 443:8443 links: - mysql80 - zabbix-server restart: always
|
####报错
1 2 3 4 5
| environment: - DB_SERVER_HOST: 'mysql80' - MYSQL_USER: 'zabbix' - MYSQL_PASSWORD: 'zabbix'
|
3、启动zabbix
1 2 3 4 5 6 7 8 9
| 1、前台检查是否有报错 [root@docker01 zabbix]# docker-compose up
2、没有报错就放后台运行 [root@docker01 zabbix]# docker-compose up -d
3、浏览器访问 10.0.0.101
|
6、docker-compose——单机编排jenkins
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| 1、gitlab镜像 docker run \ --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 \ --name gitlab \ --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
2、运行jenkins容器 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 5000:5000 \ -d jenkins/jenkins:2.422
[root@docker01 ~]# mkdir jenkins && cd jenkins [root@docker01 jenkins]# vim jenkins-docker-compose.yml version: '3' services: gitlab: image: gitlab/gitlab-ce container_name: gitlab 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 volumes: - /data/gitlab/config:/etc/gitlab - /data/gitlab/logs:/var/log/gitlab - /data/gitlab/data:/var/opt/gitlab ports: - 443:443 - 80:80 - 2222:22 restart: always jenkins: image: jenkins/jenkins:2.422 container_name: jenkins user: root privileged: ture 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 ports: - 8080:8080 - 5000:5000 restart: always
|
7、docker-compose——单机编排wordpress
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| docker run \ --name mysql57 \ -p 3306:3306 \ -v /data/mysql:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=123 \ -e MYSQL_DATABASE=wp_db \ -e MYSQL_USER=wp_user \ -e MYSQL_PASSWORD=123 \ --restart=always \ -d mysql:5.7.44
docker run \ --name wp \ --link mysql57 \ -p 80:80 \ -e WORDPRESS_DB_HOST=mysql57 \ -e WORDPRESS_DB_USER=wp_user \ -e WORDPRESS_DB_PASSWORD=123 \ -e WORDPRESS_DB_NAME=wp_db \ -d wordpress-df:v1
|
2、编写docker-compos
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| [root@docker01 ~]# mkdir wp [root@docker01 ~]# cd wp/ [root@docker01 wp]# vim wp-docker-compose.yml version: '3' services: mysql57: image: mysql:5.7.44 container_name: mysql57 environment: - MYSQL_ROOT_PASSWORD=123 - MYSQL_DATABASE=wp_db - MYSQL_USER=wp_user - MYSQL_PASSWORD=123 volumes: - /data/mysql:/var/lib/mysql ports: - 3306:3306 restart: always
wp: image: wordpress-df:v2 container_name: wp environment: - WORDPRESS_DB_HOST=mysql57 - WORDPRESS_DB_USER=wp_user - WORDPRESS_DB_PASSWORD=123 - WORDPRESS_DB_NAME=wp_db ports: - 80:80 links: - mysql57 restart: always [root@docker01 wp]# docker-compose -f wp-docker-compose.yml up -d
|