8.30笔记

一、Docker-compose实例

1、创建 yml ⽂件

 
  1. [root@localhost ~]# cd test/

  2. [root@localhost test]# ls # 创建⼀个⽬录,该⽬录是⼀

  3. 个:(project)⼯程

  4. [root@localhost test]# cat docker-compose.yml #这

  5. ⾥⽂件名称是固定不变的

  6. version: "3" # 指定⽂件版本

  7. services:

  8. nginx: # 这是service名

  9. container_name: nginx01 # 这是容器名

  10. image: nginx:latest

  11. ports:

  12. - "8001:80" # 端⼝映射

  13. volumes:

  14. - /www/wwwroot/8001:/usr/share/nginx/html # 挂

  15. hostname: nginx.test.com # 容器主机名

  16. nginx-php: # 第⼆个service服务名称

  17. container_name: nginx02 # 第⼆个容器的名称

  18. image: nginx:latest

  19. ports:

  20. - "8002:80"

  21. volumes:

  22. - /www/wwwroot/8002:/usr/share/nginx/html

  23. hostname: nginx-php.test.com

2、创建数据卷⽬录

 
  1. [root@localhost test]# mkdir -p /www/wwwroot/8001

  2. # 创建⽬录

  3. [root@localhost test]# mkdir -p /www/wwwroot/8002

  4. [root@localhost test]# echo "8001" >

  5. /www/wwwroot/8001/index.html # 准备索引⽂件

  6. [root@localhost test]# echo "8002" >

  7. /www/wwwroot/8002/index.html

3、启动 compose 集群

要在 docker-compose.yml ⽂件所在的⽬录下才能通过 dockercompose 命令启动容器。

 
  1. [root@localhost test]# docker compose up -d # 启动

  2. ⼯程后会根据指定的容器名称,⽣成对应的容器

  3. [+] Running 3/3

  4. ✔ Network test_default Created 0.1s

  5. ✔ Container nginx02 Started 0.1s

  6. ✔ Container nginx01 Started 0.1s

4、访问测试

 
  1. [root@localhost test]# curl 192.168.15.3:8001

  2. 8001

  3. [root@localhost test]# curl 192.168.15.3:8002

  4. 8002

5、查看并关闭容器

 
  1. [root@localhost test]# docker compose ps -a #

  2. 查看docker-compose的所有容器

  3. NAME IMAGE COMMAND

  4. SERVICE CREATED

  5. STATUS PORTS

  6. nginx01 nginx:latest "/docker-entrypoint.sh

  7. nginx -g 'daemon off;'" nginx 3 minutes

  8. ago Up 3 minutes 0.0.0.0:8001->80/tcp,

  9. :::8001->80/tcp

  10. nginx02 nginx:latest "/docker-entrypoint.sh

  11. nginx -g 'daemon off;'" nginx-php 3 minutes

  12. ago Up 3 minutes 0.0.0.0:8002->80/tcp,

  13. :::8002->80/tcp

  14. [root@localhost test]# docker compose stop nginx01

  15. # 以容器名关闭compose失败

  16. no such service: nginx01

  17. [root@localhost test]# docker compose stop nginx

  18. # 必须使⽤service关闭docker-compose

  19. [+] Stopping 1/1

  20. ✔ Container nginx01 Stopped

6、删除集群中的容器

rm 命令只能删除已经停⽌的容器

 
  1. [root@localhost test]# docker compose rm #删除容

  2. 器,不需要指定容器名称

  3. ? Going to remove nginx01 Yes

  4. [+] Removing 1/0

  5. ✔ Container nginx01 Removed

  6. 0.0s

  7. [root@localhost test]# docker compose rm #再次删

  8. 除找不到容器,因为没有停⽌的容器

  9. No stopped containers

  10. [root@localhost test]# docker compose ps #查看运⾏

  11. 的容器,⽬前只有⼀个nginx02

  12. NAME IMAGE COMMAND

  13. SERVICE CREATED

  14. STATUS PORTS

  15. nginx02 nginx:latest "/docker-entrypoint.sh

  16. nginx -g 'daemon off;'" nginx-php 6 minutes

  17. ago Up 6 minutes 0.0.0.0:8002->80/tcp,

  18. :::8002->80/tcp

强制删除

 
  1. [root@localhost test]# docker compose down #强制

  2. 删除创建的容器及其他内容

  3. [+] Running 2/2

  4. ✔ Container nginx02 Removed

  5. 0.5s

  6. ✔ Network test_default Removed

  7. 0.1s

