Docker-compose实例及其编排 lnmp 集群!!!

一、Docker-compose实例

1、创建 yml ⽂件

[root@localhost ~]# cd test/
[root@localhost test]# ls # 创建⼀个⽬录,该⽬录是⼀
个:(project)⼯程
[root@localhost test]# cat docker-compose.yml #这
⾥⽂件名称是固定不变的
version: "3" # 指定⽂件版本
services:
 nginx: # 这是service名
 container_name: nginx01 # 这是容器名
 image: nginx:latest
 ports:
- "8001:80" # 端⼝映射
 volumes:
- /www/wwwroot/8001:/usr/share/nginx/html # 挂
载
 hostname: nginx.test.com # 容器主机名
 nginx-php: # 第⼆个service服务名称
 container_name: nginx02 # 第⼆个容器的名称
image: nginx:latest
 ports:
- "8002:80"
 volumes:
- /www/wwwroot/8002:/usr/share/nginx/html
 hostname: nginx-php.test.com
2、创建数据卷⽬录
[root@localhost test]# mkdir -p /www/wwwroot/8001 
# 创建⽬录
[root@localhost test]# mkdir -p /www/wwwroot/8002
[root@localhost test]# echo "8001" >
/www/wwwroot/8001/index.html # 准备索引⽂件
[root@localhost test]# echo "8002" >
/www/wwwroot/8002/index.html
3、启动 compose 集群
要在 docker-compose.yml ⽂件所在的⽬录下才能通过 dockercompose 命令启动容器。
[root@localhost test]# docker compose up -d # 启动
⼯程后会根据指定的容器名称,⽣成对应的容器
[+] Running 3/3
✔ Network test_default Created 0.1s
✔ Container nginx02 Started 0.1s
✔ Container nginx01 Started 0.1s
4、访问测试
[root@localhost test]# curl 192.168.15.3:8001
8001
[root@localhost test]# curl 192.168.15.3:8002
8002
5、查看并关闭容器
[root@localhost test]# docker compose ps -a #
查看docker-compose的所有容器
NAME IMAGE COMMAND 
 SERVICE CREATED 
STATUS PORTS
nginx01 nginx:latest "/docker-entrypoint.sh
nginx -g 'daemon off;'" nginx 3 minutes
ago Up 3 minutes 0.0.0.0:8001->80/tcp,
:::8001->80/tcp
nginx02 nginx:latest "/docker-entrypoint.sh
nginx -g 'daemon off;'" nginx-php 3 minutes
ago Up 3 minutes 0.0.0.0:8002->80/tcp,
:::8002->80/tcp
[root@localhost test]# docker compose stop nginx01
 # 以容器名关闭compose失败
no such service: nginx01
[root@localhost test]# docker compose stop nginx 
# 必须使⽤service关闭docker-compose
[+] Stopping 1/1
✔ Container nginx01 Stopped
6、删除集群中的容器
rm 命令只能删除已经停⽌的容器
[root@localhost test]# docker compose rm #删除容
器,不需要指定容器名称
? Going to remove nginx01 Yes
[+] Removing 1/0
✔ Container nginx01 Removed 
0.0s
[root@localhost test]# docker compose rm #再次删
除找不到容器,因为没有停⽌的容器
No stopped containers
[root@localhost test]# docker compose ps #查看运⾏
的容器,⽬前只有⼀个nginx02
NAME IMAGE COMMAND 
 SERVICE CREATED 
STATUS PORTS
nginx02 nginx:latest "/docker-entrypoint.sh
nginx -g 'daemon off;'" nginx-php 6 minutes
ago Up 6 minutes 0.0.0.0:8002->80/tcp,
:::8002->80/tcp
强制删除
[root@localhost test]# docker compose down #强制
删除创建的容器及其他内容
[+] Running 2/2
✔ Container nginx02 Removed 
0.5s
✔ Network test_default Removed 
0.1s
7、检查 YAML ⽂件语法
[root@localhost test]# docker compose config #检
查test⽬录下docker-compose是否存在语法错误
name: test
services:
 nginx:
 container_name: nginx01
 hostname: nginx.test.com
 image: nginx:latest
 networks:
 default: null
 ports:
