文章目录
- 1 docker存储
- 1.1 目录挂载
- 2.1卷映射
- 2.1.1卷映射和目录挂载的区别
- 2.1.2卷映射的使用
- 2 docker网络
- 2.1查看docker的默认网络
- 2.2查看容器的IP
- 2.3容器互通
- 2.4自定义网络
- 2.4.1 创建自定义网络
- 2.4.2创建容器的时候加入到自定义的网络
- 2.4.3使用域名进行容器之间的访问
- 2.4.4redis主从同步集群
- 2.4.4.1启动redis主节点容器
- 2.4.4.2启动redis从节点容器
1 docker存储
1.1 目录挂载
目录挂载的作用就好像是U盘,盘符里面的内容和电脑里面展示的是一样的。如果容器挂了,那么重启开启容器并且指定原来的盘符映射到容器就可以了。
- -d:后台运行
- -p:主机port:容器port。这样外部就可以访问档容器了
- –name:指定容器的名字
- -v:主机目录:容器目录。这样就会把容器里面的数据保存到主机了,即使容器崩了,也木事。
[root@root ~]# docker run -d -p 80:80 --name mynginx -v /app/nghtml:/usr/share/nginx/html nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
09f376ebb190: Already exists
5529e0792248: Already exists
9b3addd3eb3d: Already exists
57910a8c4316: Already exists
7b5f78f21449: Already exists
b7923aa4e8a6: Already exists
785625911f12: Already exists
Digest: sha256:0f04e4f646a3f14bf31d8bc8d885b6c951fdcf42589d06845f64d18aec6a3c4d
Status: Downloaded newer image for nginx:latest
7236550f01944f5bcf235404fc455a46e986cbc879a764d054a08b5d0d92d0a2
[root@root ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7236550f0194 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp mynginx
[root@root ~]#
2.1卷映射
2.1.1卷映射和目录挂载的区别
若将nginx配置文件进行目录挂载,那么会启动报错nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
。因为目录挂载容易会以主机的目录为主,首先进行加载。
如果在容器启动就进行加载的文件,进行卷映射。卷映射会在容器启动的时候将启动的配置文件同步到主机的卷中
[root@root ~]# docker run -d -p 88:80 --name app01 -v /app/nghtml:/usr/share/nginx/htm -v /app/nghtml:/etc/nginx nginx
45d51b3d7733481ad6deb7c5d7321915ba867850021b6e3eeed67bfe5437a654
[root@root ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45d51b3d7733 nginx "/docker-entrypoint.…" 12 seconds ago Exited (1) 12 seconds ago app01
7236550f0194 nginx "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp mynginx
[root@root ~]# docker logs 45d51b3d7733
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/06/05 01:44:31 [emerg] 1#1: open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
[root@root ~]#
2.1.2卷映射的使用
删除容器卷不会删除
[root@root ~]# docker run -d -p 89:80 -v /app/nghtml:/usr/share/nginx/html -v ngconfig:/etc/nginx --name app02 nginx
514d71e9fa6b5e96442c04a5ea69401e73102191329647c728bf92d6d5e334c7
[root@root ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
514d71e9fa6b nginx "/docker-entrypoint.…" 4 seconds ago Up 4 seconds 0.0.0.0:89->80/tcp, :::89->80/tcp app02
7236550f0194 nginx "/docker-entrypoint.…" 34 minutes ago Up 34 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp mynginx
[root@root ~]#
查看卷的位置:/var/lib/docker/volumes/ngconfig(卷名)
docker默认目录
[root@root ~]# cd /var/lib/docker/volumes/ngconfig(卷名)
[root@root ngconfig]# ls
_data
[root@root ngconfig]# cd _data/
[root@root _data]# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
[root@root _data]#
[root@root _data]# docker volume ls 查看所有卷的列表
DRIVER VOLUME NAME
local ngconfig
[root@root _data]#
查看某个卷的详情
[root@root _data]# docker volume inspect ngconfig (卷名)
[
{
"CreatedAt": "2024-06-05T09:57:51+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/ngconfig/_data",
"Name": "ngconfig",
"Options": null,
"Scope": "local"
}
]
[root@root _data]#
2 docker网络
docker在安装之后就会有一个默认网络(docker0),容器在启动之后就会加入这个默认的安排
2.1查看docker的默认网络
[root@root _data]# 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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:16:3e:06:4d:35 brd ff:ff:ff:ff:ff:ff
inet 172.28.216.6/20 brd 172.28.223.255 scope global dynamic eth0
valid_lft 315352185sec preferred_lft 315352185sec
inet6 fe80::216:3eff:fe06:4d35/64 scope link
valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:e1:74:a5:bf brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:e1ff:fe74:a5bf/64 scope link
valid_lft forever preferred_lft forever
5: veth873139c@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 9a:cc:07:b8:51:aa brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::98cc:7ff:feb8:51aa/64 scope link
valid_lft forever preferred_lft forever
11: veth1b36288@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether ea:38:4b:e5:22:b3 brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet6 fe80::e838:4bff:fee5:22b3/64 scope link
valid_lft forever preferred_lft forever
[root@root _data]#
2.2查看容器的IP
查看容器的IP,使用一下命令,查看 "Networks":
中的 "Gateway": "172.17.0.1", "IPAddress": "172.17.0.3",
[root@root _data]# docker inspect app02
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"MacAddress": "02:42:ac:11:00:03",
"NetworkID": "5c06bf39edc223eda9311e67b5b0d023806ed375e8e24e8672be832f691b908b",
"EndpointID": "1ecef5a2c484b5823319b003a248ce88b5b847f16cdf88cd889887eaf1a57679",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
2.3容器互通
docker为每一个容器分配一个ip,使用ip+port可以使容器之间相互访问。
[root@root _data]# docker exec -it mynginx bash
root@7236550f0194:/# curl http://172.17.0.3:80
2.4自定义网络
原因:容器的ip不固定,创建一个新的网络,使用域名的方式进行访问
2.4.1 创建自定义网络
[root@root _data]# docker network create mynet
55cff593988b4e0de23ed26924d1f4e011a53f9d8b27ee4a5b98bc7ea6870958
[root@root _data]# docker network ls
NETWORK ID NAME DRIVER SCOPE
5c06bf39edc2 bridge bridge local
1776efe50fa7 host host local
55cff593988b mynet bridge local
5297b84dfd13 none null local
[root@root _data]#
2.4.2创建容器的时候加入到自定义的网络
[root@root _data]# docker run -d --name app01 --network mynet -p 81:80 nginx
1c0baf2a2666d839add230effa4530b5e8ef7123be0573e7a6a686276f84a9b0
[root@root _data]# docker run -d --name app02 --network mynet -p 82:80 nginx
056416a0c89f8e9ca771c7c5f301b1d5e0312afa24cc1815d895b232bf724258
[root@root _data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
056416a0c89f nginx "/docker-entrypoint.…" 5 seconds ago Up 5 seconds 0.0.0.0:82->80/tcp, :::82->80/tcp app02
1c0baf2a2666 nginx "/docker-entrypoint.…" 19 seconds ago Up 19 seconds 0.0.0.0:81->80/tcp, :::81->80/tcp app01
[root@root _data]#
2.4.3使用域名进行容器之间的访问
因为使用ip进行容器之间的访问ip不固定。所以在创建容器的时候,指定加入自定义网络,那么就可以使用固定的域名进行访问了,这个域名就是http://app02(容器名):80
[root@root ~]# docker exec -it app01 bash
root@1c0baf2a2666:/# curl http://app02:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@1c0baf2a2666:/#
2.4.4redis主从同步集群
2.4.4.1启动redis主节点容器
[root@root ~]# docker run -d -p 6379:6379 --name redis01 --network mynet -v /app/rd1:/bitnami/reids/data -e REDIS_REPLICATION_MODE=master -e REDIS_PASSWORD=123456 bitnami/redis
Unable to find image 'bitnami/redis:latest' locally
latest: Pulling from bitnami/redis
44f3255d2f6a: Pull complete
Digest: sha256:5261cae9e4076b75d114e6bb032a0699c50b004ea06a680a5304c4c08d286adb
Status: Downloaded newer image for bitnami/redis:latest
da79f3d15645162d71a0bd8891c5e55271a20332de052f2371b4f0333413c93f
[root@root ~]#
2.4.4.2启动redis从节点容器
[root@root ~]# docker run -d -p 6380:6379 --name redis02 --network mynet \
> -v /app/rd2:/bitnami/redis/data \
> -e REDIS_REPLICATION_MODE=slave \
> -e REDIS_MASTER_HOST=redis01 \
> -e REDIS_MASTER_PORT_NUMBER=6379 \
> -e REDIS_MASTER_PASSWORD=123456 \
> -e REDIS_PASSWORD=123456 \
> bitnami/redis
57aec20a231bf85265036efd1374801ef0cbdbf5a28e5c25d2e4d4c92767b18b
小插曲,redis02启动报错。1:S 05 Jun 2024 04:59:18.559 # Can't open or create append-only dir appendonlydir: Permission denied
[root@root ~]# docker logs redis02
redis 04:59:18.42 INFO ==>
redis 04:59:18.42 INFO ==> Welcome to the Bitnami redis container
redis 04:59:18.42 INFO ==> Subscribe to project updates by watching https://github.com/bitnami/containers
redis 04:59:18.42 INFO ==> Submit issues and feature requests at https://github.com/bitnami/containers/issues
redis 04:59:18.42 INFO ==> Upgrade to Tanzu Application Catalog for production environments to access custom-configured and pre-packaged software components. Gain enhanced features, including Software Bill of Materials (SBOM), CVE scan result reports, and VEX documents. To learn more, visit https://bitnami.com/enterprise
redis 04:59:18.42 INFO ==>
redis 04:59:18.43 INFO ==> ** Starting Redis setup **
redis 04:59:18.44 INFO ==> Initializing Redis
redis 04:59:18.45 INFO ==> Setting Redis config file
redis 04:59:18.47 INFO ==> Configuring replication mode
redis 04:59:18.53 INFO ==> ** Redis setup finished! **
redis 04:59:18.54 INFO ==> ** Starting Redis **
1:C 05 Jun 2024 04:59:18.558 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:C 05 Jun 2024 04:59:18.558 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 05 Jun 2024 04:59:18.558 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 05 Jun 2024 04:59:18.558 * Configuration loaded
1:S 05 Jun 2024 04:59:18.558 * monotonic clock: POSIX clock_gettime
1:S 05 Jun 2024 04:59:18.559 * Running mode=standalone, port=6379.
1:S 05 Jun 2024 04:59:18.559 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:S 05 Jun 2024 04:59:18.559 * Server initialized
1:S 05 Jun 2024 04:59:18.559 # Can't open or create append-only dir appendonlydir: Permission denied
修改文件夹的权限
[root@root app]# ll
总用量 12
drwxr-xr-x 2 root root 4096 6月 5 09:23 nghtml
drwxr-xr-x 2 root root 4096 6月 5 12:51 rd1
drwxr-xr-x 2 root root 4096 6月 5 12:59 rd2
[root@root app]# ^C
[root@root app]# ^C
[root@root app]# chmod 777 rd2
[root@root app]# ll
总用量 12
drwxr-xr-x 2 root root 4096 6月 5 09:23 nghtml
drwxr-xr-x 2 root root 4096 6月 5 12:51 rd1
drwxrwxrwx 2 root root 4096 6月 5 12:59 rd2
重启redis02
[root@root app]# docker restart redis02
redis02
[root@root app]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57aec20a231b bitnami/redis "/opt/bitnami/script…" 7 minutes ago Up 3 seconds 0.0.0.0:6380->6379/tcp, :::6380->6379/tcp redis02
da79f3d15645 bitnami/redis "/opt/bitnami/script…" 15 minutes ago Up 15 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis01
[root@root app]#