7、检查 YAML ⽂件语法

 
  1. [root@localhost test]# docker compose config #检

  2. 查test⽬录下docker-compose是否存在语法错误

  3. name: test

  4. services:

  5. nginx:

  6. container_name: nginx01

  7. hostname: nginx.test.com

  8. image: nginx:latest

  9. networks:

  10. default: null

  11. ports:

  12. - mode: ingress

  13. target: 80

  14. published: "8001"

  15. protocol: tcp

  16. volumes:

  17. - type: bind

  18. source: /www/wwwroot/8001

  19. target: /usr/share/nginx/html

  20. bind:

  21. create_host_path: true

  22. nginx-php:

  23. container_name: nginx02

  24. hostname: nginx-php.test.com

  25. image: nginx:latest

  26. networks:

  27. default: null

  28. ports:

  29. - mode: ingress

  30. target: 80

  31. published: "8002"

  32. protocol: tcp

  33. volumes:

  34. - type: bind

  35. source: /www/wwwroot/8002

  36. target: /usr/share/nginx/html

  37. bind:

  38. create_host_path: true

  39. networks:

  40. default:

  41. name: test_default

  42. [root@localhost test]# docker compose config -q

  43. #-q 检查语法,但不输出

  44. [root@localhost test]# docker compose config --

  45. services #--services 查看service

  46. nginx

  47. nginx-php

  48. [root@localhost test]# docker compose config --

  49. volumes #--volumes 查看volume

8、进⼊容器或执⾏临时命令

 
  1. [root@localhost test]# docker compose exec nginx

  2. /bin/bash # 进⼊nginx容器内部,并给容器⼀个bash环境

  3. root@nginx:/# ls

  4. bin dev docker-entrypoint.sh home lib64

  5. mnt proc run srv tmp var

  6. boot docker-entrypoint.d etc lib media

  7. opt root sbin sys usr

  8. root@nginx:/# exit

  9. [root@localhost test]# docker compose run nginxphp ls # 让容器临时执⾏⼀下ls命令

  10. bin dev docker-entrypoint.sh home lib64

  11. mnt proc run srv tmp var

  12. boot docker-entrypoint.d etc lib media

  13. opt root sbin sys usr

  14. root@nginx-php:/# exit

