首页 > 其他分享 >docker compose

docker compose

时间:2022-10-22 21:02:47浏览次数:73  
标签:web docker1 compose docker php root

(一)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}

docker compose_docker



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的指令:

docker compose_php_02

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、浏览器登录测试:

​http://114.55.110.152/​




⑩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/​



标签:web,docker1,compose,docker,php,root
From: https://blog.51cto.com/ipran/5786115

相关文章

  • docker网络
    docker启动后,会产生一个名为docker0的虚拟网桥常用基本命令查看帮助信息[root@docker~]#dockernetwork--help查看网络dockernetworkls查看网络源数据......
  • SpringBoot微服务打包Docker镜像
    1.构建sprintboot项目2.打包应用3.编写dockerfileidea下载docker插件(可以高亮)4.构建镜像先把之前镜像都移除dockerrm-f$(dockerps-qa)在Linuxhome目......
  • docker删除镜像
    docker要删除镜像,先要删除依赖它的容器1.删除容器dockerps#查看正在运行的容器dockerps-a#查看所有容器dockerrmcontainer_id#删除容器2.删除镜像dockeri......
  • windows安装docker
    安装DockerDesktopwindows10及以上版本可以直接安装DockerDesktop(官方推荐)。根据提示下载linuxkernelupdate,双击安装即可。更换中国源{"registry-mirrors":[......
  • Redis集群部署实战(docker版)
      先创建一个网卡dockernetworkcreateredis--subnet172.38.0.0/16通过脚本创建六个redis配置forportin$(seq16);domkdir-p/mydata/redis/node......
  • docker安装zabbix6.0的ZBX状态为红色
    docker安装zabbix6.0详细方法参照官网https://www.zabbix.com/documentation/6.0/en/manual/installation/containers如果出现这种报错,请修改zabbix-agent配置文件中的se......
  • dremio 23 版本docker 镜像jdk 说明
    实际上dremio官方在232版本的relasenote中已经说明了关于jdk的处理,在看了最新docker镜像也可以印证此说明参考处理22版本的  23版本的  说明目......
  • CentOS 安装 Docker+ Aliyun 镜像
     1.使用一键安装命令curl-fsSLhttps://get.docker.com|bash-sdocker--mirrorAliyun  2.配置阿里云镜像,进入网站https://cr.console.aliyun.com/cn-ha......
  • docker安装redis(win10已安装Docker Desktop)
    参考文档:RunRedisStackonDocker|Redis如图所示:  终端中运行以下命令:dockerrun-d--nameredis-stack-p6379:6379-p8001:8001redis/redis-stack:late......
  • docker学习
    1.安装官方地址一键安装:curl-fsSLhttps://get.docker.com|bash-sdocker--mirrorAliyun或国内daocloud一键安装:curl-sSLhttps://get.daocloud.io/docker......