docker容器网络与配置

本文深入探讨了Docker的4种网络模式,包括bridge、none、container和host模式,并详细解析了Linux内核如何创建和操作Network Namespace,以及veth pair在容器通信中的作用。此外,还介绍了容器的常用网络配置和操作,如主机名设置、DNS指定、端口映射等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

docker容器网络

Docker在安装后自动提供3种网络,可以使用docker network ls命令查看

[root@xieyanxin ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
032579a91051   bridge    bridge    local
fd594d69163d   host      host      local
69428a6bf0d2   none      null      local

Docker使用Linux桥接,在宿主机虚拟一个Docker容器网桥(docker0),Docker启动一个容器时会根据Docker网桥的网段分配给容器一个IP地址,称为Container-IP,同时Docker网桥是每个容器的默认网关。因为在同一宿主机内的容器都接入同一个网桥,这样容器之间就能够通过容器的Container-IP直接通信。

docker的4种网络模式

网络模式配置说明
host 主机模式–network host容器和宿主机共享Network namespace
container容器模式–network container:NAME_OR_ID容器和另外一个容器共享Network namespace
none 孤岛模式–network none“容器有独立的Network namespace,
但并没有对其进行任何网络设置,
如分配veth pair 和网桥连接,配置IP等”
bridge桥接模式–network bridge默认模式

Linux内核实现名称空间的创建

ip netns命令

可以借助ip netns命令来完成对 Network Namespace 的各种操作。ip netns命令来自于iproute安装包,一般系统会默认安装,如果没有的话,请自行安装。

注意:ip netns命令修改网络配置时需要 sudo 权限。

可以通过ip netns命令完成对Network Namespace 的相关操作,可以通过ip netns help查看命令帮助信息:

[root@xieyanxin ~]# ip netns help
Usage:  ip netns list
        ip netns add NAME
        ip netns attach NAME PID
        ip netns set NAME NETNSID
        ip [-all] netns delete [NAME]
        ip netns identify [PID]
        ip netns pids NAME
        ip [-all] netns exec [NAME] cmd ...
        ip netns monitor
        ip netns list-id [target-nsid POSITIVE-INT] [nsid POSITIVE-INT]
NETNSID := auto | POSITIVE-INT

默认情况下,Linux系统中是没有任何 Network Namespace的,所以ip netns list命令不会返回任何信息。

创建Network Namespace

[root@xieyanxin ~]# ip netns list
[root@xieyanxin ~]# ip netns add ns0
[root@xieyanxin ~]# ip netns add ns1
[root@xieyanxin ~]# ip netns list
ns1
ns0

新创建的 Network Namespace 会出现在/var/run/netns/目录下。如果相同名字的 namespace 已经存在,命令会报Cannot create namespace file “/var/run/netns/ns0”: File exists的错误。

[root@xieyanxin ~]# ls /var/run/netns/
ns0  ns1
[root@xieyanxin ~]# ip netns add ns0
Cannot create namespace file "/var/run/netns/ns0": File exists

操作Network Namespace

ip命令提供了ip netns exec子命令可以在对应的 Network Namespace 中执行命令。

查看新创建 Network Namespace 的网卡信息

[root@xieyanxin ~]# ip netns exec ns0 ip addr
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

可以看到,新创建的Network Namespace中会默认创建一个lo回环网卡,此时网卡处于关闭状态。此时,尝试去 ping 该lo回环网卡,会提示Network is unreachable

[root@xieyanxin ~]# ip netns exec ns0 ping 127.0.0.1
connect: Network is unreachable

通过下面的命令启用lo回环网卡:

[root@xieyanxin ~]# ip netns exec ns0 ip link set lo up
[root@xieyanxin ~]# ip netns exec ns0 ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.126 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.121 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.084 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.074 ms
^C
--- 127.0.0.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3063ms
rtt min/avg/max/mdev = 0.074/0.101/0.126/0.023 ms


转移设备

我们可以在不同的 Network Namespace 之间转移设备(如veth)。由于一个设备只能属于一个 Network Namespace ,所以转移后在这个 Network Namespace 内就看不到这个设备了。

其中,veth设备属于可转移设备,而很多其它设备(如lo、vxlan、ppp、bridge等)是不可以转移的。

veth pair

veth pair 全称是 Virtual Ethernet Pair,是一个成对的端口,所有从这对端口一 端进入的数据包都将从另一端出来,反之也是一样。
引入veth pair是为了在不同的 Network Namespace 直接进行通信,利用它可以直接将两个 Network Namespace 连接起来。

img

创建veth pair

[root@xieyanxin ~]# ip netns list
ns1
ns0
//把veth0给ns1用、veth1给ns2用
[root@xieyanxin ~]# ip link set veth0 netns ns0
[root@xieyanxin ~]# ip link set veth1 netns ns1
//可以看到给了网卡之后,真机上面就没有了,所有网卡是移动过去的
[root@xieyanxin ~]# 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:35:c7:eb brd ff:ff:ff:ff:ff:ff
    inet 192.168.177.2/24 brd 192.168.177.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::3124:91a2:9e08:83b7/64 scope link noprefixroute 
       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:b7:14:b5:77 brd ff:ff:ff:ff:ff:ff
57: vethaff647e@if56: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether 5e:59:08:d4:6f:fa brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::5c59:8ff:fed4:6ffa/64 scope link 
       valid_lft forever preferred_lft forever
65: veth2466dc2@if64: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether a6:7d:9e:35:55:68 brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet6 fe80::a47d:9eff:fe35:5568/64 scope link 
       valid_lft forever preferred_lft forever
//设置网卡开启
[root@xieyanxin ~]# ip netns exec ns0 ip link set veth0 up
[root@xieyanxin ~]# ip netns exec ns0 ip addr add 192.168.177.1/24 dev veth0  //分别添加ip
[root@xieyanxin ~]# ip netns exec ns1 ip link setnlo up
[root@xieyanxin ~]# ip netns exec ns1 ip link set veth1 up
[root@xieyanxin ~]# ip netns exec ns1 ip addr add 192.168.177.2/24 dev veth1 //分别添加ip
//查看这对veth pair的状态
[root@xieyanxin ~]# ip netns exec ns0 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
66: veth0@if67: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 22:19:45:ab:25:c0 brd ff:ff:ff:ff:ff:ff link-netns ns1
    inet 192.168.177.1/24 scope global veth0
       valid_lft forever preferred_lft forever
    inet6 fe80::2019:45ff:feab:25c0/64 scope link 
       valid_lft forever preferred_lft forever
       
[root@ xieyanxin~]# ip netns exec ns1 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
67: veth1@if66: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 52:fe:46:2b:b5:a2 brd ff:ff:ff:ff:ff:ff link-netns ns0
    inet 192.168.177.2/24 scope global veth1
       valid_lft forever preferred_lft forever
    inet6 fe80::50fe:46ff:fe2b:b5a2/64 scope link 
       valid_lft forever preferred_lft forever
//网络互相可以ping通
[root@xieyanxin ~]# ip netns exec ns1 ping 192.168.177.1
PING 192.168.177.1 (192.168.177.1) 56(84) bytes of data.
64 bytes from 192.168.177.1: icmp_seq=1 ttl=64 time=0.071 ms
64 bytes from 192.168.177.1: icmp_seq=2 ttl=64 time=0.058 ms
64 bytes from 192.168.177.1: icmp_seq=3 ttl=64 time=0.051 ms
^C
--- 192.168.177.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2080ms
rtt min/avg/max/mdev = 0.051/0.060/0.071/0.008 ms

可以看到,veth pair成功实现了两个不同Network Namespace之间的网络交互。

veth设备重命名

//先让网卡down
[root@xieyanxin ~]# ip netns exec ns0 ip link set veth0 down
//修改名称
[root@xieyanxin ~]# ip netns exec ns0 ip link set dev veth0 name eth0
//开启新网卡
[root@xieyanxin~]# ip netns exec ns0 ip link set eth0 up
//查看
[root@xieyanxin ~]# ip netns exec ns0 ifconfig -a
eth0: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.177.1  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 22:19:45:ab:25:c0  txqueuelen 1000  (Ethernet)
        RX packets 17  bytes 1314 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 17  bytes 1314 (1.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
0

容器四种网络模式的配置

bridge模式配置

//用rm创建容器代表一旦退出就自己删除
[root@xieyanxin ~]# docker run -it --name c1 --rm centos bin/bash
[root@xieyanxin~]# docker ps
CONTAINER ID   IMAGE              COMMAND                  CREATED          STATUS          PORTS                               NAMES
06fef12566d6   centos             "bin/bash"               15 seconds ago   Up 14 seconds                                       c1
779973e9810f   wjmz2/httpd:v1.8   "/usr/local/apache/b…"   25 hours ago     Up 24 hours     0.0.0.0:80->80/tcp, :::80->80/tcp   web6
eaa607c9f823   centos:8           "/bin/bash"              27 hours ago     Up 25 hours                                         httpd

[root@06fef12566d6 /]# exit
exit
[root@xieyanxin ~]# docker ps -a
CONTAINER ID   IMAGE              COMMAND                  CREATED        STATUS                    PORTS                               NAMES
1aab8e7fc6db   wjmz2/httpd:v1.0   "/bin/bash"              24 hours ago   Exited (0) 24 hours ago                                       web
779973e9810f   wjmz2/httpd:v1.8   "/usr/local/apache/b…"   25 hours ago   Up 24 hours               0.0.0.0:80->80/tcp, :::80->80/tcp   web6
eaa607c9f823   centos:8           "/bin/bash"              27 hours ago   Up 25 hours                                                   httpd
//查看容器状态与docker ps -a一样
[root@xieyanxin ~]# docker container ls -a
CONTAINER ID   IMAGE              COMMAND                  CREATED        STATUS                    PORTS                               NAMES
1aab8e7fc6db   wjmz2/httpd:v1.0   "/bin/bash"              24 hours ago   Exited (0) 24 hours ago                                       web
779973e9810f   wjmz2/httpd:v1.8   "/usr/local/apache/b…"   25 hours ago   Up 24 hours               0.0.0.0:80->80/tcp, :::80->80/tcp   web6
eaa607c9f823   centos:8           "/bin/bash"              27 hours ago   Up 25 hours                                                   httpd

# 在创建容器时添加--network bridge与不加--network选项效果是一致的
[root@xieyanxinxieyanxin ~]# docker run -it --network bridge --name t1 --rm busybox /bin/sh
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:04  
          inet addr:172.17.0.4  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:516 (516.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)


none模式配置

[root@xieyanxin ~]# docker run -it --rm --name t1 --network none busybox 
/ # ifconfig
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)


container模式配置

  • 启动第一个容器
[root@xieyanxin ~]# docker run -it --name b1 --rm busybox
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:516 (516.0 B)  TX bytes:0 (0.0 B)

  • 启动第二个容器
[root@xieyanxin ~]# docker run -it --name b2 --rm busybox
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:03  
          inet addr:172.17.0.3  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:516 (516.0 B)  TX bytes:0 (0.0 B)

可以看到名为b2的容器IP地址是10.0.0.3,与第一个容器的IP地址不是一样的,也就是说并没有共享网络,此时如果我们将第二个容器的启动方式改变一下,就可以使名为b2的容器IP与B1容器IP一致,也即共享IP,但不共享文件系统。

[root@xieyanxin ~]# docker run -it --name b2 --rm --network container:b1 busybox
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:796 (796.0 B)  TX bytes:0 (0.0 B)

此时我们在b1容器上创建一个目录

/ # mkdir /tmp/data
/ # ls /tmp
data

到b2容器上检查/tmp目录会发现并没有这个目录,因为文件系统是处于隔离状态,仅仅是共享了网络而已。

在b2容器上部署一个站点

/ # echo 'hello world' > /tmp/index.html
/ # ls /tmp
index.html
/ # httpd -h /tmp
/ # netstat -antl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 :::80                   :::*                    LISTEN  

在b1容器上用本地地址去访问此站点

/ # wget -O - -q 172.17.0.2
hello world

由此可见,container模式下的容器间关系就相当于一台主机上的两个不同进程

host模式配置

  • 启动容器时直接指明模式为host

[root@xieyanxin ~]#  docker run -it --name b2 --rm --network host busybox 
/ # ifconfig
docker0   Link encap:Ethernet  HWaddr 02:42:B7:14:B5:77  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:75640 errors:0 dropped:0 overruns:0 frame:0
          TX packets:107944 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:3118923 (2.9 MiB)  TX bytes:278949063 (266.0 MiB)

ens33     Link encap:Ethernet  HWaddr 00:0C:29:35:C7:EB  
          inet addr:192.168.177.8  Bcast:192.168.177.255  Mask:255.255.255.0
          inet6 addr: fe80::3124:91a2:9e08:83b7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:511843 errors:0 dropped:0 overruns:0 frame:0
          TX packets:210383 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:706955819 (674.2 MiB)  TX bytes:17337475 (16.5 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2112 (2.0 KiB)  TX bytes:2112 (2.0 KiB)

/ # 

此时如果我们在这个容器中启动一个http站点,我们就可以直接用宿主机的IP直接在浏览器中访问这个容器中的站点了。

容器的常用操作

查看容器的主机名

[root@xieyanxin ~]# docker run -it --name t1 --network bridge --rm busybox
/ # hostname
2cd56f455d4a

在容器启动时注入主机名

[root@xieyanxin ~]# docker run -it --name t1 --rm --hostname xieyanxin busybox
/ # hostname
xieyanxin
/ # 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.17.0.2      xieyanxin   //注入主机名时会自动创建主机名到IP的映射关系
/ # cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 114.114.114.114   //DNS也会自动配置为宿主机的DNS
/ # route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.17.0.1      0.0.0.0         UG    0      0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0
/ # 

手动指定容器要使用的DNS

[root@xieyanxin ~]# docker run -it --name t1 --network bridge --hostname xieyanxin --dns 114.114.114.114 --rm busybox
/ # cat /etc/resolv.conf 
nameserver 114.114.114.114
/ # 

手动往/etc/hosts文件中注入主机名到IP地址的映射

[root@xieyanxin ~]# docker run -it --name t1 --network bridge --hostname xieyanxin --add-host www.a.com:1.1.1.1 --rm busybox  
/ # 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
1.1.1.1 www.a.com
172.17.0.2      xieyanxin
/ # 

开放容器端口

执行docker run的时候有个-p选项,可以将容器中的应用端口映射到宿主机中,从而实现让外部主机可以通过访问宿主机的某端口来访问容器内应用的目的。

-p选项能够使用多次,其所能够暴露的端口必须是容器确实在监听的端口。

-p选项的使用格式:

  • -p <containerPort>
    • 将指定的容器端口映射至主机所有地址的一个动态端口
  • -p <hostPort>:<containerPort>
    • 将容器端口映射至指定的主机端口
  • -p <ip>::<containerPort>
    • 将指定的容器端口映射至主机指定的动态端口
  • -p <ip>::<containerPort>
    • 将指定的容器端口映射至主机指定的端口

动态端口指的是随机端口,具体的映射结果可使用docker port命令查看。

[root@xieyanxin ~]# docker run -d --rm --name web -p 80 httpd
b9445433e5cabb35b636585be0b4070ff705b711f4457c71277add2257d7f5e1

以上命令执行后会一直占用着前端,我们新开一个终端连接来看一下容器的80端口被映射到了宿主机的什么端口上

[root@xieyanxin~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS                                     NAMES
b9445433e5ca   httpd     "httpd-foreground"   4 seconds ago   Up 2 seconds   0.0.0.0:49233->80/tcp, :::49153->80/tcp   web

iptables防火墙规则将随容器的创建自动生成,随容器的删除自动删除规则。

[root@xieyanxin ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 11 packets, 656 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   41  2216 DOCKER     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL

Chain INPUT (policy ACCEPT 1 packets, 136 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 8 packets, 480 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 MASQUERADE  all  --  *      !docker0  172.17.0.0/16        0.0.0.0/0           
    0     0 MASQUERADE  tcp  --  *      *       172.17.0.2           172.17.0.2           tcp dpt:80

Chain OUTPUT (policy ACCEPT 6 packets, 360 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    2   120 DOCKER     all  --  *      *       0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL

Chain DOCKER (2 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 RETURN     all  --  docker0 *       0.0.0.0/0            0.0.0.0/0           
   32  1680 DNAT       tcp  --  !docker0 *       0.0.0.0/0            0.0.0.0/0            tcp dpt:49233 to:172.17.0.2:80
[root@xieyanxin ~]# docker rm -f web
web
[root@xieyanxin ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 11 packets, 656 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   41  2216 DOCKER     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL

Chain INPUT (policy ACCEPT 1 packets, 136 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 8 packets, 480 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 MASQUERADE  all  --  *      !docker0  172.17.0.0/16        0.0.0.0/0           

Chain OUTPUT (policy ACCEPT 6 packets, 360 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    2   120 DOCKER     all  --  *      *       0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL

Chain DOCKER (2 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 RETURN     all  --  docker0 *       0.0.0.0/0            0.0.0.0/0        

把容器端口映射到主机端口

[root@xieyanxin~]# docker run -d --rm --name web -p 80:80 httpd
9e273f0cf09132c91052e0fabced114b53e31701d008490415cf3cc899990e23
[root@xieyanxin ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS                               NAMES
9e273f0cf09132c   httpd     "httpd-foreground"   6 seconds ago   Up 4 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   web


将容器端口映射到指定IP的随机端口

[root@xieyanxin ~]# ip addr add 192.168.177.2 /24 dev ens33
[root@xieyanxin ~]# 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:35:c7:eb brd ff:ff:ff:ff:ff:ff
    inet 192.168.177.8/24 brd 192.168.177.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.177.2/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::3124:91a2:9e08:83b7/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@xieyanxin~]# docker run -d --name web --rm -p 192.168.177.2::80 httpd
68da025b03da9df436c9af2706632cefb5c98e6f60c926ed0149ffc3f65a96f8
[root@xieyanxin~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS                         NAMES
68da025b03da   httpd     "httpd-foreground"   3 seconds ago   Up 2 seconds  192.168.177.2:49153->80/tcp   web


将容器端口映射到宿主机的指定ip

[root@xieyanxin ~]# docker run -d --name web1 --rm -p 192.168.177.2:8080:80 httpd
63bdc6ed32fffbdf0fae0bfd737c85fa2106a85df63bbe893f6b0a90d87260a3


在另一个终端上查看端口映射情况

[root@xieyanxin ~]# ss -antl
State      Recv-Q     Send-Q         Local Address:Port          Peer Address:Port     Process     
LISTEN     0          128            192.168.177.2:8080               0.0.0.0:*                    
LISTEN     0          128                  0.0.0.0:22                 0.0.0.0:*                    
LISTEN     0          128            192.168.177.8:49153              0.0.0.0:*                    
LISTEN     0          128                     [::]:22                    [::]:*                    
[root@xieyanxin ~]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值