文章目录
顶级 services 配置
一、基本配置
vim docker-compose.yml
version: "3.8"
services:
h1:
image: "nginx"
container_name: docker-myhosts
[root@Docker1 compose]# docker-compose up -d
[root@Docker1 compose]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------
docker-myhosts /docker-entrypoint.sh ngin ... Up 80/tcp
二、Build
a.简单使用
[root@Docker-1 compose]# cat docker-compose.yml
version: "3.8"
services:
webapp:
build: .
[root@Docker-1 compose]# cat Dockerfile
FROM nginx
COPY ./index.html /usr/share/nginx/html/index.html
[root@Docker-1 compose]# echo Hello,neko > index.html
[root@Docker-1 compose]# ls
docker-compose.yml Dockerfile index.html
[root@Docker-1 compose]# docker-compose up -d
WARNING: Found orphan containers (docker-myhosts, host1) 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.
Building webapp
Step 1/2 : FROM nginx
---> f6d0b4767a6c
Step 2/2 : COPY ./index.html /usr/share/nginx/html/index.html
---> 236d0b8f3cf2
Successfully built 236d0b8f3cf2
Successfully tagged compose_webapp:latest
WARNING: Image for service webapp was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating compose_webapp_1 ... done
[root@Docker-1 compose]# docker images compose_webapp:latest
REPOSITORY TAG IMAGE ID CREATED SIZE
compose_webapp latest 236d0b8f3cf2 57 seconds ago 133MB
[root@Docker-1 compose]# docker-compose ps
Name Command State Ports
------------------------------------------------------------------
compose_webapp_1 /docker-entrypoint.sh ngin ... Up 80/tcp
b.构建lnmp环境
[root@Docker-1 neko]# mkdir mysql nginx php
[root@Docker-1 neko]# ls
docker-compose.yml Dockerfile mysql nginx php
[root@Docker-1 neko]# cp Dockerfile nginx/
[root@Docker-1 neko]# cp Dockerfile mysql/
[root@Docker-1 neko]# cp Dockerfile php/
[root@Docker-1 neko]# tree .
.
├── docker-compose.yml
├── Dockerfile
├── mysql
│ └── Dockerfile
├── nginx
│ ├── Dockerfile
│ └── index.html
└── php
└── Dockerfile
[root@Docker-1 neko]# cat docker-compose.yml
version: "3.8"
services:
webapp:
build: ./nginx
db:
build: ./mysql
php:
build: ./php
销毁之前的compose
docker-compose down
[root@Docker-1 neko]# cat nginx/Dockerfile
FROM nginx
COPY ./index.html /usr/share/nginx/html/index.html
[root@Docker-1 neko]# cat mysql/Dockerfile
FROM mysql
[root@Docker-1 neko]# cat php/Dockerfile
FROM php:7.4-fpm
构建 不启动
docker-compose build
查看项目
[root@Docker-1 neko]# docker images |grep neko
neko_webapp latest 236d0b8f3cf2 35 minutes ago 133MB
neko_db latest c8562eaf9d81 2 days ago 546MB
neko_php latest f5460fa2369d 9 days ago 405MB
报错解决
[root@Docker-1 compose]# docker-compose up -d
WARNING: Found orphan containers (myhost) 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.
解决办法:
docker-compose up -d --remove-orphans
三、Image
指定为镜像名称或镜像 ID。如果镜像在本地不存在,Compose 将会尝试拉取这个镜像。
image: ubuntu
image: orchardup/postgresql
image: a4bc65fd
[root@Docker-1 ~]# mkdir image-compose
[root@Docker-1 ~]# cd image-compose/
[root@Docker-1 image-compose]# cat docker-compose.yml
version: "3.8"
services:
neko-1:
image: nginx
[root@Docker-1 image-compose]# docker-compose up -d
Creating network "image-compose_default" with the default driver
Creating image-compose_neko-1_1 ... done
[root@Docker-1 image-compose]# docker-compose ps
Name Command State Ports
------------------------------------------------------------------------
image-compose_neko-1_1 /docker-entrypoint.sh ngin ... Up 80/tcp
如果同时指定了 image和 build, image 不在具有单独使用它的意义,而是指定了目前要构建的镜像的名称。也就是说 Compose 会使用 build 指令中指定的 Dockerfilel 构建的镜像,之后构建的镜像名称使用 image 中指定的名字 webapp:tag命名。
[root@Docker-1 image-compose]# cat Dockerfile
FROM nginx
[root@Docker-1 image-compose]# vim docker-compose.yml
[root@Docker-1 image-compose]# cat docker-compose.yml
version: "3.8"
services:
neko-1:
image: nginx
neko-2:
build: .
image: hello.c
[root@Docker-1 image-compose]# docker-compose up -d
image-compose_neko-1_1 is up-to-date
Creating image-compose_neko-2_1 ... done
[root@Docker-1 image-compose]#
四、Command
使用 command 可以覆盖容器启动后默认执行的命令。
command: bundle exec thin -p 3000
也可以写成类似 Dockerfile 中的格式:
command: [bundle, exec, thin, -p, 3000]
[root@Docker-1 ~]# mkdir command
[root@Docker-1 ~]# cd command/
[root@Docker-1 command]# cat docker-compose.yml
version: "3"
services:
app-1:
image: nginx
command: /bin/sh
container_name: nginx-sh
app-2:
image: nginx
container_name: nginx-off
[root@Docker-1 command]# docker-compose up -d
Creating network "command_default" with the default driver
Creating nginx-sh ... done
Creating nginx-off ... done
[root@Docker-1 command]# docker-compose ps
Name Command State Ports
------------------------------------------------------------
nginx-off /docker-entrypoint.sh ngin ... Up 80/tcp
nginx-sh /docker-entrypoint.sh /bin/sh Exit 0
五、Depends_on
解决容器的依赖、启动先后的问题。以下例子中会先启动容器 db 和 redis 再启动 web
扫描二维码关注公众号,回复: 12417308 查看本文章
version: '3'
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
注意:web 服务不会等待 redis 和 db 「完全启动」之后才启动。
六、Environment
[root@Docker-1 ~]# mkdir env
[root@Docker-1 ~]# cd env/
[root@Docker-1 env]# cat docker-compose.yml
version: "3.8"
service:
h1:
image: alpine
stdin_open: true # docker run -i
tty: true # docker run -i
environment:
user: neko
TEST:
[root@Docker-1 env]# docker run nginx env
HOSTNAME=0e58b69d59dc
HOME=/root
PKG_RELEASE=1~buster
NGINX_VERSION=1.19.6
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NJS_VERSION=0.5.0
PWD=/
七、Expose 和 Ports
暴露端口 expose
暴露端口,但不映射到宿主机,只被其他容器连接到此容器时使用。
仅可以指定容器内部的端口为参数
映射端口 ports
使用:
宿主端口:容器端口 (即:HOST:CONTAINER) 的格式格式,或者仅仅指定容器的端口(宿主将会随机选择端口)都可以。
[root@Docker-1 ~]# mkdir ports
[root@Docker-1 ~]# cd ports/
[root@Docker-1 ports]# cat docker-compose.yml
version: "3.8"
services:
web1:
image: nginx
ports:
- "9000:80" # 相当于 docker run -p 9000:80
[root@Docker-1 ports]# docker-compose up -d
Creating network "ports_default" with the default driver
Creating ports_web1_1 ... done
[root@Docker-1 ports]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------
ports_web1_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:9000->80/tcp
然后访问端口
curl 192.168.31.248:9000 #访问宿主机9000端口
八、Extra_hosts
[root@Docker-1 ~]# mkdir extra_hosts
[root@Docker-1 ~]# cd extra_hosts/
[root@Docker-1 extra_hosts]# cat docker-compose.yml
version: "3.8"
services:
web1:
image: nginx
ports:
- "9010:80" # 相当于 docker run -p 9010:80
extra_hosts:
- "web2:172.18.0.9"
- "www.nekoosu.com:1.2.3.4"
[root@Docker-1 extra_hosts]# docker-compose up -d
Creating extra_hosts_web1_1 ... done
进去系统发现 已添加hosts
[root@Docker-1 extra_hosts]# docker-compose exec web1 sh
# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.18.0.9 web2
1.2.3.4 www.nekoosu.com
172.28.0.2 ddc53752333e
九、Networks
[root@Docker-1 ~]# mkdir network
[root@Docker-1 ~]# cd network/
[root@Docker-1 network]# cat docker-compose.yml
version: "3.8"
services:
web1:
image: nginx
ports:
- "9020:80" # 相当于 docker run -p 9020:80
networks:
- web-net # 自定义网络的名称
web2:
image: centos
tty: true
stdin_open: true
networks:
- web-net # docker run --network=web-net
networks: # docker network create
web-net:
[root@Docker-1 network]# docker-compose up -d
Creating network "network_web-net" with the default driver
Pulling web2 (centos:)...
latest: Pulling from library/centos
7a0437f04f83: Already exists
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
Creating network_web1_1 ... done
Creating network_web2_1 ... done
查看是否有自定义的网络
[root@Docker-1 network]# docker network inspect network_web-net
"Containers": {
"c12d9c084743248e0f1c8c85d651f2c795ceec4ba3c0365ecd927c6ccaef3241": {
"Name": "network_web1_1",
"EndpointID": "063b66b7bba0346a9b2cab71958ed9b52083eab64c35e28476202ad4dd46d4c8",
"MacAddress": "02:42:ac:1d:00:02",
"IPv4Address": "172.29.0.2/16",
"IPv6Address": ""
},
"d325643ae6ae53597370a3b835f03ed786a4f918dfa4608aac940c7491791e7f": {
"Name": "network_web2_1",
"EndpointID": "a2feb44407bae619ed5d082e5eaf3a20c987c0bd616ab00d39ac351c8044353a",
"MacAddress": "02:42:ac:1d:00:03",
"IPv4Address": "172.29.0.3/16",
"IPv6Address": ""
查看是否通信
[root@Docker-1 network]# docker-compose exec web2 bash
[root@d325643ae6ae /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@d325643ae6ae /]# ping web1
PING web1 (172.29.0.2) 56(84) bytes of data.
64 bytes from network_web1_1.network_web-net (172.29.0.2): icmp_seq=1 ttl=64 time=0.073 ms
^C
--- web1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.073/0.073/0.073/0.000 ms
[root@d325643ae6ae /]# curl web1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title
...
[root@Docker-1 ~]# mkdir web-db-redis
[root@Docker-1 ~]# cd web-db-redis/
[root@Docker-1 web-db-redis]# cat docker-compose.yml
version: "3"
services:
web1:
image: centos
stdin_open: true
tty: true
ports:
- "9030:80"
tworks:
- net-web
net-db
db:
image: centos
stdin_open: true
tty: true
networks:
- net-db
redis:
image: redis:alpine
networks:
- net-db
networks:
net-db:
net-web:
[root@Docker-1 web-db-redis]# docker-compose up -d
Creating network "web-db-redis_net-web" with the default driver
Creating network "web-db-redis_net-db" with the default driver
Creating web-db-redis_web1_1 ... done
Creating web-db-redis_redis_1 ... done
Creating web-db-redis_db_1 ... done
[root@Docker-1 web-db-redis]# docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------
web-db-redis_db_1 /bin/bash Up
web-db-redis_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
web-db-redis_web1_1 /bin/bash Up 0.0.0.0:9030->80/tcp
检测网络连通性
[root@Docker-1 web-db-redis]# docker-compose exec web1 bash
[root@f416f6829256 /]# ping redis
PING redis (192.168.32.2) 56(84) bytes of data.
64 bytes from web-db-redis_redis_1.web-db-redis_net-db (192.168.32.2): icmp_seq=1 ttl=64 time=0.167 ms
64 bytes from web-db-redis_redis_1.web-db-redis_net-db (192.168.32.2): icmp_seq=2 ttl=64 time=0.051 ms
^C
--- redis ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 0.051/0.109/0.167/0.058 ms
[root@f416f6829256 /]# ping db
PING db (192.168.32.3) 56(84) bytes of data.
64 bytes from web-db-redis_db_1.web-db-redis_net-db (192.168.32.3): icmp_seq=1 ttl=64 time=0.065 ms
64 bytes from web-db-redis_db_1.web-db-redis_net-db (192.168.32.3): icmp_seq=2 ttl=64 time=0.051 ms
^C
--- db ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 0.051/0.058/0.065/0.007 ms
[root@f416f6829256 /]# ping web1
PING web1 (192.168.32.4) 56(84) bytes of data.
64 bytes from f416f6829256 (192.168.32.4): icmp_seq=1 ttl=64 time=0.035 ms
64 bytes from f416f6829256 (192.168.32.4): icmp_seq=2 ttl=64 time=0.031 ms
^C
--- web1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.031/0.033/0.035/0.002 ms
1.指定 ip
基于之前的操作
[root@Docker-1 web-db-redis]# cd
[root@Docker-1 ~]# cd network/
[root@Docker-1 network]# docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------
network_web1_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:9020->80/tcp
network_web2_1 /bin/bash Up
[root@Docker-1 network]# docker-compose exec web1 hostname -i
172.29.0.2
[root@Docker-1 network]# docker-compose exec web2 hostname -i
172.29.0.3
指定 ipv4 和 ipv6
[root@Docker-1 network]# cat docker-compose.yml
version: "3.8"
services:
web1:
image:
tty: true
stdin_open: true
ports:
- "9020:80" # 相当于 docker run -p 9020:80
networks:
web-net: # 自定义网络的名称
ipv4_address: 172.16.238.10
ipv6_address: 2001:3984:3989::10
web2:
image: centos
tty: true
stdin_open: true
networks:
- web-net # docker run --network=web-net
networks: # docker network create
web-net: # 名称
driver: bridge
enable_ipv6: true # 是否用 ipv6 是 true 否 force
ipam:
driver: default # 网络模式
config:
- subnet: 172.16.238.0/24 # 指定网络号 可以修改 与ipv4 对应
gateway: 172.16.238.1
- subnet: 2001:3984:3989::/64
gateway: 2001:3984:3989::1
重新创建网络
[root@Docker-1 network]# docker-compose down
Stopping network_web2_1 ... done
Stopping network_web1_1 ... done
Removing network_web2_1 ... done
Removing network_web1_1 ... done
Removing network network_web-net
[root@Docker-1 network]# docker-compose up -d
Creating network "network_web-net" with driver "bridge"
Creating network_web2_1 ... done
Creating network_web1_1 ... done
[root@Docker-1 network]# docker-compose exec web1 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
60: eth0@if61: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:10:ee:0a brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.16.238.10/24 brd 172.16.238.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 2001:3984:3989::10/64 scope global nodad
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe10:ee0a/64 scope link
valid_lft forever preferred_lft forever
[root@Docker-1 network]# docker-compose exec web2 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
54: eth0@if55: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:10:ee:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.16.238.2/24 brd 172.16.238.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 2001:3984:3989::2/64 scope global nodad
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe10:ee02/64 scope link
valid_lft forever preferred_lft forever
2.使用现有网络
如果您希望容器加入现有网络,请使用以下external 选项.
version: "3"
servers:
host1:
image: centos
stdin_open: true
tty: true
networks:
# 此项目中的服务使用的默认网络,此网络不会被创建
# 实际会使用 my-pre-existing-network
default:
external:
name: my-pre-existing-network
这种情况下,Compose不会尝试创建名为 [projectname]_default 的网络,而是查找名为 my-pre-existing-network 的网络,并将应用程序的容器连接到该网络。
也可以把默认 default 网络改成其他的名称,比如
version: "3.8"
services:
host1:
image: centos
stdin_open: true
tty: true
networks:
neko:
networks:
neko:
external:
name: my-pre-existing-network
十、Aliases
网络上此服务的别名 备用名称
格式:
version: "3"
services:
host1:
image: centos
expose:
- "4500"
stdin_open: true
networks:
network-1:
aliases:
- h1
network-2:
aliases:
- h2
host2:
image: centos
stdin_open: true
networks:
network-1:
host3:
image: centos
stdin_open: true
networks:
network-2:
networks:
h1
h2
[root@docker-host aliases]# docker-compose ps
Name Command State Ports
-------------------------------------------
aliases_host1_1 /bin/bash Up 4500/tcp
aliases_host2_1 /bin/bash Up
aliases_host3_1 /bin/bash Up
十一、Healthcheck
通过命令检查容器是否健康运行。
[root@Docker-1 ~]# mkdir healthcheck
[root@Docker-1 ~]# cd healthcheck/
[root@Docker-1 healthcheck]# cat docker-compose.yml
version: "3"
services:
web1:
image: nginx
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 1m30s
timeout: 10s
retries: 3
[root@Docker-1 healthcheck]# docker-compose up -d
Creating network "healthcheck_default" with the default driver
Creating healthcheck_web1_1 ... done
[root@Docker-1 healthcheck]# docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------
healthcheck_web1_1 /docker-entrypoint.sh ngin ... Up (health: starting) 80/tcp
等到达设定时间 1分30秒 后
[root@Docker-1 healthcheck]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------
healthcheck_web1_1 /docker-entrypoint.sh ngin ... Up (healthy) 80/tcp
拓扑:
删除Linux文件中的 中文字符
ls | grep -vP '[a-z0-9]' | rm -rf `xargs`