目录
Docker 网络实现原理
Docker使用Linux桥接,在宿主机虚拟一个Docker容器网桥(docker0),Docker启动一个容器时会根据Docker网桥的网段分配给容器一个IP地址,称为Container-IP,同时Docker网桥是每个容器的默认网关。因为在同一宿主机内的容器都接入同一个网桥,这样容器之间就能够通过容器的 Container-IP 直接通信。
Docker网桥是宿主机虚拟出来的,并不是真实存在的网络设备,外部网络是无法寻址到的,这也意味着外部网络无法直接通过 Container-IP 访问到容器。如果容器希望外部访问能够访问到,可以通过映射容器端口到宿主主机(端口映射),即 docker run 创建容器时候通过 -p 或 -P 参数来启用,访问容器的时候就通过[宿主机IP]:[容器端口]访问容器。
docker run -d --name test1 -P nginx #随机映射端口(从32768开始)
docker run -d --name test2 -p 43000:80 nginx #指定映射端口
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85a8b9f0219a nginx "/docker-entrypoint.…" 5 seconds ago Up 5 seconds 0.0.0.0:32768->80/tcp, :::32768->80/tcp test2
2408cad84080 nginx "/docker-entrypoint.…" 18 minutes ago Exited (0) 2 minutes ago test1
316bd9904a0c centos:7 "/bin/bash" 22 hours ago Exited (137) 2 minutes ago goofy_hellman
2b08b6377167 nginx:latest "/docker-entrypoint.…" 23 hours ago Exited (137) 2 minutes ago hungry_chatterj
浏览器访问:http://192.168.110.100:43000 、http://192.168.110.100:49170
在另一台主机上能通过curl 192.168.110.100:32768连接上
注:连接可能会失败,要提前做路由转发和docker重启
vim /etc/sysctl.conf
net.ipv4.ip_forword = 1
systemctl daemon-reload
systemctl restart docker
查看容器的输出和日志信息
格式:docker logs 容器的ID/名称
/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: 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
2023/07/20 06:22:03 [notice] 1#1: using the "epoll" event method
2023/07/20 06:22:03 [notice] 1#1: nginx/1.25.1
2023/07/20 06:22:03 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2023/07/20 06:22:03 [notice] 1#1: OS: Linux 3.10.0-1160.el7.x86_64
2023/07/20 06:22:03 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/07/20 06:22:03 [notice] 1#1: start worker processes
2023/07/20 06:22:03 [notice] 1#1: start worker process 29
2023/07/20 06:22:03 [notice] 1#1: start worker process 30
192.168.110.90 - - [20/Jul/2023:06:22:20 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-
注:一个容器最好只做一个业务,这样可以通过判断容器的状态来确定容器业务的状态
Docker 的网络模式:
●Host:容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口。
●Container:创建的容器不会创建自己的网卡,配置自己的IP,而是和一个指定的容器共享IP、端口范围。
●None:该模式关闭了容器的网络功能。
●Bridge:默认为该模式,此模式会为每一个容器分配、设置IP等,并将容器连接到一个docker0虚拟网桥,通过docker0网桥以及iptables nat 表配置与宿主机通信。
●自定义网络
安装Docker时,它会自动创建三个网络,bridge(创建容器默认连接到此网络)、 none 、host
docker network ls 或 docker network list #查看docker网络列表
NETWORK ID NAME DRIVER SCOPE
69986460f1e0 bridge bridge local
14ed40770acd host host local
c5e107f96259 none null local
#使用docker run创建Docker容器时,可以用 --net 或 --network 选项指定容器的网络模式
●host模式:使用 --net=host 指定。
●none模式:使用 --net=none 指定。
●container模式:使用 --net=container:NAME_or_ID 指定。
●bridge模式:使用 --net=bridge 指定,默认设置,可省略。
网络模式详解:
1.host模式
相当于Vmware中的桥接模式,与宿主机在同一个网络中,但没有独立IP地址。
Docker使用了Linux的Namespaces技术来进行资源隔离,如PID Namespace隔离进程,Mount Namespace隔离文件系统,Network Namespace隔离网络等。
一个Network Namespace提供了一份独立的网络环境,包括网卡、路由、iptable规则等都与其他的Network Namespace隔离。 一个Docker容器一般会分配一个独立的Network Namespace。 但如果启动容器的时候使用host模式,那么这个容器将不会获得一个独立的Network Namespace, 而是和宿主机共用一个Network Namespace。容器将不会虚拟出自己的网卡、配置自己的IP等,而是使用宿主机的IP和端口。
docker run -itd --name test4 --net=host nginx
curl 192.168.110.100: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;