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
• 18,570 points
30,774 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 8000 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
• 52,360 points

edited Jan 16, 2020 by Kalgi
Now run the containers and map port 5000 to the corresponding interface. For example:
-> think you mean 8000
Yes, I meant 8000. I have updated my answer.

Thanks

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 17, 2018 in Docker by Sophie may
• 10,610 points
8,571 views
+1 vote
2 answers
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, 2019 in Docker by Sirajul
• 59,230 points
11,911 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, 2019 in Docker by Sirajul
• 59,230 points
10,402 views
+2 votes
1 answer
+2 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
• 58,090 points
2,426 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,220 points

edited Oct 16, 2018 by lina 32,861 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
3,149 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP