How to bind one docker container inside another to 127.0.0.1?

Валера Соловьев asked:

I need to bind a container with storage-legacy to another app2-legacy, but I need that application inside the app2-legacy can connect to storage-legacy throught localhost.

I know about
`docker run -P -ti –rm –name app2-legacy –link storage-legacy:storage-legacy mystorage-legacy app.sh

But I need connect throught 127.0.0.1. How I can do this?

My answer:


You can put two containers in the same network namespace, which means they will share the lo interface and each container’s process can access ports from the other container via localhost.

Do this with --net=container:<name|id>

docker run container1
docker run --net=container:container1 container2

Note that the containers will also share their other network interfaces, such as external network connections.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.