Docker容器——容器端口映射

Posted diantong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker容器——容器端口映射相关的知识,希望对你有一定的参考价值。

(1).容器端口映射

  容器的端口映射用到了-p选项,-p [物理机端口]:[容器实例端口]

  让centos:httpd运行在后台

[root@youxi1 ~]# docker run -d -p 80:80 centos:httpd
92c1d60be419a06d24f6a8eb4c60e89a5d5e6652087ffa73bd86be320fa589e0
[root@youxi1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92c1d60be419 centos:httpd "/bin/sh -c /usr/loc…" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp jovial_chebyshev

  接着使用浏览器访问

技术图片

 

  查看物理机上开启的80代理端口

[root@youxi1 ~]# yum -y install net-tools
[root@youxi1 ~]# netstat -antup | grep 80
tcp        0      0 192.168.5.101:46116     114.80.24.198:80        TIME_WAIT   -                   
tcp        0      0 192.168.5.101:36968     114.80.24.199:80        TIME_WAIT   -                   
tcp        0      0 192.168.5.101:58516     114.80.24.201:80        TIME_WAIT   -                   
tcp        0      0 192.168.5.101:46120     114.80.24.198:80        TIME_WAIT   -                   
tcp        0      0 192.168.5.101:60010     114.80.24.203:443       TIME_WAIT   -                   
tcp6       0      0 :::80                   :::*                    LISTEN      5040/docker-proxy  //这一条
udp        0      0 127.0.0.1:323           0.0.0.0:*                           801/chronyd         
udp6       0      0 ::1:323                 :::*                                801/chronyd   

(2).访问后台运行的容器实例

  语法:docker exec -it [容器ID|镜像名称] /bin/bash

  进入后台运行的容器实例,查看容器的IP地址

[root@youxi1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
92c1d60be419        centos:httpd        "/bin/sh -c /usr/loc…"   13 minutes ago      Up 13 minutes       0.0.0.0:80->80/tcp   jovial_chebyshev
[root@youxi1 ~]# docker exec -it 92c1d60be419 /bin/bash
[root@92c1d60be419 /]# yum -y install net-tools
[root@92c1d60be419 /]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255  //容器实例的IP地址
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 83  bytes 319729 (312.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 4649 (4.5 KiB)
        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

  在看一下物理机的IP地址

[root@youxi1 ~]# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255  //这个IP与容器实例处于同网段
        inet6 fe80::42:24ff:fe5d:36c1  prefixlen 64  scopeid 0x20<link>
        ether 02:42:24:5d:36:c1  txqueuelen 0  (Ethernet)
        RX packets 8209  bytes 333975 (326.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12228  bytes 40934054 (39.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.5.101  netmask 255.255.255.0  broadcast 192.168.5.255
        inet6 fe80::201:7257:85b:7dc8  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:e6:d6:27  txqueuelen 1000  (Ethernet)
        RX packets 177898  bytes 60097281 (57.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 112125  bytes 155022609 (147.8 MiB)
        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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 32  bytes 2592 (2.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32  bytes 2592 (2.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethf3eac0e: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::8098:68ff:fed3:5ad7  prefixlen 64  scopeid 0x20<link>
        ether 82:98:68:d3:5a:d7  txqueuelen 0  (Ethernet)
        RX packets 81  bytes 5530 (5.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 98  bytes 321124 (313.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

  

以上是关于Docker容器——容器端口映射的主要内容,如果未能解决你的问题,请参考以下文章

Docker端口映射与进入容器内部

docker端口映射与容器互联

07-docker端口映射与容器关联

Docker ❀ 容器内部/外部通信端口映射网络模式自定义容器网络

解决CentOS7 docker容器映射端口只监听ipv6的问题

Docker-端口映射与容器互联