- mode: ingress
 target: 80
 published: "8001"
 protocol: tcp
 volumes:
- type: bind
source: /www/wwwroot/8001
 target: /usr/share/nginx/html
 bind:
 create_host_path: true
 nginx-php:
 container_name: nginx02
 hostname: nginx-php.test.com
 image: nginx:latest
 networks:
 default: null
 ports:
- mode: ingress
 target: 80
 published: "8002"
 protocol: tcp
 volumes:
- type: bind
source: /www/wwwroot/8002
 target: /usr/share/nginx/html
 bind:
 create_host_path: true
networks:
 default:
 name: test_default
[root@localhost test]# docker compose config -q 
#-q 检查语法,但不输出
[root@localhost test]# docker compose config --
services #--services 查看service
nginx
nginx-php
[root@localhost test]# docker compose config --
volumes #--volumes 查看volume
8、进⼊容器或执⾏临时命令
[root@localhost test]# docker compose exec nginx
/bin/bash # 进⼊nginx容器内部,并给容器⼀个bash环境
root@nginx:/# ls
bin dev docker-entrypoint.sh home lib64 
mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media 
opt root sbin sys usr
root@nginx:/# exit
[root@localhost test]# docker compose run nginxphp ls # 让容器临时执⾏⼀下ls命令
bin dev docker-entrypoint.sh home lib64 
mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media 
opt root sbin sys usr
root@nginx-php:/# exit
9、查看容器的输出⽇志
[root@localhost test]# docker compose logs nginx 
#查看docker-compose中nginx服务的⽇志
nginx01 | /docker-entrypoint.sh: /dockerentrypoint.d/ is not empty, will attempt to
perform configuration
nginx01 | /docker-entrypoint.sh: Looking for
shell scripts in /docker-entrypoint.d/
nginx01 | /docker-entrypoint.sh: Launching
/docker-entrypoint.d/10-listen-on-ipv6-bydefault.sh
nginx01 | 10-listen-on-ipv6-by-default.sh: info:
Getting the checksum of
/etc/nginx/conf.d/default.conf
nginx01 | 10-listen-on-ipv6-by-default.sh: info:
Enabled listen on IPv6 in
/etc/nginx/conf.d/default.conf
nginx01 | /docker-entrypoint.sh: Launching
/docker-entrypoint.d/20-envsubst-on-templates.sh
nginx01 | /docker-entrypoint.sh: Launching
/docker-entrypoint.d/30-tune-worker-processes.sh
nginx01 | /docker-entrypoint.sh: Configuration
complete; ready for start up
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: using
the "epoll" event method
nginx01 | 2023/12/13 13:37:24 [notice] 1#1:
nginx/1.21.5
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: built
by gcc 10.2.1 20210110 (Debian 10.2.1-6)
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: OS:
Linux 4.18.0-425.3.1.el8.x86_64
nginx01 | 2023/12/13 13:37:24 [notice] 1#1:
getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start
worker processes
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start
worker process 30
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start
worker process 31
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start
worker process 32
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start
worker process 33
[root@localhost test]# docker compose logs #查看
docker-compose中所有服务的⽇志
nginx01 | /docker-entrypoint.sh: /dockerentrypoint.d/ is not empty, will attempt to
perform configuration
nginx01 | /docker-entrypoint.sh: Looking for
shell scripts in /docker-entrypoint.d/
nginx01 | /docker-entrypoint.sh: Launching
/docker-entrypoint.d/10-listen-on-ipv6-bydefault.sh
nginx01 | 10-listen-on-ipv6-by-default.sh: info:
Getting the checksum of
/etc/nginx/conf.d/default.conf
nginx01 | 10-listen-on-ipv6-by-default.sh: info:
Enabled listen on IPv6 in
/etc/nginx/conf.d/default.conf
nginx01 | /docker-entrypoint.sh: Launching
/docker-entrypoint.d/20-envsubst-on-templates.sh
nginx01 | /docker-entrypoint.sh: Launching
/docker-entrypoint.d/30-tune-worker-processes.sh
nginx01 | /docker-entrypoint.sh: Configuration
complete; ready for start up
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: using
the "epoll" event method
nginx01 | 2023/12/13 13:37:24 [notice] 1#1:
nginx/1.21.5
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: built
by gcc 10.2.1 20210110 (Debian 10.2.1-6)
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: OS:
Linux 4.18.0-425.3.1.el8.x86_64
nginx01 | 2023/12/13 13:37:24 [notice] 1#1:
getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start
worker processes
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start
worker process 30
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start
worker process 31
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start
worker process 32
nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start
worker process 33
nginx02 | /docker-entrypoint.sh: /dockerentrypoint.d/ is not empty, will attempt to
perform configuration
nginx02 | /docker-entrypoint.sh: Looking for
shell scripts in /docker-entrypoint.d/
nginx02 | /docker-entrypoint.sh: Launching
/docker-entrypoint.d/10-listen-on-ipv6-bydefault.sh
nginx02 | 10-listen-on-ipv6-by-default.sh: info:
Getting the checksum of
/etc/nginx/conf.d/default.conf
nginx02 | 10-listen-on-ipv6-by-default.sh: info:
Enabled listen on IPv6 in
/etc/nginx/conf.d/default.conf
nginx02 | /docker-entrypoint.sh: Launching
/docker-entrypoint.d/20-envsubst-on-templates.sh
nginx02 | /docker-entrypoint.sh: Launching
/docker-entrypoint.d/30-tune-worker-processes.sh
nginx02 | /docker-entrypoint.sh: Configuration
complete; ready for start up
nginx02 | 2023/12/13 13:37:24 [notice] 1#1: using
the "epoll" event method
nginx02 | 2023/12/13 13:37:24 [notice] 1#1:
nginx/1.21.5
nginx02 | 2023/12/13 13:37:24 [notice] 1#1: built
by gcc 10.2.1 20210110 (Debian 10.2.1-6)
nginx02 | 2023/12/13 13:37:24 [notice] 1#1: OS:
Linux 4.18.0-425.3.1.el8.x86_64
nginx02 | 2023/12/13 13:37:24 [notice] 1#1:
getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx02 | 2023/12/13 13:37:24 [notice] 1#1: start
worker processes
nginx02 | 2023/12/13 13:37:24 [notice] 1#1: start
worker process 31
nginx02 | 2023/12/13 13:37:24 [notice] 1#1: start
worker process 32
nginx02 | 2023/12/13 13:37:24 [notice] 1#1: start
worker process 33
nginx02 | 2023/12/13 13:37:24 [notice] 1#1: start
worker process 34
# -f:跟踪输出⽇志
10、查看容器集群的镜像
[root@localhost test]# docker compose images
CONTAINER REPOSITORY 
TAG IMAGE ID SIZE
nginx01 nginx 
latest 605c77e624dd 141MB
nginx02 nginx 
latest 605c77e624dd 141MB
test-nginx-run-b1081d144ae5 nginx 
latest 605c77e624dd 141MB
11、启动多个相同配置的容器 1. 有时候需要⼀个集群,启动多个相同的容器,但是 compose 不
提供负载代理功能,只能⾃⼰启动代理。
2. 需要伸展的容器必须不能使⽤名字和固定端⼝。
[root@localhost test]# cat docker-compose.yml
version: "3"
networks:
 haha:
 external: true
