Docker containers (C) - a container port mapping

(1) The container port mapping

  Container port mapping option uses -p, -p [a physical local port]: [Example container port]

  Let centos: httpd running in the background

[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

  Then use a browser to access

 

  Open view on the physical machine 80 proxy port

[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) Access background container instance

  Syntax: docker exec -it [container ID | mirror name] / bin / bash

  Into the container instance running in the background, see the IP address of the container

[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

  In a look at the physical machine's IP address

[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

  

Guess you like

Origin www.cnblogs.com/diantong/p/11519571.html