<p>On Linux, containers live in separate network namespaces, so localhost or 127.0.0.1 on the container is not localhost on the host. I'm not sure about other OSes. You could expose a port to the host like this:</p>

<pre><code>docker run -p 3306:3306 mysql
</code></pre>

<p>Then you can access it from any container at the host IP address, which for the default network is 172.17.0.1:3306. You can add -p many times, read the docs here: <a href="https://docs.docker.com/" rel="nofollow noreferrer">https://docs.docker.com/</a></p>

<p>All that the -p does on Linux, is essentially:</p>

<pre><code>sudo iptables -t nat -A DOCKER \! -i docker0 -p tcp --dport 3306 -j DNAT --to 172.17.0.2:3306
</code></pre>

<p>(Or whatever your mysql container IP address is.) Docker compose and similar utilities make containers able to see each other with <strong>--link</strong> eg.</p>

<pre><code>docker run --link web --link mysql:localmysql app
</code></pre>

<p>The only thing this does is to put the correct IP address for the container in a file called /etc/hosts inside the container (but this might change in future.) </p>

<p>If you want to use --link, all containers need to be on the same network. You can connect them while running with:</p>

<pre><code>docker network connect mynet web
</code></pre>

<p>The main reason for doing this is because the containers don't have fixed IP addresses. You can do:</p>

<pre><code>docker run --network mynet --ip 172.18.0.10 -d app
</code></pre>

<p>To give your container a static IP - and then just use that instead of the name. The docker team keeps changing how things work, so you might need to run additional commands to allow different networks to connect to each other. </p>

