Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

networking - How to expose docker container's ip and port to outside docker host without port mapping?

When i started two docker containers for a same web image on one docker host.

  • two docker containers listened on the same port 5000
  • port 5000 of the two containers were mapped to different ports of docker host: 49155, 49156
  • to access the two containers from outside docker host need to be by accessing the docker host ip and port 49155 or 49156

Is there a solution to access a docker container from outside docker host by its ip and port, x.x.x.x:5000, without port mapping?

All docker containers on different dock hosts can access each other directly.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can accomplish this with IP aliasing on the host.

First, add a virtual interface on the host that has a different IP address than the primary interface. We'll call the primary interface eth0 with IP 10.0.0.10, and the virtual interface eth0:1 with IP address 10.0.0.11.

 ifconfig eth0:1 10.0.0.11 netmask 255.255.255.0 up 

Now run the containers and map port 5000 to the corresponding interface. For example:

docker run -p 10.0.0.10:5000:5000 -name container1 <someimage> <somecommand>
docker run -p 10.0.0.11:5000:5000 -name container2 <someimage> <somecommand>

Now you can access each container on port 5000 using different IP addresses externally.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...