(一)docker compose
(1)docker compose:
①概述:
1、单机容器编排工具
2、docker compse+docker、swarm实现集群管理
3、docker三剑客:docker machine{管理虚拟机}、docker compse{容器编排}、docker swarm{集群}
②docker compose格式:
1、docker comose书写格式:{现在使用1.18,但是配置文件要写成3.3}
2、docker compose能被识别的格式名字:
1.docker-compose.yml
2.docker-compose.yaml
3.或者docker-compose up -d -f 指定文件名
3、如果docker-compose简单的修改端口,数据卷,docker-compose up -d会自动识别,重新创建容器
4、但是如果容器的名字也改了,会造成新旧容器的端口冲突,会失败,可以 up -d --remove-orphans删除失效的容器
5、docker-compose要在构建docker-compose的目录下运行,在别的目录下运行是不行的,发现不了容器
③docker-compose的指令:
1、容器之间的依赖关系,就是先后启动顺序
depends_on:依赖,先启动指定的容器然后再启动当前容器
2、挂载数据卷:
volumes:
3、更多选项:
https://docs.docker.com/compose/compose-file/compose-file-v3/
④安装:
[root@docker1 ~]# yum -y install docker-compose
⑤docker compose的书写:
1、目录准备:
[root@docker1 ~]# mkdir -p /server/compose/1-run-nginx/
[root@docker1 ~]# cd /server/compose/1-run-nginx/
2、docker-compose书写:
[root@docker1 1-run-nginx]# cat docker-compose.yml
version: "3.3"
services:
web_ngx:
image: "nginx:1.20.2-alpine"
links:
- web_php
ports:
- "8000:80"
web_php:
image: "php:7-fpm"
3、构建容器:
[root@docker1 1-run-nginx]# docker-compose up -d
Creating 1runnginx_web_php_1 ... done
Creating 1runnginx_web_php_1 ...
Creating 1runnginx_web_ngx_1 ... done
[root@docker1 1-run-nginx]#
4、检查是否构建:
[root@docker1 1-run-nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
664cf0edba60 nginx:1.20.2-alpine "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:8000->80/tcp, :::8000->80/tcp 1runnginx_web_ngx_1
0d9492b1dcd0 php:7-fpm "docker-php-entrypoi…" 2 minutes ago Up 2 minutes 9000/tcp 1runnginx_web_php_1
[root@docker1 1-run-nginx]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------------
1runnginx_web_ngx_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8000->80/tcp,:::8000->80/tcp
1runnginx_web_php_1 docker-php-entrypoint php-fpm Up 9000/tcp
[root@docker1 1-run-nginx]#
⑥docker-compose更改剧本的端口:
1、将剧本的端口变为8001:
2、剧本书写:
[root@docker1 1-run-nginx]# vi docker-compose.yml
version: "3.3"
services:
web_ngx:
image: "nginx:1.20.2-alpine"
links:
- web_php
ports:
- "8001:80"
web_php:
image: "php:7-fpm"
3、构建容器:
[root@docker1 1-run-nginx]# docker-compose up -d
1runnginx_web_php_1 is up-to-date
Recreating 1runnginx_web_ngx_1 ... done
4、检查构建的容器:
[root@docker1 1-run-nginx]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------------
1runnginx_web_ngx_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8001->80/tcp,:::8001->80/tcp
1runnginx_web_php_1 docker-php-entrypoint php-fpm Up 9000/tcp
[root@docker1 1-run-nginx]#
⑦docker-compose更改剧本的服务名字:
1、将剧本的服务名字增加一个
2、剧本书写:
[root@docker1 1-run-nginx]# vi docker-compose.yml
version: "3.3"
services:
web_ngx1:
image: "nginx:1.20.2-alpine"
links:
- web_php
ports:
- "8001:80"
web_ngx2:
image: "nginx:1.20.2-alpine"
links:
- web_php
ports:
- "8002:80"
web_php:
image: "php:7-fpm"
3、构建容器:{因为之前的容器更改了一个服务的名字}所以再次启动的时候就会失败
[root@docker1 1-run-nginx]# docker-compose up -d
WARNING: Found orphan containers (1runnginx_web_ngx_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
1runnginx_web_php_1 is up-to-date
Creating 1runnginx_web_ngx1_1 ...
Creating 1runnginx_web_ngx1_1 ... error
Creating 1runnginx_web_ngx2_1 ... done
web_ngx1_1 (b670432ba46897af2db9404c1dedd50a3fe5d0e0f3663848e97da12b41bab820): Bind for 0.0.0.0:8001 failed: port is already allocated
ERROR: for web_ngx1 Cannot start service web_ngx1: driver failed programming external connectivity on endpoint 1runnginx_web_ngx1_1 (b670432ba46897af2db9404c1dedd50a3fe5d0e0f3663848e97da12b41bab820): Bind for 0.0.0.0:8001 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
4、再次构建容器:{会把错误的容器删掉,重新生成}
[root@docker1 1-run-nginx]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------------------------
1runnginx_web_ngx1_1 /docker-entrypoint.sh ngin ... Exit 128
1runnginx_web_ngx2_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8002->80/tcp,:::8002->80/tcp
1runnginx_web_php_1 docker-php-entrypoint php-fpm Up 9000/tcp
5、检查构建的容器:
[root@docker1 1-run-nginx]# docker-compose up -d --remove-orphans
Removing orphan container "1runnginx_web_ngx_1"
1runnginx_web_php_1 is up-to-date
Starting 1runnginx_web_ngx1_1 ...
Starting 1runnginx_web_ngx1_1 ... done
[root@docker1 1-run-nginx]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------------------
1runnginx_web_ngx1_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8001->80/tcp,:::8001->80/tcp
1runnginx_web_ngx2_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8002->80/tcp,:::8002->80/tcp
1runnginx_web_php_1 docker-php-entrypoint php-fpm Up 9000/tcp
[root@docker1 1-run-nginx]#
⑧docker-compose的启动顺序:
1、php先启动然后在启动nginx:
2、剧本书写:
[root@docker1 2-ngx-php]# cat docker-compose.yml
version: "3.3"
services:
web_ngx:
image: "nginx:1.20.2-alpine"
links:
- web_php
ports:
- "8001:80"
depends_on
- web_php
web_php:
image: "php:7-fpm"
[root@docker1 2-ngx-php]#
3、构建容器:
[root@docker1 2-ngx-php]# docker-compose up -d
4、检查构建的容器:
[root@docker1 2-ngx-php]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------
2ngxphp_web_ngx_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:80->80/tcp,:::80->80/tcp
2ngxphp_web_php_1 docker-php-entrypoint php-fpm Up 9000/tcp
[root@docker1 2-ngx-php]#
⑨docker-compose的数据卷的挂载:
1、准备挂载的几个文件
2、剧本书写:
[root@docker1 2-ngx-php]# cat docker-compose.yml
version: "3.3"
services:
web_ngx:
image: "nginx:1.20.2-alpine"
links:
- "web_php:php"
ports:
- "80:80"
depends_on:
- web_php
volumes:
- "/app/docker/kodexp/code:/app/code/kod/"
- "/app/docker/kodexp/conf/nginx.conf:/etc/nginx/nginx.conf"
- "/app/docker/kodexp/conf/kod.conf:/etc/nginx/conf.d/kod.conf"
web_php:
image: "php:7-fpm"
expose:
- "9000"
volumes:
- "/app/docker/kodexp/conf/www.conf:/usr/local/etc/php-fpm.d/www.conf"
- "/app/docker/kodexp/code:/app/code/kod/"
[root@docker1 2-ngx-php]#
3、构建容器:
[root@docker1 2-ngx-php]# docker-compose up -d
Recreating 2ngxphp_web_php_1 ... done
Recreating 2ngxphp_web_ngx_1 ... done
4、检查构建的容器:{要在构建的目录下运行,不然找不到}
[root@docker1 2-ngx-php]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------
2ngxphp_web_ngx_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:80->80/tcp,:::80->80/tcp
2ngxphp_web_php_1 docker-php-entrypoint php-fpm Up 9000/tcp
[root@docker1 2-ngx-php]#
[root@docker1 conf]# docker-compose -f /server/compose/2-ngx-php/docker-compose.yml ps
Name Command State Ports
---------------------------------------------------------------------------------------------
2ngxphp_web_ngx_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:80->80/tcp,:::80->80/tcp
2ngxphp_web_php_1 docker-php-entrypoint php-fpm Up 9000/tcp
[root@docker1 conf]#
5、报错解决:{File not found}
也要将站点的代码复制一份到php的目录下,因为php在解析动态文件的时候也得去这个路径下找代码,如果没写就会去默认的路径去找
6、浏览器登录测试:
⑩docker-compose和dockerfile:
1、准备目录
[root@docker1 3-tengine-restart]# ls -l
total 4848
-rw-r--r-- 1 root root 87 Jun 14 22:30 docker-compose.yml
-rw-r--r-- 1 root root 2023 Jun 14 22:22 Dockerfile
-rw-r--r-- 1 root root 4950457 Jun 14 22:22 restart.tar.gz
[root@docker1 3-tengine-restart]# pwd
/server/compose/3-tengine-restart
[root@docker1 3-tengine-restart]#
2、剧本书写:{dockerfile构建镜像、compose使用镜像启动容器、build 是指docker build的目录}
[root@docker1 3-tengine-restart]# cat docker-compose.yml
version: "3.3"
services:
tengine_restart:
build: .
ports:
- "8080:80"
[root@docker1 3-tengine-restart]#
[root@docker1 3-tengine-restart]# cat Dockerfile
#1 pull ubuntu image
FROM ubuntu:20.04 AS temp
LABEL maintainer="Tengine docker [email protected]" author="itter"
ENV Web_User="nginx"
ENV Web_Server="tengine"
ENV Web_version="2.3.2"
ENV Server_Dir="/app/tools/tengine-2.3.2"
ENV Server_Dir_Soft="/app/tools/tengine"
RUN sed -ri 's#archive.canonical.com|security.ubuntu.com#mirrors.aliyun.com#g' /etc/apt/sources.list \
&& apt-get update \
&& apt-get install -y wget libssl-dev make gcc pcre2-utils libpcre3-dev zlib1g-dev \
&& wget -P /tmp/ http://tengine.taobao.org/download/${Web_Server}-${Web_version}.tar.gz \
&& cd /tmp \
&& tar -xf ${Web_Server}-${Web_version}.tar.gz \
&& cd ${Web_Server}-${Web_version} \
&& ./configure --prefix=${Server_Dir}/ \
--user=${Web_User} \
--group=${Web_User} \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_mp4_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--add-module=modules/ngx_http_upstream_check_module/ \
--add-module=modules/ngx_http_upstream_session_sticky_module \
&& make \
&& make install \
&& groupadd ${Web_User} \
&& useradd -g ${Web_User} ${Web_User} \
&& ln -s ${Server_Dir} ${Server_Dir_Soft} \
&& ln -s ${Server_Dir_Soft}/sbin/nginx /sbin/
####################################################################
####开始创建最终镜像
FROM ubuntu:20.04
COPY --from=temp /app/ /app/
ADD restart.tar.gz /app/tools/tengine/html/
RUN sed -ri 's#archive.canonical.com|security.ubuntu.com#mirrors.aliyun.com#g' /etc/apt/sources.list \
&& apt-get update \
&& apt-get install -y libssl-dev pcre2-utils libpcre3-dev zlib1g-dev \
&& ln -s /app/tools/tengine/sbin/nginx /sbin/ \
&& groupadd nginx \
&& useradd -g nginx nginx \
&& rm -fr /tmp/* /var/cache/*
EXPOSE 80 443
CMD ["nginx","-g","daemon off;"]
[root@docker1 3-tengine-restart]#
3、构建容器:{因为之前已经构建了镜像,如果要重新构建就使用命令,其实不需要}
[root@docker1 3-tengine-restart]# docker-compose up -d
Successfully built 982d7b9813bd
Successfully tagged 3tenginerestart_tengine_restart:latest
WARNING: Image for service tengine_restart was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating 3tenginerestart_tengine_restart_1 ... done
4、检查构建的容器:
[root@docker1 3-tengine-restart]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------------------------------
3tenginerestart_tengine_restart_1 nginx -g daemon off; Up 443/tcp, 0.0.0.0:8080->80/tcp,:::8080->80/tcp
[root@docker1 3-tengine-restart]#
5、测试:{浏览器测试}
http://114.55.110.152:8080/