<p>You can of course also do all of this manually, on Linux, using the firewall, on the host machine. (Start learning about it here: <a href="https://www.google.com/search?q=iptables" rel="nofollow noreferrer">https://www.google.com/search?q=iptables</a> (Also look at the Images tab!)</p>

<pre><code>iptables -nvxL # shows the filter table
iptables -nvxL -t nat # shows the NAT table where port forwarding is done
</code></pre>

<p>To dynamically forward a port to a container, get its IP address, eg 172.18.0.10, then:</p>

<pre><code>iptables -t nat -A POSTROUTING -p tcp --dport 1234 -j DNAT --to 172.18.0.10:4567
</code></pre>

<p>Be keenly aware that if you do this, and your container restarts, or gets another IP address, that this will not automatically update, and unless you use iptables-save and iptables-restore in your startup and cron scripts, these manually created rules will disappear. Also that the docker command and compose scripts won't know about any configuration that you manually add like this.</p>

<p>If you really want localhost in your container to point somewhere else, you can edit /etc/hosts inside the container, and change the IP address of localhost - be very keenly aware that other things might break, because localhost is supposed to point to the same host, not another one, so there will be unintended consequences at some point. </p>

CC BY-SA 4.0
2019-05-10T14:34:49.520Z
2019-05-10T15:04:05.200Z
2019-05-10T15:04:05.200Z
Dagelf
<p>Let's consider two docker conatiner named <code>app</code> and <code>mysql</code>.
I am looking for a way, where I can map <code>app</code> and <code>mysql</code> ports, so that if I try to connect to <code>mysql</code> from <code>web</code>, using <code>localhost:3306</code> it can connect.</p>

<p>I know I can connect using <code>dns</code> and <code>docker network</code>. But I am curious if there is a way to do this.</p>

CC BY-SA 4.0
2019-05-10T13:47:11.090Z
In docker you don't need to think about ips, but service names, you can accomplish that easily using docker-compose, see this for example: http://blog.adnansiddiqi.me/create-your-first-php-mysql-application-in-docker/
kainlite
CC BY-SA 4.0
2019-05-10T14:35:27.027Z
I think its important that you explain why you want to do this, and the context. Are you confined to a container and you want to get access to another one? Or do you have a legitimate use case and you don't have too much time to spend on learning docker? Or you you want to dynamically add and remove some containers?
Dagelf
CC BY-SA 4.0
2019-05-12T17:29:11.047Z
@Dagelf : I have just started working on an application, where dependencies are lot(mysql,redis,rabbit,mongodb etc etc). And for local development, most of the connection string are written as localhost:3306, localhost:11211 etc. I have found a workaround for that, but I was curious without making changes in config code, I can still run the application in docker environment, thats why the question.
And the solution I found was to run all dependencies in docker containers, and run application on local, so all dependencies are accessible through localhost:<port_numebr>
kadamb
<docker><networking><ports>
Is there a way to map ports two docker container
kadamb
# Is there a way to map ports two docker container

(Asked by: username_0 on 2019-05-10)

Let's consider two docker conatiner named `app` and `mysql`.
I am looking for a way, where I can map `app` and `mysql` ports, so that if I try to connect to `mysql` from `web`, using `localhost:3306` it can connect.

I know I can connect using `dns` and `docker network`. But I am curious if there is a way to do this.

**Tags:** docker, networking, ports

> **Comments:**
> **username_1**: In docker you don't need to think about ips, but service names, you can accomplish that easily using docker-compose, see this for example: http://blog.adnansiddiqi.me/create-your-first-php-mysql-application-in-docker/  
> **username_2**: I think its important that you explain why you want to do this, and the context. Are you confined to a container and you want to get access to another one? Or do you have a legitimate use case and you don't have too much time to spend on learning docker? Or you you want to dynamically add and remove some containers?  
> **username_0**: @username_2 : I have just started working on an application, where dependencies are lot(mysql,redis,rabbit,mongodb etc etc). And for local development, most of the connection string are written as localhost:3306, localhost:11211 etc. I have found a workaround for that, but I was curious without making changes in config code, I can still run the application in docker environment, thats why the question. And the solution I found was to run all dependencies in docker containers, and run application on local, so all dependencies are accessible through localhost:<port_numebr>  

---

## 1 answers

### Answer by username_2 on 2019-05-10. Score: 1

On Linux, containers live in separate network namespaces, so localhost or 127.0.0.1 on the container is not localhost on the host. I'm not sure about other OSes. You could expose a port to the host like this:
docker run -p 3306:3306 mysql

Then you can access it from any container at the host IP address, which for the default network is 172.17.0.1:3306. You can add -p many times, read the docs here: https://docs.docker.com/
All that the -p does on Linux, is essentially:
sudo iptables -t nat -A DOCKER \! -i docker0 -p tcp --dport 3306 -j DNAT --to 172.17.0.2:3306

(Or whatever your mysql container IP address is.) Docker compose and similar utilities make containers able to see each other with --link eg.
docker run --link web --link mysql:localmysql app

The only thing this does is to put the correct IP address for the container in a file called /etc/hosts inside the container (but this might change in future.) 
If you want to use --link, all containers need to be on the same network. You can connect them while running with:
docker network connect mynet web

The main reason for doing this is because the containers don't have fixed IP addresses. You can do:
docker run --network mynet --ip 172.18.0.10 -d app

To give your container a static IP - and then just use that instead of the name. The docker team keeps changing how things work, so you might need to run additional commands to allow different networks to connect to each other. 
You can of course also do all of this manually, on Linux, using the firewall, on the host machine. (Start learning about it here: https://www.google.com/search?q=iptables (Also look at the Images tab!)
iptables -nvxL # shows the filter table
iptables -nvxL -t nat # shows the NAT table where port forwarding is done

To dynamically forward a port to a container, get its IP address, eg 172.18.0.10, then:
iptables -t nat -A POSTROUTING -p tcp --dport 1234 -j DNAT --to 172.18.0.10:4567

Be keenly aware that if you do this, and your container restarts, or gets another IP address, that this will not automatically update, and unless you use iptables-save and iptables-restore in your startup and cron scripts, these manually created rules will disappear. Also that the docker command and compose scripts won't know about any configuration that you manually add like this.
If you really want localhost in your container to point somewhere else, you can edit /etc/hosts inside the container, and change the IP address of localhost - be very keenly aware that other things might break, because localhost is supposed to point to the same host, not another one, so there will be unintended consequences at some point.


---