services:
 nginx:
#container_name: nginx01 # 使⽤scale不能使⽤名
字,注释掉
 image: nginx:latest
# ports: # 使⽤scale不能使⽤端⼝,注释掉
#- "8001:80"
 volumes:
- /www/wwwroot/8001:/usr/share/nginx/html
 hostname: nginx.test.com
 networks:
- haha
 nginx-php:
# container_name: nginx02
 image: nginx:latest
#ports:
#- "8002:80"
 volumes:
- /www/wwwroot/8002:/usr/share/nginx/html
 hostname: nginx-php.test.com
 networks:
- haha
[root@localhost test]# docker compose up -d --
scale nginx=3
# 指定启动3个nginx容器,不加参数默认启动⼀个,nginx-php没
加该参数,所以只启动了⼀个
[+] Running 4/4
✔ Container test-nginx-3 Started 
0.0s
✔ Container test-nginx-php-1 Started 
0.1s
✔ Container test-nginx-2 Started 
0.0s
✔ Container test-nginx-1 Started 
0.0s
12、控制容器集群的注意事项
# 在启动docker-compose的注意事项
# 所有docker-compose命令必须在docker-compose.yml⽂件所
在的项⽬⽬录下⾯,否则执⾏会失败
[root@localhost ~]# docker compose up -d #在其他
⽬录下启动docker-compose失败
no configuration file provided: not found
[root@localhost ~]# docker compose -f test/dockercompose.yml -p web up -d #需要使⽤参数书指定⽬录位置
# -f 指定yaml⽂件名字及路径
# -p 必须指定项⽬名,否则默认为yaml⽂件所在⽬录名
# 使⽤这两个选项优点麻烦,不如直接cd到docker-compose的⼯
程⽬录
[+] Running 3/3
✔ Network web_default Created 
0.1s #创建⽹络
✔ Container nginx02 Started 
0.1s #创建容器
✔ Container nginx01 Started 
0.1s #创建容器
# 查询也必须指定yaml⽂件和项⽬⽬录
[root@localhost ~]# docker compose ps # 没有
yaml和项⽬名⽆法查看,正确的查看⽅法是假-f和-p
no configuration file provided: not found
[root@localhost ~]# docker compose -f test/dockercompose.yml ps # 没有-p,不能得到查询结果
NAME IMAGE COMMAND SERVICE CREATED 
STATUS PORTS
[root@localhost ~]# docker compose -f test/dockercompose.yml -p web ps # -f和-p可以查出结果
NAME IMAGE COMMAND 
 SERVICE CREATED 
