有一些应用只需要1个端口就能访问
比如 nginx

而有一些应用的搭建都是由多个节点搭建的
比如hadoop 当访问hdfs的时候 访问的不仅仅用port还会用ip
这时后端口映射就不那么好使了

1. 环境介绍

win10 开发环境
win10下通过虚拟机安装了ubuntu-16 网络为桥接
ubuntu-16中运行了docker

win10-IP: 192.168.88.123
ubuntu-IP: 192.168.88.3

2. 目标

  • 通常操作
    docker run -d –name nx -p 8080:80 nginx 访问nginx可在win10浏览器打开 192.168.88.3:8080

  • 希望实现

1
2
3
4
ash-4.3# docker run -d --name nx nginx
077b5cdd75a5457a0a45c40acfafc4493d20e53eedda3c1304f944d19c147925
ash-4.3# docker inspect -f {{.NetworkSettings.IPAddress}}  nx
172.17.0.6

访问nginx可在win10浏览器打开 172.17.0.6

  • 事实上
    在ubuntu内可以ping通
    但win10直接访问172.17.0.6是连不上的

3. 方案1-配置路由

1
2
3
4
5
#在ubuntu中执行
ash-4.3# docker run -d --name nx nginx
077b5cdd75a5457a0a45c40acfafc4493d20e53eedda3c1304f944d19c147925
ash-4.3# docker inspect -f {{.NetworkSettings.IPAddress}}  nx
172.17.0.6

得到nginx的ip为 172.17.0.6 则在win10 为172.17.0.0添加路由

1
2
3
4
##在win10中执行
route -p add 172.17.0.0/16  192.168.88.3
##route -p add 172.17.0.0 mask 255.255.0.0 192.168.88.3 
##route delete 172.17.0.0

4. 方案2-vpn

在 ubuntu中通过docker安装vpn-pptp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
cat > chap-secrets << EOF
# Secrets for authentication using PAP
# client    server      secret      acceptable local IP addresses
tom    *           123    *
EOF

docker run -d --privileged \

--net=host --name pptp \

-v `pwd`/chap-secrets:/etc/ppp/chap-secrets \

mobtitude/vpn-pptp
ubuntu上装好代理后  
在win10上连接上pptp的代理  
vpn-IP 192.168.88.3  
用户名 tom 密码 123

5. 方案3

为容器分配和宿主机相同网段的ip
网上就能搜pipework docker
步骤不少就不写了