9、查看容器的输出⽇志

 
  1. [root@localhost test]# docker compose logs nginx

  2. #查看docker-compose中nginx服务的⽇志

  3. nginx01 | /docker-entrypoint.sh: /dockerentrypoint.d/ is not empty, will attempt to

  4. perform configuration

  5. nginx01 | /docker-entrypoint.sh: Looking for

  6. shell scripts in /docker-entrypoint.d/

  7. nginx01 | /docker-entrypoint.sh: Launching

  8. /docker-entrypoint.d/10-listen-on-ipv6-bydefault.sh

  9. nginx01 | 10-listen-on-ipv6-by-default.sh: info:

  10. Getting the checksum of

  11. /etc/nginx/conf.d/default.conf

  12. nginx01 | 10-listen-on-ipv6-by-default.sh: info:

  13. Enabled listen on IPv6 in

  14. /etc/nginx/conf.d/default.conf

  15. nginx01 | /docker-entrypoint.sh: Launching

  16. /docker-entrypoint.d/20-envsubst-on-templates.sh

  17. nginx01 | /docker-entrypoint.sh: Launching

  18. /docker-entrypoint.d/30-tune-worker-processes.sh

  19. nginx01 | /docker-entrypoint.sh: Configuration

  20. complete; ready for start up

  21. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: using

  22. the "epoll" event method

  23. nginx01 | 2023/12/13 13:37:24 [notice] 1#1:

  24. nginx/1.21.5

  25. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: built

  26. by gcc 10.2.1 20210110 (Debian 10.2.1-6)

  27. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: OS:

  28. Linux 4.18.0-425.3.1.el8.x86_64

  29. nginx01 | 2023/12/13 13:37:24 [notice] 1#1:

  30. getrlimit(RLIMIT_NOFILE): 1048576:1048576

  31. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start

  32. worker processes

  33. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start

  34. worker process 30

  35. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start

  36. worker process 31

  37. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start

  38. worker process 32

  39. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start

  40. worker process 33

  41. [root@localhost test]# docker compose logs #查看

  42. docker-compose中所有服务的⽇志

  43. nginx01 | /docker-entrypoint.sh: /dockerentrypoint.d/ is not empty, will attempt to

  44. perform configuration

  45. nginx01 | /docker-entrypoint.sh: Looking for

  46. shell scripts in /docker-entrypoint.d/

  47. nginx01 | /docker-entrypoint.sh: Launching

  48. /docker-entrypoint.d/10-listen-on-ipv6-bydefault.sh

  49. nginx01 | 10-listen-on-ipv6-by-default.sh: info:

  50. Getting the checksum of

  51. /etc/nginx/conf.d/default.conf

  52. nginx01 | 10-listen-on-ipv6-by-default.sh: info:

  53. Enabled listen on IPv6 in

  54. /etc/nginx/conf.d/default.conf

  55. nginx01 | /docker-entrypoint.sh: Launching

  56. /docker-entrypoint.d/20-envsubst-on-templates.sh

  57. nginx01 | /docker-entrypoint.sh: Launching

  58. /docker-entrypoint.d/30-tune-worker-processes.sh

  59. nginx01 | /docker-entrypoint.sh: Configuration

  60. complete; ready for start up

  61. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: using

  62. the "epoll" event method

  63. nginx01 | 2023/12/13 13:37:24 [notice] 1#1:

  64. nginx/1.21.5

  65. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: built

  66. by gcc 10.2.1 20210110 (Debian 10.2.1-6)

  67. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: OS:

  68. Linux 4.18.0-425.3.1.el8.x86_64

  69. nginx01 | 2023/12/13 13:37:24 [notice] 1#1:

  70. getrlimit(RLIMIT_NOFILE): 1048576:1048576

  71. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start

  72. worker processes

  73. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start

  74. worker process 30

  75. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start

  76. worker process 31

  77. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start

  78. worker process 32

  79. nginx01 | 2023/12/13 13:37:24 [notice] 1#1: start

  80. worker process 33

  81. nginx02 | /docker-entrypoint.sh: /dockerentrypoint.d/ is not empty, will attempt to

  82. perform configuration

  83. nginx02 | /docker-entrypoint.sh: Looking for

  84. shell scripts in /docker-entrypoint.d/

  85. nginx02 | /docker-entrypoint.sh: Launching

  86. /docker-entrypoint.d/10-listen-on-ipv6-bydefault.sh

  87. nginx02 | 10-listen-on-ipv6-by-default.sh: info:

  88. Getting the checksum of

  89. /etc/nginx/conf.d/default.conf

  90. nginx02 | 10-listen-on-ipv6-by-default.sh: info:

  91. Enabled listen on IPv6 in

  92. /etc/nginx/conf.d/default.conf

  93. nginx02 | /docker-entrypoint.sh: Launching

  94. /docker-entrypoint.d/20-envsubst-on-templates.sh

  95. nginx02 | /docker-entrypoint.sh: Launching

  96. /docker-entrypoint.d/30-tune-worker-processes.sh

  97. nginx02 | /docker-entrypoint.sh: Configuration

  98. complete; ready for start up

  99. nginx02 | 2023/12/13 13:37:24 [notice] 1#1: using

  100. the "epoll" event method

  101. nginx02 | 2023/12/13 13:37:24 [notice] 1#1:

  102. nginx/1.21.5

  103. nginx02 | 2023/12/13 13:37:24 [notice] 1#1: built

  104. by gcc 10.2.1 20210110 (Debian 10.2.1-6)

  105. nginx02 | 2023/12/13 13:37:24 [notice] 1#1: OS:

  106. Linux 4.18.0-425.3.1.el8.x86_64

  107. nginx02 | 2023/12/13 13:37:24 [notice] 1#1:

  108. getrlimit(RLIMIT_NOFILE): 1048576:1048576

  109. nginx02 | 2023/12/13 13:37:24 [notice] 1#1: start

  110. worker processes

  111. nginx02 | 2023/12/13 13:37:24 [notice] 1#1: start

  112. worker process 31

  113. nginx02 | 2023/12/13 13:37:24 [notice] 1#1: start

  114. worker process 32

  115. nginx02 | 2023/12/13 13:37:24 [notice] 1#1: start

  116. worker process 33

  117. nginx02 | 2023/12/13 13:37:24 [notice] 1#1: start

  118. worker process 34

  119. # -f:跟踪输出⽇志