STATUS PORTS
nginx01 nginx:latest "/docker-entrypoint.sh
nginx -g 'daemon off;'" nginx 34 seconds
ago Up 32 seconds 0.0.0.0:8001->80/tcp,
:::8001->80/tcp
nginx02 nginx:latest "/docker-entrypoint.sh
nginx -g 'daemon off;'" nginx-php 34 seconds
ago Up 31 seconds 0.0.0.0:8002->80/tcp,
:::8002->80/tcp

二、docker-compose 编排 lnmp 集群
1、docker-compose.yml ⽂件
[root@doc lnmp]# vim docker-compose.yml
version: '2'
volumes:
 mysql-conf:
 php-conf:
networks:
 lnmp_net:
 external: true
services:
 nginx:
 image: nginx
 container_name: nginx-lnmp
 hostname: nginx-lnmp
 privileged: true
 ports:
- 90:80
 volumes:
-
/root/lnmp/nginx/html/:/usr/share/nginx/html/:rw
-
/root/lnmp/nginx/conf.d/:/etc/nginx/conf.d/:rw
- /root/lnmp/nginx/logs/:/var/log/nginx/:rw
restart: always
 networks:
- lnmp_net
 php:
 image: php:7.3.29-fpm
 container_name: php-lnmp
 hostname: php-lnmp
 privileged: true
 volumes:
- /root/lnmp/nginx/html/:/var/www/html/:rw
- php-conf:/usr/local/etc/php
 volumes_from:
- mysql
restart: always
 networks:
- lnmp_net
 mysql:
 image: mysql:5.6
 container_name: mysql-lnmp
 ports:
