docker网络访问(三)

docker网络访问

ifconfig查看网卡,启动docker的时候,docker会帮我们创建一个docker0的网桥。

1、随机映射

  • docker run -P

2、指定映射 

  • -p hostPort:containerPort
  • -p ip:hostPort:containerPort
  • -p ip::containerPort
  • -p hostPort:containerPort:udp
  • -p hostPort:containerPort -p hostPort:containerPort (多个端口映射)

 

随机映射案例

使用docker启动nginx

[root@linux-node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              01f818af747d        2 weeks ago         181.6 MB
centos              latest              67591570dd29        4 weeks ago         191.8 MB
[root@linux-node1 ~]# docker run -d -P nginx 
9725a9fb539a3334436c74a808f9ba134a8f530ed14065435f49846a7ecd4330
[root@linux-node1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
9725a9fb539a        nginx               "nginx -g 'daemon off"   9 seconds ago       Up 5 seconds        0.0.0.0:10001->80/tcp, 0.0.0.0:10000->443/tcp   dreamy_gates

从上面docker ps命令可以看到,nginx容器启动,物理机的10001被映射到容器的80端口,物理机的10000被映射到容器的443端口。

访问物理机IP和port,http://192.168.137.11:10001/

进入nginx容器,查看网络分配地址为172.17.0.2

[root@linux-node1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
9725a9fb539a        nginx               "nginx -g 'daemon off"   9 minutes ago       Up 9 minutes        0.0.0.0:10001->80/tcp, 0.0.0.0:10000->443/tcp   dreamy_gates
[root@linux-node1 ~]# ./docker_in.sh 9725a9fb539a
root@9725a9fb539a:/# ip ad li
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    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
20: eth0@if21: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link 
       valid_lft forever preferred_lft forever

iptables查看Nat表,命令:iptables -t nat -vnL

从上图可以看到容器和物理机的映射关系。

查看nginx容器日志

[root@linux-node1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
9725a9fb539a        nginx               "nginx -g 'daemon off"   13 minutes ago      Up 12 minutes       0.0.0.0:10001->80/tcp, 0.0.0.0:10000->443/tcp   dreamy_gates
[root@linux-node1 ~]# docker logs 9725a9fb539a

 

指定映射案例

1、使用docker启动一个nginx,指定88端口

[root@linux-node1 ~]# docker run -d -p 192.168.137.11:88:80 --name mynginx nginx
f807dc543291857aecd7ff9ebc7318039c6873e5c727cf71dbdd07d8ebac624a
[root@linux-node1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
f807dc543291        nginx               "nginx -g 'daemon off"   4 seconds ago       Up 2 seconds        443/tcp, 192.168.137.11:88->80/tcp   mynginx

提示: -d代表后台运行 -p指定端口 --name指定名称

访问页面 http://192.168.137.11:88/

查看nginx端口映射

[root@linux-node1 ~]# docker port mynginx
80/tcp -> 192.168.137.11:88

 

2、docker 启动映射多端口

[root@linux-node1 ~]# docker run -d -p 443:443 -p 89:80 --name mynginx2 nginx
e9e4eed824ddb26fa1abe742123e3c46515be775dbc5c23556cefbaa94e92838
[root@linux-node1 ~]# docker port mynginx2
443/tcp -> 0.0.0.0:443
80/tcp -> 0.0.0.0:89

 

posted @ 2017-01-15 21:15  shhnwangjian  阅读(716)  评论(0编辑  收藏  举报