10、查看容器集群的镜像

 
  1. [root@localhost test]# docker compose images

  2. CONTAINER REPOSITORY

  3. TAG IMAGE ID SIZE

  4. nginx01 nginx

  5. latest 605c77e624dd 141MB

  6. nginx02 nginx

  7. latest 605c77e624dd 141MB

  8. test-nginx-run-b1081d144ae5 nginx

  9. latest 605c77e624dd 141MB

11、启动多个相同配置的容器 1. 有时候需要⼀个集群,启动多个相同的容器,但是 compose 不

提供负载代理功能,只能⾃⼰启动代理。

2. 需要伸展的容器必须不能使⽤名字和固定端⼝。

 
  1. [root@localhost test]# cat docker-compose.yml

  2. version: "3"

  3. networks:

  4. haha:

  5. external: true

  6. services:

  7. nginx:

  8. #container_name: nginx01 # 使⽤scale不能使⽤名

  9. 字,注释掉

  10. image: nginx:latest

  11. # ports: # 使⽤scale不能使⽤端⼝,注释掉

  12. #- "8001:80"

  13. volumes:

  14. - /www/wwwroot/8001:/usr/share/nginx/html

  15. hostname: nginx.test.com

  16. networks:

  17. - haha

  18. nginx-php:

  19. # container_name: nginx02

  20. image: nginx:latest

  21. #ports:

  22. #- "8002:80"

  23. volumes:

  24. - /www/wwwroot/8002:/usr/share/nginx/html

  25. hostname: nginx-php.test.com

  26. networks:

  27. - haha

  28. [root@localhost test]# docker compose up -d --

  29. scale nginx=3

  30. # 指定启动3个nginx容器,不加参数默认启动⼀个,nginx-php没

  31. 加该参数,所以只启动了⼀个

  32. [+] Running 4/4

  33. ✔ Container test-nginx-3 Started

  34. 0.0s

  35. ✔ Container test-nginx-php-1 Started

  36. 0.1s

  37. ✔ Container test-nginx-2 Started

  38. 0.0s

  39. ✔ Container test-nginx-1 Started

  40. 0.0s

12、控制容器集群的注意事项

 
  1. # 在启动docker-compose的注意事项

  2. # 所有docker-compose命令必须在docker-compose.yml⽂件所

  3. 在的项⽬⽬录下⾯,否则执⾏会失败

  4. [root@localhost ~]# docker compose up -d #在其他

  5. ⽬录下启动docker-compose失败

  6. no configuration file provided: not found

  7. [root@localhost ~]# docker compose -f test/dockercompose.yml -p web up -d #需要使⽤参数书指定⽬录位置

  8. # -f 指定yaml⽂件名字及路径

  9. # -p 必须指定项⽬名,否则默认为yaml⽂件所在⽬录名

  10. # 使⽤这两个选项优点麻烦,不如直接cd到docker-compose的⼯

  11. 程⽬录

  12. [+] Running 3/3

  13. ✔ Network web_default Created

  14. 0.1s #创建⽹络

  15. ✔ Container nginx02 Started

  16. 0.1s #创建容器

  17. ✔ Container nginx01 Started

  18. 0.1s #创建容器

  19. # 查询也必须指定yaml⽂件和项⽬⽬录

  20. [root@localhost ~]# docker compose ps # 没有

  21. yaml和项⽬名⽆法查看,正确的查看⽅法是假-f和-p

  22. no configuration file provided: not found

  23. [root@localhost ~]# docker compose -f test/dockercompose.yml ps # 没有-p,不能得到查询结果

  24. NAME IMAGE COMMAND SERVICE CREATED

  25. STATUS PORTS

  26. [root@localhost ~]# docker compose -f test/dockercompose.yml -p web ps # -f和-p可以查出结果

  27. NAME IMAGE COMMAND

  28. SERVICE CREATED

  29. STATUS PORTS

  30. nginx01 nginx:latest "/docker-entrypoint.sh

  31. nginx -g 'daemon off;'" nginx 34 seconds

  32. ago Up 32 seconds 0.0.0.0:8001->80/tcp,

  33. :::8001->80/tcp

  34. nginx02 nginx:latest "/docker-entrypoint.sh

  35. nginx -g 'daemon off;'" nginx-php 34 seconds

  36. ago Up 31 seconds 0.0.0.0:8002->80/tcp,

  37. :::8002->80/tcp

 

二、docker-compose 编排 lnmp 集群