- 3306:3306
 privileged: true
 volumes:
- /var/lib/mysql
- /root/lnmp/mysql/data/:/var/lib/mysql/:rw
- mysql-conf:/etc/mysql/
restart: always
 networks:
- lnmp_net
 environment:
 MYSQL_ROOT_PASSWORD: "123456"
2、⼯程⽬录结构树状图
[root@doc lnmp]# tree ./
./
"## docker-compose.yml
"## mysql
$ %## data
"## mysql-conf
$ "## conf.d
"## nginx
$ "## conf.d
$ "## html
$ $ "## index.html
$ $ "## testa.php
$ $ "## testb.php
$ $ %## wordpress
$ %## logs
%## php-conf
 "## conf.d
 $ "## docker-php-ext-pdo_mysql.ini
 $ %## docker-php-ext-sodium.ini
 "## php.ini
 "## php.ini-development
 %## php.ini-production
3、修改并导⼊配置⽂件
(1)nginx 配置⽂件
[root@doc lnmp]# vim nginx/conf.d/default.conf
server {
 listen 80;
 root /usr/share/nginx/html;
 index index.html index.htm index.php;
# redirect server error pages to the static
page /50x.html
#
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/share/nginx/html;
 }
 location / {
 index index.html index.htm index.php ;
try_files $uri $uri/ /index.php?
$query_string;
 autoindex on;
 }
 location ~ \.php$ {
 fastcgi_pass php-lnmp:9000;
 fastcgi_index index.php;
 include fastcgi_params;
 fastcgi_param PATH_INFO
$fastcgi_path_info;
 fastcgi_param SCRIPT_FILENAME 
/var/www/html/$fastcgi_script_name;
 }
}
(2)导⼊ mysql 和 php 的配置⽂件到容器内
[root@doc lnmp]# mv mysql-conf/*
/var/lib/docker/volumes/lnmp_mysql-conf/_data/
[root@doc lnmp]# mv php-conf/*
/var/lib/docker/volumes/lnmp_php-conf/_data/
(3)php 容器安装 mysql 扩展插件
[root@doc lnmp]# docker compose exec php bash
root@php-lnmp:/var/www/html# docker-php-extinstall pdo_mysql
root@php-lnmp:/var/www/html# docker-php-extinstall mysqli
root@php-lnmp:/var/www/html#
exit
[root@doc lnmp]# docker compose restart php
[+] Restarting 1/1
✔ Container php-lnmp Started 
0.3s

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我来为您介绍一下使用docker-compose实现容器编排的具体步骤。 首先,您需要安装DockerDocker Compose。具体安装方法可以参考官方文档。 接着,您需要创建一个docker-compose.yml文件,在该文件中定义您需要的服务和容器。例如,以下是一个简单的docker-compose.yml文件: ``` version: '3' services: web: image: nginx ports: - "8080:80" db: image: mysql environment: MYSQL_ROOT_PASSWORD: example MYSQL_DATABASE: test MYSQL_USER: test MYSQL_PASSWORD: test ``` 该文件定义了两个服务:web和db。web服务使用nginx镜像,并将本地主机的8080端口映射到容器的80端口。db服务使用mysql镜像,并设置了一些环境变量。 接下来,您可以使用docker-compose命令来启动这些服务。在终端中进入docker-compose.yml文件所在目录,输入以下命令: ``` docker-compose up ``` 该命令会启动所有定义在docker-compose.yml文件中的服务,并将它们连接在一起。 如果您需要在后台运行服务,可以使用以下命令: ``` docker-compose up -d ``` 如果您需要停止服务,可以使用以下命令: ``` docker-compose down ``` 这些命令将会停止并删除所有的容器和网络。如果您只想停止服务,而不删除容器和网络,可以使用以下命令: ``` docker-compose stop ``` 以上就是使用docker-compose实现容器编排的基本步骤。当然,您可以根据需要定义更多的服务和容器,并使用docker-compose命令来管理它们。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值