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

+1 vote

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

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

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

Aug 2, 2018 in Docker by Hannah
• 15,440 points
2,535 views
When you have the option of port mapping why would you want to use anything else?

1 answer to this question.

+1 vote

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:8000:8000 -name container1 <someimage> <somecommand>
docker run -p 10.0.0.11:8000:8000 -name container2 <someimage> <somecommand>

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

answered Aug 2, 2018 by Kalgi
• 41,660 points

Related Questions In Docker

0 votes
1 answer

How to obtain the Docker container's IP address from the host?

This can be done by executing the ...READ MORE

answered Jul 16, 2018 in Docker by Sophie may
• 9,850 points
636 views
0 votes
1 answer

How to move Docker containers from one host to another?

Yes you could definitely do it the ...READ MORE

answered Jul 2 in Docker by Sirajul
• 30,540 points
138 views
0 votes
1 answer

How to enable forwarding from docker containers to the outside world?

By default, traffic from containers connected to ...READ MORE

answered Jul 8 in Docker by Sirajul
• 30,540 points
243 views
0 votes
1 answer
0 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 56,900 points
189 views
+4 votes
4 answers

How To Access a Service on Host From a Docker Container?

Adding to kalgi's answer, You can also ...READ MORE

answered Oct 16, 2018 in Docker by lina
• 8,110 points

edited Oct 16, 2018 by lina 4,819 views
+1 vote
2 answers

How to run docker containers on different machines

You can use labels and selectors for ...READ MORE

answered Oct 23, 2018 in Docker by Laila
87 views