docker的网络访问

 

Docker的网络访问:

#systemctl  start docker

#ifconfig

[root@zizhen02 ~]# ifconfig

docker0: flags=4099  mtu 1500

        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255

        ether 02:42:b2:e3:5b:db  txqueuelen 0  (Ethernet)

        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

 

必须安装网桥工具bridge-utils,运行命令:#yum install bridge-utils

https://fp-moon.iteye.com/blog/1468650

[root@zizhen02 ~]# brctl show

bridge name    bridge id            STP enabled     interfaces

docker0             8000.0242b2e35bdb        no    

 

docker网络访问有两种访问方式: 随机映射和指定映射

随机映射:docker   run  -P

#docker images

#docker run –d –P nginx   #docker 运行到后台

#[root@zizhen02 ~]# docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES

2ce4f13ed654        nginx               "nginx -g 'daemon of…"   22 seconds ago      Up 21 seconds       0.0.0.0:32768->80/tcp   condescending_wilbur

查看路由表 #iptables –t  nat  -vnL

[root@zizhen02 ~]# iptables -t nat -vnL

Chain PREROUTING (policy ACCEPT 10 packets, 1030 bytes)

 pkts bytes target     prot opt in     out     source               destination        

   34  1760 DOCKER     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL

 

Chain INPUT (policy ACCEPT 10 packets, 1030 bytes)

 pkts bytes target     prot opt in     out     source               destination        

 

Chain OUTPUT (policy ACCEPT 24 packets, 1448 bytes)

 pkts bytes target     prot opt in     out     source               destination        

    1    60 DOCKER     all  --  *      *       0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL

 

Chain POSTROUTING (policy ACCEPT 53 packets, 2964 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 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          

   29  1516 DNAT       tcp  --  !docker0 *       0.0.0.0/0            0.0.0.0/0            tcp dpt:32768 to:172.17.0.2:80

#ps  -aux |grep dhcp

#cat /var/lib/libvirt/dnsmasq/default.conf

[root@zizhen02 ~]# docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES

2ce4f13ed654        nginx               "nginx -g 'daemon of…"   16 minutes ago      Up 16 minutes       0.0.0.0:32768->80/tcp   condescending_wilbur

[root@zizhen02 ~]# docker logs 2ce4f13ed654

 

指定映射:

-p  hostPort:containerPort

-p  ip: hostPort:containerPort

-p  ip: : containerPort

-p  hostPort:containerPort:udp

-p  443:443  -p 81:80

#

[root@zizhen02 ~]# docker run -d -p 192.168.1.107:8000:80 --name mynginx nginx

5cb5e2fb6d1151d4cf5a1bf5c5241ce6baa55375fc319d97723b5ae17ed98199

[root@zizhen02 ~]# docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                        NAMES

5cb5e2fb6d11        nginx               "nginx -g 'daemon of…"   16 seconds ago      Up 15 seconds       192.168.1.107:8000->80/tcp   mynginx

2ce4f13ed654        nginx               "nginx -g 'daemon of…"   23 minutes ago      Up 23 minutes       0.0.0.0:32768->80/tcp        condescending_wilbur

[root@zizhen02 ~]# docker ps -l

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                        NAMES

5cb5e2fb6d11        nginx               "nginx -g 'daemon of…"   23 seconds ago      Up 22 seconds       192.168.1.107:8000->80/tcp   mynginx

[root@zizhen02 ~]# curl 192.168.1.107:8000

Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.

 

For online documentation and support please refer to

nginx.org.

Commercial support is available at

nginx.com.

 

Thank you for using nginx.

#

[root@zizhen02 ~]# docker port mynginx

80/tcp -> 192.168.1.107:8000

[root@zizhen02 ~]# docker run -d -p 443:443 -p 8001:80 --name nginxv2 nginx

97b6ec6152522ef7d747d60e0d9b611fab741fd4a05e14baa645d1dd23c6d874

[root@zizhen02 ~]# docker ps -l

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                        NAMES

97b6ec615252        nginx               "nginx -g 'daemon of…"   10 seconds ago      Up 8 seconds        0.0.0.0:443->443/tcp, 0.0.0.0:8001->80/tcp   nginxv2

[root@zizhen02 ~]# docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                        NAMES

97b6ec615252        nginx               "nginx -g 'daemon of…"   20 seconds ago      Up 18 seconds       0.0.0.0:443->443/tcp, 0.0.0.0:8001->80/tcp   nginxv2

5cb5e2fb6d11        nginx               "nginx -g 'daemon of…"   8 minutes ago       Up 8 minutes        192.168.1.107:8000->80/tcp                   mynginx

2ce4f13ed654        nginx               "nginx -g 'daemon of…"   32 minutes ago      Up 32 minutes       0.0.0.0:32768->80/tcp                        condescending_wilbur

[root@zizhen02 ~]# docker port ngixv2

Error: No such container: ngixv2

[root@zizhen02 ~]# docker port nginxv2

443/tcp -> 0.0.0.0:443

80/tcp -> 0.0.0.0:8001

[root@zizhen02 ~]#

#

 

转载于:https://www.cnblogs.com/weizitianming/p/10532808.html

你可能感兴趣的:(docker的网络访问)