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

2

53

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.

Swindell answered 30/7, 2014 at 12:30 Comment(1)
The question is unclear. Do docker containers to "be" on the same network adapter as the host? In that case, use --net=host. Or do you want to bind the port to not be random?Virescence
D
74

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.

Demurral answered 30/7, 2014 at 16:11 Comment(3)
I am using boot2docker, I dont see eth0:1 avaliable for me in (boot2docker) mac, any further steps to get it working on mac ?Continuant
Try it like this sudo ifconfig en1 inet w.x.y.z netmask 255.255.255.0 alias where en1 is the interface you want to add an alias for.Demurral
I tried , but it complaint "Error response from daemon: Cannot start container XXXXX: Error starting userland proxy: listen tcp 172.16.1.99:80: bind: cannot assign requested address " , any clues?Continuant
C
-7

When creating a VM make sure that the following are selected under networking

Attached to:        Bridged NetworkManager
Adapter Type:       PCnet-Fast III (Am 79C973)
Promiscious Mode    Allow All

RHEL 6.5 / Fedora 20

Install docker, libvrt

Make sure the following are done using root

# chkconfig NetworkManager off
# chkconfig network on  
# service NetworkManager stop
# service network start

Create file ifcfg-xxxxx in /etc/sysconfig/network-scripts

DEVICE=xxxxx
TYPE=Bridge
BOOTPROTO=dhcp
ONBOOT=yes
DELAY=0

and append to ifcfg-p2p1 / ifcfg-eth0 at the end of the file BRIDGE=xxxx

Restart the VM

run

brctl show 

to make sure the bridged connected has an adapter either p2p1 or eth0 e.g.

# brctl show
bridge name     bridge id               STP enabled     interfaces
gsbr01          8000.080027595649       no              eth0
virbr0          8000.5254004c1564       yes             virbr0-nic

now before starting docker we have to use our bridge and not docker0 to do that, run docker as docker -d -b=gsbr01

$ echo 'DOCKER_OPTS="-b=gsbr01"' >> /etc/sysconfig/docker
$ sudo service docker start

Check the result:

# brctl show
bridge name     bridge id               STP enabled     interfaces
gsbr01          8000.080027595649       no              eth0
                                                        veth5806f27
                                                        vethb3e33da
virbr0          8000.5254004c1564       yes             virbr0-nic

docker -d -b=gsbr01
Corabella answered 15/12, 2014 at 10:33 Comment(1)
How does this answer answers the question?Nuzzle

© 2022 - 2024 — McMap. All rights reserved.