Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 424
kino lucky Avatar asked Jul 30 '14 12:07

kino lucky


People also ask

How do you expose Docker ports outside?

Need of exposing ports. In order to make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, we can use the -P or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.

How do I expose a Docker container IP address?

You can easily get the IP address of any container if you have the name or ID of the container. You can get the container names using the "Docker ps -a" command. This will list all the existing containers.

How do I expose a docker port?

Exposing Docker ports can be done using the ‘-p’ option with ‘docker run’ command to bind the port when launching the container: docker run -d -p 9090:80 -t nginx This command will create a container with the image ‘nginx’ and bind the container’s port 80 to the host machine’s port 9090.

What is port mapping in Docker?

Port mapping makes the processes inside the container available from the outside. While running a new Docker container, we can assign the port mapping in the docker run command using the -p option: The above command launches an httpd container and maps the host’s port 81 to port 80 inside that container.

What is the difference between expose and expose in Docker?

The above line will instruct Docker that the container’s service can be connected to via port 8080. By default, the EXPOSE keyword specifies that the port listens on TCP protocol. On the other hand, –expose is a runtime flag that lets you expose a specific port or a range of ports inside the container.

How to connect to Docker containers with external IP address?

So if you have a use case to connect to those containers externally, you would need to use the host machine's external IP (assuming that you are exposing the containers ports correctly). Or if you are using kubernetes, for instance, to manage your Docker containers, let it handle the IP Addresses for you kubernetes-expose-external-ip-address ?.


1 Answers

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.

like image 85
Ben Whaley Avatar answered Sep 29 '22 17:09

Ben Whaley