1、docker-compose.yml ⽂件

 
  1. [root@doc lnmp]# vim docker-compose.yml

  2. version: '2'

  3. volumes:

  4. mysql-conf:

  5. php-conf:

  6. networks:

  7. lnmp_net:

  8. external: true

  9. services:

  10. nginx:

  11. image: nginx

  12. container_name: nginx-lnmp

  13. hostname: nginx-lnmp

  14. privileged: true

  15. ports:

  16. - 90:80

  17. volumes:

  18. -

  19. /root/lnmp/nginx/html/:/usr/share/nginx/html/:rw

  20. -

  21. /root/lnmp/nginx/conf.d/:/etc/nginx/conf.d/:rw

  22. - /root/lnmp/nginx/logs/:/var/log/nginx/:rw

  23. restart: always

  24. networks:

  25. - lnmp_net

  26. php:

  27. image: php:7.3.29-fpm

  28. container_name: php-lnmp

  29. hostname: php-lnmp

  30. privileged: true

  31. volumes:

  32. - /root/lnmp/nginx/html/:/var/www/html/:rw

  33. - php-conf:/usr/local/etc/php

  34. volumes_from:

  35. - mysql

  36. restart: always

  37. networks:

  38. - lnmp_net

  39. mysql:

  40. image: mysql:5.6

  41. container_name: mysql-lnmp

  42. ports:

  43. - 3306:3306

  44. privileged: true

  45. volumes:

  46. - /var/lib/mysql

  47. - /root/lnmp/mysql/data/:/var/lib/mysql/:rw

  48. - mysql-conf:/etc/mysql/

  49. restart: always

  50. networks:

  51. - lnmp_net

  52. environment:

  53. MYSQL_ROOT_PASSWORD: "123456"

2、⼯程⽬录结构树状图

 
  1. [root@doc lnmp]# tree ./

  2. ./

  3. "## docker-compose.yml

  4. "## mysql

  5. $ %## data

  6. "## mysql-conf

  7. $ "## conf.d

  8. "## nginx

  9. $ "## conf.d

  10. $ "## html

  11. $ $ "## index.html

  12. $ $ "## testa.php

  13. $ $ "## testb.php

  14. $ $ %## wordpress

  15. $ %## logs

  16. %## php-conf

  17. "## conf.d

  18. $ "## docker-php-ext-pdo_mysql.ini

  19. $ %## docker-php-ext-sodium.ini

  20. "## php.ini

  21. "## php.ini-development

  22. %## php.ini-production

3、修改并导⼊配置⽂件

(1)nginx 配置⽂件

 
  1. [root@doc lnmp]# vim nginx/conf.d/default.conf

  2. server {

  3. listen 80;

  4. root /usr/share/nginx/html;

  5. index index.html index.htm index.php;

  6. # redirect server error pages to the static

  7. page /50x.html

  8. #

  9. error_page 500 502 503 504 /50x.html;

  10. location = /50x.html {

  11. root /usr/share/nginx/html;

  12. }

  13. location / {

  14. index index.html index.htm index.php ;

  15. try_files $uri $uri/ /index.php?

  16. $query_string;

  17. autoindex on;

  18. }

  19. location ~ \.php$ {

  20. fastcgi_pass php-lnmp:9000;

  21. fastcgi_index index.php;

  22. include fastcgi_params;

  23. fastcgi_param PATH_INFO

  24. $fastcgi_path_info;

  25. fastcgi_param SCRIPT_FILENAME

  26. /var/www/html/$fastcgi_script_name;

  27. }

  28. }

(2)导⼊ mysql 和 php 的配置⽂件到容器内

 
  1. [root@doc lnmp]# mv mysql-conf/*

  2. /var/lib/docker/volumes/lnmp_mysql-conf/_data/

  3. [root@doc lnmp]# mv php-conf/*

  4. /var/lib/docker/volumes/lnmp_php-conf/_data/

(3)php 容器安装 mysql 扩展插件

 
  1. [root@doc lnmp]# docker compose exec php bash

  2. root@php-lnmp:/var/www/html# docker-php-extinstall pdo_mysql

  3. root@php-lnmp:/var/www/html# docker-php-extinstall mysqli

  4. root@php-lnmp:/var/www/html#

  5. exit

  6. [root@doc lnmp]# docker compose restart php

  7. [+] Restarting 1/1

  8. ✔ Container php-lnmp Started

  9. 0.3s

  • 12
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值