下载镜像只是相当于将软件下载下来安装好,但是并不代表把它运行起来,类似于root@ubuntu2204:~# apt install nginx = docker pull nginx ,然而进行运行起来就变成了容器,镜像只是模板文件,容器复制一份模板文件生成相关进程对外提供服务,随着运行的时间推移容器除了模板文件还会生成别的文件。
root@ubuntu2204:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be01627007fb nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp priceless_nightingale
8dd8f5328dff nginx:latest "/docker-entrypoint.…" 34 minutes ago Up 10 minutes 80/tcp boring_goldstine
root@ubuntu2204:~# ps axu|grep nginx
root 6576 0.0 0.1 8856 5516 ? Ss 12:53 0:00 nginx: master process nginx -g daemon off;
systemd+ 6628 0.0 0.0 9276 2672 ? S 12:53 0:00 nginx: worker process
systemd+ 6629 0.0 0.0 9276 2672 ? S 12:53 0:00 nginx: worker process
root 6655 0.0 1.2 1273784 49348 pts/6 Sl+ 13:02 0:00 docker run nginx:latest
root 6724 0.0 0.1 8856 5508 ? Ss 13:02 0:00 nginx: master process nginx -g daemon off;
systemd+ 6774 0.0 0.0 9276 2588 ? S 13:02 0:00 nginx: worker process
systemd+ 6775 0.0 0.0 9276 2588 ? S 13:02 0:00 nginx: worker process
root 6908 0.0 0.0 6608 2264 pts/8 R+ 13:08 0:00 grep --color=auto nginx
容器是一个在主机上运行的进程 ,主机可以是本地主机,也可以是远程主机。运行的容器进程被隔离,容器有自己的文件系统、自己的网络和自己的 独立于主机的独立进程树。
image:文件模板,仅是文件,只消耗磁盘空间,静态概念;container:复制模板文件生成新的文件和进程,即消耗磁盘空间也消耗内存空间,动态概念,有生命期。
root@ubuntu2204:~# docker container --help
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
root@ubuntu2204:~#
容器启动流程
Docker----Docker容器的启动流程:https://developer.aliyun.com/article/920837
为了后续做实验,将Docker数据根目录更改为/data/docker ,和添加镜像加速器。
root@ubuntu2204:~# docker info
Client:
Context: default
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.12
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
Default Runtime: runc
Init Binary: docker-init
containerd version:
runc version:
init version:
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.0-43-generic
Operating System: Ubuntu 22.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.8GiB
Name: ubuntu2204
ID: KZWM:XV2H:TECS:YALE:RV7H:IMVK:BITA:QTRO:52DQ:POVV:WL5I:YQVO
Docker Root Dir: /var/lib/docker #默认数据根目录
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
#停止Dokcer服务
root@ubuntu2204:~# sudo systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
docker.socket
root@ubuntu2204:~# sudo systemctl stop docker.socket
root@ubuntu2204:~# sudo systemctl stop containerd
#转移 root 目录为/data/docker
root@ubuntu2204:~# mkdir -p /data/docker
root@ubuntu2204:~# mv /var/lib/docker/ /data/
#编写 daemon.json,添加如下内容
root@ubuntu2204:~# sudo vim /etc/docker/daemon.json
{
"data-root":"/data/docker/"
}
#重启docker服务
root@ubuntu2204:~# sudo systemctl start docker
root@ubuntu2204:~# docker info
Client:
Context: default
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.12
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version:
runc version:
init version:
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.0-43-generic
Operating System: Ubuntu 22.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.8GiB
Name: ubuntu2204
ID: KZWM:XV2H:TECS:YALE:RV7H:IMVK:BITA:QTRO:52DQ:POVV:WL5I:YQVO
Docker Root Dir: /data/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
root@ubuntu2204:~#
添加镜像加速器如果之前有了别的定义,也写在大括号里面用逗号隔开。
root@ubuntu2204:~# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://uietgfqt.mirror.aliyuncs.com"]
}
root@ubuntu2204:~# sudo systemctl daemon-reload
root@ubuntu2204:~# sudo systemctl restart docker
root@ubuntu2204:~# sudo systemctl daemon-reload ; sudo systemctl restart docker
root@ubuntu2204:~# docker info
Client:
Context: default
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.12
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version:
runc version:
init version:
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.0-43-generic
Operating System: Ubuntu 22.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.8GiB
Name: ubuntu2204
ID: KZWM:XV2H:TECS:YALE:RV7H:IMVK:BITA:QTRO:52DQ:POVV:WL5I:YQVO
Docker Root Dir: /data/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://uietgfqt.mirror.aliyuncs.com/
Live Restore Enabled: false
root@ubuntu2204:~#
root@ubuntu2204:~# cat /etc/docker/daemon.json
{
"data-root":"/data/docker/",
"registry-mirrors": ["https://uietgfqt.mirror.aliyuncs.com"]
}
root@ubuntu2204:~#
docker create
创建容器实际上就是复制image,命令docker create
用法
oot@ubuntu2204:~# docker create --help
Usage: docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Create a new container
Options:
。。。。
--name string Assign a name to the container #在创建容器时指定容器名称
示例
root@ubuntu2204:~# du -sh /data/docker/
244K /data/docker/
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
root@ubuntu2204:~# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 12 months ago 141MB
root@ubuntu2204:~# docker create nginx:latest
8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8
root@ubuntu2204:~# du -sh /data/docker/
151M /data/docker/
root@ubuntu2204:~#
docker -ps
默认显示运行状态的容器,
-a 显示所有状态的容器。
root@ubuntu2204:~# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states)
(default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
root@ubuntu2204:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dd8f5328dff nginx:latest "/docker-entrypoint.…" 10 minutes ago Created boring_goldstine
容器本质上进程,创建容器只是复制image,但是还没有运行起来,所以看不到进程。此时,并且数据根目录大小也没变化。
root@ubuntu2204:~# ps aux|grep nginx
root 6484 0.0 0.0 6476 2260 pts/6 S+ 12:44 0:00 grep --color=auto nginx
root@ubuntu2204:~# du -sh /data/docker
151M /data/docker
root@ubuntu2204:~# ll /data/docker/containers/
total 12
drwx--x--- 3 root root 4096 Jan 3 12:29 ./
drwx--x--- 13 root root 4096 Jan 3 12:14 ../
drwx--x--- 3 root root 4096 Jan 3 12:29 8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8/
root@ubuntu2204:~# tree /data/docker/containers/8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8/
/data/docker/containers/8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8/
├── checkpoints
├── config.v2.json
└── hostconfig.json
1 directory, 2 files
root@ubuntu2204:~#
示例:指定名字
docker start
docker start 容器ID或名称
root@ubuntu2204:~# docker start --help
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
Options:
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys string Override the key sequence for detaching a container
-i, --interactive Attach container's STDIN
root@ubuntu2204:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dd8f5328dff nginx:latest "/docker-entrypoint.…" 23 minutes ago Created boring_goldstine
root@ubuntu2204:~# du -sh /data/docker
151M /data/docker
root@ubuntu2204:~# docker start boring_goldstine
boring_goldstine
root@ubuntu2204:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dd8f5328dff nginx:latest "/docker-entrypoint.…" 23 minutes ago Up 4 seconds 80/tcp boring_goldstine
容器启动之后数据目录变大,也增加了一些文件。
root@ubuntu2204:~# du -sh /data/docker
298M /data/docker
root@ubuntu2204:~# tree /data/docker/containers/8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8/
/data/docker/containers/8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8/
├── 8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8-json.log
├── checkpoints
├── config.v2.json
├── hostconfig.json
├── hostname
├── hosts
├── mounts
├── resolv.conf
└── resolv.conf.hash
2 directories, 7 files
root@ubuntu2204:~#
root@ubuntu2204:~# ls /data/docker/volumes/
backingFsBlockDev metadata.db
root@ubuntu2204:~# ls /data/docker/
buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
root@ubuntu2204:~# ls /data/docker/overlay2/
274af8f4099d035812b99a2d45652cd4dca273d7db9bb3d9eada764eaefd5b40 73cbb9671db1515f18bdf3e274b9cf836ff3b9c925adb438f79da63e1200c056
36980a6dd0f318d40fa2ceb48b4d27e81833c95ea89f94776c868f32bd1a34a9 8fef2c8d09ae78da14b279bc1ab1a07de713c530dd8ec53c76c5dd9573c29f11
4511584cd776f33c45876483b8cd80962695b3526cf34ffbfef9b524aa73e121 9ba8ba2a6fece861e26c40f2c3ab77806f3d9496eccc208a6b6ef19e1af9506d
4511584cd776f33c45876483b8cd80962695b3526cf34ffbfef9b524aa73e121-init l
657928bd618dd8de2b94bc3915f31eb124dde49b36a5f0c568029ec936dadac3
root@ubuntu2204:~# du -sh /data/docker/overlay2/274af8f4099d035812b99a2d45652cd4dca273d7db9bb3d9eada764eaefd5b40/
28K /data/docker/overlay2/274af8f4099d035812b99a2d45652cd4dca273d7db9bb3d9eada764eaefd5b40/
root@ubuntu2204:~# ls /data/docker/overlay2/ -t
4511584cd776f33c45876483b8cd80962695b3526cf34ffbfef9b524aa73e121 73cbb9671db1515f18bdf3e274b9cf836ff3b9c925adb438f79da63e1200c056
4511584cd776f33c45876483b8cd80962695b3526cf34ffbfef9b524aa73e121-init 8fef2c8d09ae78da14b279bc1ab1a07de713c530dd8ec53c76c5dd9573c29f11
l 657928bd618dd8de2b94bc3915f31eb124dde49b36a5f0c568029ec936dadac3
36980a6dd0f318d40fa2ceb48b4d27e81833c95ea89f94776c868f32bd1a34a9 9ba8ba2a6fece861e26c40f2c3ab77806f3d9496eccc208a6b6ef19e1af9506d
274af8f4099d035812b99a2d45652cd4dca273d7db9bb3d9eada764eaefd5b40
root@ubuntu2204:~# du -sh /data/docker/overlay2/4511584cd776f33c45876483b8cd80962695b3526cf34ffbfef9b524aa73e121
148M /data/docker/overlay2/4511584cd776f33c45876483b8cd80962695b3526cf34ffbfef9b524aa73e121
root@ubuntu2204:~#
用同一个镜像在创建一个容器,也会在复制一份镜像文件。
root@ubuntu2204:~# du -sh /data/docker/
298M /data/docker/
root@ubuntu2204:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dd8f5328dff nginx:latest "/docker-entrypoint.…" 31 minutes ago Up 7 minutes 80/tcp boring_goldstine
root@ubuntu2204:~# docker run nginx:latest
/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: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/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
2023/01/03 13:02:34 [notice] 1#1: using the "epoll" event method
2023/01/03 13:02:34 [notice] 1#1: nginx/1.21.5
2023/01/03 13:02:34 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/01/03 13:02:34 [notice] 1#1: OS: Linux 5.15.0-43-generic
2023/01/03 13:02:34 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/01/03 13:02:34 [notice] 1#1: start worker processes
2023/01/03 13:02:34 [notice] 1#1: start worker process 31
2023/01/03 13:02:34 [notice] 1#1: start worker process 32 #默认前台运行,-d后台运行
root@ubuntu2204:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be01627007fb nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp priceless_nightingale
8dd8f5328dff nginx:latest "/docker-entrypoint.…" 34 minutes ago Up 10 minutes 80/tcp boring_goldstine
root@ubuntu2204:~# ps axu|grep nginx
root 6576 0.0 0.1 8856 5516 ? Ss 12:53 0:00 nginx: master process nginx -g daemon off;
systemd+ 6628 0.0 0.0 9276 2672 ? S 12:53 0:00 nginx: worker process
systemd+ 6629 0.0 0.0 9276 2672 ? S 12:53 0:00 nginx: worker process
root 6655 0.0 1.2 1273784 49348 pts/6 Sl+ 13:02 0:00 docker run nginx:latest
root 6724 0.0 0.1 8856 5508 ? Ss 13:02 0:00 nginx: master process nginx -g daemon off;
systemd+ 6774 0.0 0.0 9276 2588 ? S 13:02 0:00 nginx: worker process
systemd+ 6775 0.0 0.0 9276 2588 ? S 13:02 0:00 nginx: worker process
root 6908 0.0 0.0 6608 2264 pts/8 R+ 13:08 0:00 grep --color=auto nginx
root@ubuntu2204:~# du -sh /data/docker/
445M /data/docker/
docker run=复制image生成容器docker create+docker start,容器的创建实际上就是复制image文件。
运行容器基本命令采用以下形式:docker run ,容器创建并启动
容器必须要有一个前台运行的进程,才能不退出。
–privileged 向此容器授予扩展权限,让容器可以控制宿主机,这个比较危险,谨慎使用。
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
#该命令必须指定要生成容器的 IMAGE。
#选项:
-i, --interactive Keep STDIN open even if not attached,通常和-t一起使用
-t, --tty 分配pseudo-TTY,通常和-i一起使用,注意对应的容器必须运行shell才支持进入
-d, --detach Run container in background and print container ID,台后运行,默认前台
--volume,-v 绑定装载卷
--rm 容器退出时自动移除容器
--publish,-p 将容器的端口发布到主机
--publish-all,-P 将所有公开的端口发布到随机端口
--env,-e 设置环境变量
示例
docker run -d -p 80:80 my_image service nginx start
#Ubuntu默认是前台运行,运行就退出,-i交互,-t分配终端
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 12 months ago 141MB
ubuntu jammy 9d28ccdc1fc7 13 months ago 76.3MB
root@ubuntu2204:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be01627007fb nginx:latest "/docker-entrypoint.…" 2 hours ago Up 9 minutes 80/tcp priceless_nightingale
root@ubuntu2204:~# docker run ubuntu:jammy
root@ubuntu2204:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
feac00c812fa ubuntu:jammy "bash" 3 seconds ago Exited (0) 3 seconds ago eager_shaw
be01627007fb nginx:latest "/docker-entrypoint.…" 2 hours ago Up 10 minutes 80/tcp priceless_nightingale
root@ubuntu2204:~#
#容器Ubuntu交互式
root@ubuntu2204:~# docker run -it ubuntu:jammy
root@52c36c5079d5:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@52c36c5079d5:/# pwd
/
root@52c36c5079d5:/#
root@52c36c5079d5:/# exit
exit
root@ubuntu2204:~#
#容器Ubuntu后台运行,-d
#docker命令后台运行,-d
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.16.2 9c6f07244728 4 months ago 5.54MB
nginx latest 605c77e624dd 12 months ago 141MB
ubuntu jammy 9d28ccdc1fc7 13 months ago 76.3MB
root@ubuntu2204:~# docker run -d alpine:3.16.2 sleep 100
b7788f0d576400168d52c2b748ae3aad0b900ac729db4e576a4ad2d21781005f
root@ubuntu2204:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7788f0d5764 alpine:3.16.2 "sleep 100" 14 seconds ago Up 12 seconds thirsty_williamson
52c36c5079d5 ubuntu:jammy "bash" 11 hours ago Up 5 minutes compassionate_albattani
be01627007fb nginx:latest "/docker-entrypoint.…" 13 hours ago Up 14 minutes 80/tcp priceless_nightingale
root@ubuntu2204:~#
容器启动后,默认处于预定义的NAT网络中,所以外部网络的主机无法直接访问容器中网络服务
docker run -P 可以将事先容器预定义的所有端口映射宿主机的网卡的随机端口,默认从32768开始
使用随机端口 时,当停止容器后再启动可能会导致端口发生变化
-P , --publish-all= true | false默认为false
#示例:
docker run -P docker.io/nginx #映射容器所有暴露端口至随机本地端口
在启动容器的时候是分配给容器唯一的IP。这个IP地址实际上是和docker0桥接一起的。
root@ubuntu2204:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52c36c5079d5 ubuntu:jammy "bash" 11 hours ago Up 13 minutes compassionate_albattani
be01627007fb nginx:latest "/docker-entrypoint.…" 13 hours ago Up 22 minutes 80/tcp priceless_nightingale
root@ubuntu2204:~# docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' priceless_nightingale
172.17.0.2
root@ubuntu2204:~# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:2a:fe:f9 brd ff:ff:ff:ff:ff:ff
altname enp2s1
inet 10.0.0.223/24 brd 10.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe2a:fef9/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:98:82:64:b1 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:98ff:fe82:64b1/64 scope link
valid_lft forever preferred_lft forever
5: vethe3a719b@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default #这是虚拟网卡,对应其中一个运行的容器,并与docker0桥接
link/ether 62:75:cb:7d:cc:13 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::6075:cbff:fe7d:cc13/64 scope link
valid_lft forever preferred_lft forever
15: vethbf57483@if14: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default #这是虚拟网卡,对应其中一个运行的容器,成对出现,并与docker0桥接
link/ether 82:b6:84:3a:96:5c brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet6 fe80::80b6:84ff:fe3a:965c/64 scope link
valid_lft forever preferred_lft forever
进入容器查看Ip容器IP地址
root@ubuntu2204:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52c36c5079d5 ubuntu:jammy "bash" 11 hours ago Up 21 minutes compassionate_albattani
be01627007fb nginx:latest "/docker-entrypoint.…" 13 hours ago Up 30 minutes 80/tcp priceless_nightingale
root@ubuntu2204:~# docker exec -it 52c36c5079d5 bash
root@52c36c5079d5:/# ip a
bash: ip: command not found
root@52c36c5079d5:/# apt update
Get:1 http://archive.ubuntu.com/ubuntu jammy InRelease [270 kB]
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 Packages [4732 B]
Get:5 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Get:6 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [667 kB]
Get:7 http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages [17.5 MB]
Get:8 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [593 kB]
Get:9 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [781 kB]
Get:10 http://archive.ubuntu.com/ubuntu jammy/multiverse amd64 Packages [266 kB]
Get:11 http://archive.ubuntu.com/ubuntu jammy/restricted amd64 Packages [164 kB]
Get:12 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages [1792 kB]
Get:13 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [641 kB]
Get:14 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [973 kB]
Get:15 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [977 kB]
Get:16 http://archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [8150 B]
Get:17 http://archive.ubuntu.com/ubuntu jammy-backports/main amd64 Packages [3520 B]
Get:18 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [7291 B]
Fetched 24.9 MB in 1min 20s (312 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
94 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@52c36c5079d5:/# apt install -y net-tools
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
net-tools
0 upgraded, 1 newly installed, 0 to remove and 94 not upgraded.
Need to get 204 kB of archives.
After this operation, 819 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 net-tools amd64 1.60+git20181103.0eebece-1ubuntu5 [204 kB]
Fetched 204 kB in 2s (119 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package net-tools.
(Reading database ... 4384 files and directories currently installed.)
Preparing to unpack .../net-tools_1.60+git20181103.0eebece-1ubuntu5_amd64.deb ...
Unpacking net-tools (1.60+git20181103.0eebece-1ubuntu5) ...
Setting up net-tools (1.60+git20181103.0eebece-1ubuntu5) ...
root@52c36c5079d5:/# ip a
bash: ip: command not found
root@52c36c5079d5:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 8778 bytes 25626708 (25.6 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8759 bytes 477437 (477.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@52c36c5079d5:/# hostname -I
172.17.0.3
root@52c36c5079d5:/# exit
exit
进入nginx容器修改首页,并通过容器ip本机访问。
那么问题来了,容器是隔离的,那么不同的宿主机的容器IP地址一样会怎么样?默认容器IP不支持夸宿主机,只支持本机访问或本宿主机内其他容器进行访问,所以需要进行暴露。
root@ubuntu2204:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52c36c5079d5 ubuntu:jammy "bash" 11 hours ago Up 27 minutes compassionate_albattani
be01627007fb nginx:latest "/docker-entrypoint.…" 13 hours ago Up 36 minutes 80/tcp priceless_nightingale
root@ubuntu2204:~# docker exec -it priceless_nightingale bash
root@be01627007fb:/# hostname -I
172.17.0.2
root@be01627007fb:/# ls /usr/share/nginx/html
50x.html index.html
root@be01627007fb:/# echo "docker site" > /usr/share/nginx/html/index.html
root@be01627007fb:/#
root@ubuntu2204:~# curl 172.17.0.2
<!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@ubuntu2204:~# curl 172.17.0.2
docker site
root@ubuntu2204:~#
桥接模式:
宿主机里不同容器进行访问,宿主机与容器访问。容器自身可以访互联网,但是外面的访问不了容器,也不支持跨宿主机访问。
root@ubuntu2204:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52c36c5079d5 ubuntu:jammy "bash" 12 hours ago Up 40 minutes compassionate_albattani
be01627007fb nginx:latest "/docker-entrypoint.…" 13 hours ago Up 49 minutes 80/tcp priceless_nightingale
root@ubuntu2204:~# docker exec -it priceless_nightingale bash #容器Ubuntu
root@be01627007fb:/# hostname -I
172.17.0.2
root@be01627007fb:/# exit
exit
root@ubuntu2204:~# docker exec -it compassionate_albattani bash #容器nginx
root@52c36c5079d5:/# hostname -i
172.17.0.3
root@52c36c5079d5:/# curl 172.17.0.2
bash: curl: command not found
root@52c36c5079d5:/# apt update ,apt -y install curl
root@52c36c5079d5:/# curl 172.17.0.2
docker site
root@52c36c5079d5:/# curl www.baidu.com #容器自身可以访互联网
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc>