From inside of a Docker container, how do I connect to the localhost of the machine?
Asked Answered
M

43

3371

I have a Nginx running inside a docker container. I have a MySql running on the host system. I want to connect to the MySql from within my container. MySql is only binding to the localhost device.

Is there any way to connect to this MySql or any other program on localhost from within this docker container?

This question is different from "How to get the IP address of the docker host from inside a docker container" due to the fact that the IP address of the docker host could be the public IP or the private IP in the network which may or may not be reachable from within the docker container (I mean public IP if hosted at AWS or something). Even if you have the IP address of the docker host it does not mean you can connect to docker host from within the container given that IP address as your Docker network may be overlay, host, bridge, macvlan, none etc which restricts the reachability of that IP address.

Mesoblast answered 20/6, 2014 at 3:54 Comment(5)
Why not bind mysql to docker0 as well?Pilgrim
For Windows Machine :- $docker run -d --name MyWebServer -P httpdThumping
Without network: host you can't go back from a container to the host. Only host to container. This is the main ideology behind containers. They are isolated for both stability and security reasons.Thomasina
You will likely need to make sure your local server accepts connections from anywhere (ie. 0.0.0.0) when starting it as docker containers are isolated from the local network.Mats
I found a solution without network: host that works for host services listening on 127.0.0.1 (not 0.0.0.0). Actually it uses network:host just for a "relay" container. Explained in this blog post.Narrate
F
4613

Edit:

If you are using Docker-for-mac or Docker-for-Windows 18.03+, connect to your mysql service using the host host.docker.internal (instead of the 127.0.0.1 in your connection string).

If you are using Docker-for-Linux 20.10.0+, you can also use the host host.docker.internal if you started your Docker container with the --add-host host.docker.internal:host-gateway option, or added the following snippet in your docker-compose.yml file :

extra_hosts:
    - "host.docker.internal:host-gateway"

Otherwise, read below


TLDR

Use --network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host.

Note: This mode only works on Docker for Linux, per the documentation.


Note on docker container networking modes

Docker offers different networking modes when running containers. Depending on the mode you choose you would connect to your MySQL database running on the docker host differently.

docker run --network="bridge" (default)

Docker creates a bridge named docker0 by default. Both the docker host and the docker containers have an IP address on that bridge.

on the Docker host, type sudo ip addr show docker0 you will have an output looking like:

[vagrant@docker:~] $ sudo ip addr show docker0
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
    inet 172.17.42.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::5484:7aff:fefe:9799/64 scope link
       valid_lft forever preferred_lft forever

So here my docker host has the IP address 172.17.42.1 on the docker0 network interface.

Now start a new container and get a shell on it: docker run --rm -it ubuntu:trusty bash and within the container type ip addr show eth0 to discover how its main network interface is set up:

root@e77f6a1b3740:/# ip addr show eth0
863: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 66:32:13:f0:f1:e3 brd ff:ff:ff:ff:ff:ff
    inet 172.17.1.192/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::6432:13ff:fef0:f1e3/64 scope link
       valid_lft forever preferred_lft forever

Here my container has the IP address 172.17.1.192. Now look at the routing table:

root@e77f6a1b3740:/# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         172.17.42.1     0.0.0.0         UG    0      0        0 eth0
172.17.0.0      *               255.255.0.0     U     0      0        0 eth0

So the IP Address of the docker host 172.17.42.1 is set as the default route and is accessible from your container.

root@e77f6a1b3740:/# ping 172.17.42.1
PING 172.17.42.1 (172.17.42.1) 56(84) bytes of data.
64 bytes from 172.17.42.1: icmp_seq=1 ttl=64 time=0.070 ms
64 bytes from 172.17.42.1: icmp_seq=2 ttl=64 time=0.201 ms
64 bytes from 172.17.42.1: icmp_seq=3 ttl=64 time=0.116 ms

docker run --network="host"

Alternatively you can run a docker container with network settings set to host. Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0.0.1) will refer to the docker host.

Be aware that any port opened in your docker container would be opened on the docker host. And this without requiring the -p or -P docker run option.

IP config on my docker host:

[vagrant@docker:~] $ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:98:dc:aa brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe98:dcaa/64 scope link
       valid_lft forever preferred_lft forever

and from a docker container in host mode:

[vagrant@docker:~] $ docker run --rm -it --network=host ubuntu:trusty ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:98:dc:aa brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe98:dcaa/64 scope link
       valid_lft forever preferred_lft forever

As you can see both the docker host and docker container share the exact same network interface and as such have the same IP address.


Connecting to MySQL from containers

bridge mode

To access MySQL running on the docker host from containers in bridge mode, you need to make sure the MySQL service is listening for connections on the 172.17.42.1 IP address.

To do so, make sure you have either bind-address = 172.17.42.1 or bind-address = 0.0.0.0 in your MySQL config file (my.cnf).

If you need to set an environment variable with the IP address of the gateway, you can run the following code in a container :

export DOCKER_HOST_IP=$(route -n | awk '/UG[ \t]/{print $2}')

then in your application, use the DOCKER_HOST_IP environment variable to open the connection to MySQL.

Note: if you use bind-address = 0.0.0.0 your MySQL server will listen for connections on all network interfaces. That means your MySQL server could be reached from the Internet ; make sure to set up firewall rules accordingly.

Note 2: if you use bind-address = 172.17.42.1 your MySQL server won't listen for connections made to 127.0.0.1. Processes running on the docker host that would want to connect to MySQL would have to use the 172.17.42.1 IP address.

host mode

To access MySQL running on the docker host from containers in host mode, you can keep bind-address = 127.0.0.1 in your MySQL configuration and connect to 127.0.0.1 from your containers:

[vagrant@docker:~] $ docker run --rm -it --network=host mysql mysql -h 127.0.0.1 -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.5.41-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

note: Do use mysql -h 127.0.0.1 and not mysql -h localhost; otherwise the MySQL client would try to connect using a unix socket.

Fulfill answered 20/6, 2014 at 11:46 Comment(28)
Thank you for such a detailed answer! From what I've gathered, using host mode is the only way to get this functionality through localhost. I haven't tried but I would assume you could create a separate network to connect containers over their own bridge offering them a common 'localhost'.Reactivate
There is also a guide on Advanced Networking at docs.docker.com/articles/networkingFulfill
Note for OSX users: log into your docker virtual machine first (boot2docker), using "docker-machine ssh default" , then run "sudo ip addr show docker0". Continue with Thomas' instructions from there.Tarpan
I am running Docker for Mac, and there is no 172.17.42.1 anymore, no docker0 anymore. It was 172.17.0.1 as gateway, and can't even telnet 172.17.0.1 3306Edible
There is a reason they didn't try to make this easy. It's better to have all the dependencies dockerized as well, which allows you to sidestep this whole problem.Determinant
You can mount the mysql socket into the container instead of networking like -v /var/run/mysqld/mysqld.sock:/tmp/mysql.sock this.Defend
Can someone address the situation when your running Docker on Mac and not using boot2docker as such there is no docker0 interface?Gurley
if you get access denied from inside docker container, you may need to allow root access outside localhost, with GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;Deaconess
For anyone trying to connect to a host service and running Docker for Mac. There is special Mac-only DNS name docker.for.mac.host.internal, which resolves to the internal IP address used by the host. sourceAlgernon
The latest version of Docker supports host.docker.internal as a domain name from the container (as per the latest update in @Janne Annala 's answer ). Going forward, that appears to be the more general solution when you still want some level of isolation between the container and host networks.Cantle
@Cantle note that for docker 18.04.0-ce host.docker.internal does not work for Linux. It only works for docker-for-Windows and docker-for-mac. See github.com/docker/for-linux/issues/264Fulfill
--net=host breaks published port translation so that if the container exposes port 80 (using --publish XXX:80 or more simply -p XXX:80 then if a service on the host is already bound to port 80 (a docker instance or a host native service) then the container will not be able to bind the port 80. So it's not good in any situation.Bratton
You can see the mac directions since Docker version 18.03 referencing host.docker.internal hereMakedamakefast
Just tried --network="host" in Docker Desktop for Windows (2.3.0.2) and it worked perfectly fine.Lewert
This is by a fair margin one of the top 5 best answers I've come by on SO! Thomasleveil thx for your ATI ( attention, time, and interest) <3 @iam.Carrot huge kudos for maintaining this answer :plusone: and :thumbsup:Faircloth
This partly solved my problem - host.docker.internal worked perfectly. However, I'm trying to access an IIS site hosted locally, and I get an SSL error because, sure, I don't have an SSL cert for host.docker.internal. Is there a way to generate a local cert for that use?Deutero
@PeterTirrell I’m having the same issue you described. Were you able to find a solution?Jolynnjon
@Jolynnjon I didn't, really. In my case I was using an internal wrapper class around HttpClient to make the call, so I customized the HttpClientHandler.ServerCertificateCustomValidationCallback to basically intercept any SSL validation errors and ignore them if the RequestUri contains "host.docker.internal". It's hacky, but got me on my way for debugging.Deutero
Running docker like: docker run --rm -d -p 5000:5000/tcp mytestimage:latest --network="host" does not work. I get an error that it cannot connect to 127.0.0.1. The only thing that worked was in my C# code, using host.docker.internal instead of localhost or 127.0.0.1. Any idea why this wouldn't work on Docker on Windows?Drida
Outstanding answer! I just wanted to add that to get this same to work in a docker-compose.yml, you must add an extra-hosts entry with host.docker.internal:host-gateway, just like you would with the docker run --add-host. Perhaps @Fulfill could add that to the top edit too. Took me a bit to figure that one out.Thumbsdown
does it works for windows containers - seems not.Maressa
--network="host" in docker run solved my problemShadchan
This works unfortunately only for Docker Desktop. This is for development purpose and will not work in a production environment outside of Docker Desktop for Mac.Tridactyl
So much for "write once ship everywhere".Predictor
I can see http://<servicename>:5000/ is working in docker composeTroxell
What about images without the route command?Soissons
using host.docker.internal:host-gateway works on windows, my issue was my spring application properties was referring to "localhost" and not "host.docker.internal" as mysql hostname hence mysql was not getting connectedModular
host.docker.internal is a feature of Docker Desktop. That is likely why some say it works on Linux and some don't. If you have Docker Desktop for Linux installed it will work, otherwise not. See this answer from the Docker Forums.Strouse
Y
757

For all platforms

Docker v 20.10 and above (since December 14th 2020)

Use your internal IP address or connect to the special DNS name host.docker.internal which will resolve to the internal IP address used by the host.

This is for development purpose and does not work in a production environment outside of Docker Desktop.

Linux caveats

To enable this in Docker on Linux, add --add-host=host.docker.internal:host-gateway to your docker command to enable the feature.

To enable this in Docker Compose on Linux, add the following lines to the container definition:

extra_hosts:
    - "host.docker.internal:host-gateway"

According to some users the special DNS name only works within the Docker's default bridge network, not within custom networks.

For older macOS and Windows versions of Docker

Docker v 18.03 and above (since March 21st 2018)

Use your internal IP address or connect to the special DNS name host.docker.internal which will resolve to the internal IP address used by the host.

Linux support pending https://github.com/docker/for-linux/issues/264

For older macOS versions of Docker

Docker for Mac v 17.12 to v 18.02

Same as above but use docker.for.mac.host.internal instead.

Docker for Mac v 17.06 to v 17.11

Same as above but use docker.for.mac.localhost instead.

Docker for Mac 17.05 and below

To access host machine from the docker container you must attach an IP alias to your network interface. You can bind whichever IP you want, just make sure you're not using it to anything else.

sudo ifconfig lo0 alias 123.123.123.123/24

Then make sure that you server is listening to the IP mentioned above or 0.0.0.0. If it's listening on localhost 127.0.0.1 it will not accept the connection.

Then just point your docker container to this IP and you can access the host machine!

To test you can run something like curl -X GET 123.123.123.123:3000 inside the container.

The alias will reset on every reboot so create a start-up script if necessary.

Solution and more documentation here: https://docs.docker.com/desktop/networking/#use-cases-and-workarounds-for-all-platforms

Yes answered 21/4, 2017 at 11:33 Comment(12)
brilliant and thanks, this worked for me from within the container. mysql -uroot -hdocker.for.mac.localhostMurrelet
docker.for.mac.localhost Is exactly what I was looking for. But this is dirty as hell at the same time. IN docker, one would expect that the hook docker.for.mac.localhost would be a generic docker internal name that would be valid for any operating system, not just for Mac. But for development purposes this is good enough.Hierodule
DNS name docker.for.mac.host.internal should be used instead of docker.for.mac.localhost (still valid) for host resolution from containers, since there is an RFC banning the use of subdomains of localhost. See tools.ietf.org/html/draft-west-let-localhost-be-localhost-06.Wasteland
This doesn't work for me. When I docker run -e HOSTNAME= docker.for.mac.host.internal , the container is created but nothign happens. I have to then crtl+C. With --net=host -e HOSTNAME=localhost at least the container runs and complains that it can't find the service I need (MySQL db).Poteet
thanks a lot, field "docker.for.mac.host.internal" works for macOsAsclepius
Looks like on Linux this works only if the container is on default docker bridge network 172.17.0.0 which is the one used by docker0 interface. This won't work if you create a new network and add containers to it.Laquanda
extra_hosts: - "host.docker.internal:host-gateway" does not work on Linux.Alliterate
added extra_hosts: - "host.docker.internal:host-gateway" in docker-compsoe file and it worked like charm . thanks @JannePopup
It never worked for Linux, so "for all platforms" is misleading, the answer is clearly incorrect.Macguiness
It surely does, the instructions are just incorrect, so they should be removed, and there're multiple comments (including mine) to confirm it.Macguiness
This answer does not work for me on Debian 11. I have added host.docker.internal:host-gateway to extra_hosts, and in the container's /etc/hosts/ I can see 172.17.0.1 host.docker.internal, but from within the container I still get can't connect to remote host (172.17.0.1): Connection refused.Jessjessa
@Jessjessa Are you using a custom network instead of the default docker network? In that case it unfortunately doesn't work on Linux.Yes
A
357

Use

host.docker.internal

instead of

localhost
Amaranthine answered 27/7, 2020 at 9:52 Comment(15)
Perhaps this answer could be expanded and clarified a bit? Does this mean you can still use Bridge mode?Sorosis
@ArnoldRoa this will only work on Mac/Windows not LinuxRigidify
Doesn't work as of 4/26/2021. host.docker.internal resolves to nothing.Gage
Can't replicate @Triynko's issue - this works just fine for me (engine: 20.10.6)Melly
For setting host.docker.internal with docker-compose, see https://mcmap.net/q/40719/-what-is-the-linux-equivalent-of-quot-host-docker-internal-quot-duplicateDissimilitude
Thank you, It is working like a charm on MacInflux
Running WSL2 on Windows 10 (Docker Desktop with WSL backend enabled). This does not work.Woodwind
Excellent. It is working.Emma
root@e8528aaeab39:/app# redis-cli -h "host.docker.internal" Could not connect to Redis at host.docker.internal:6379: Name or service not known not connected>Carpic
... and how do you get this done? The ServiceHost entry in my LaunchSettings.json" file seems to be replaced by localhost, while I would like it to be replaced by host.docker.internal, but how can I do this?Thigh
I found this video very useful to understand Docker networking and not just copy/paste brute force until it works youtube.com/watch?v=bKFMS5C4CG0&ab_channel=NetworkChuckViehmann
Thanks! This resolved the issue. I was using mac with network -> driver: host setting.Thegn
You save me a lot of time. Thank you <3Utter
This is working for me Docker Desktop 4.28.0Redmer
Just pointing out the official documentation about this -> docs.docker.com/desktop/networking/…Terminable
O
131

I doing a hack similar to above posts of get the local IP to map to a alias name (DNS) in the container. The major problem is to get dynamically with a simple script that works both in Linux and OSX the host IP address. I did this script that works in both environments (even in Linux distribution with "$LANG" != "en_*" configured):

ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1

So, using Docker Compose, the full configuration will be:

Startup script (docker-run.sh):

export DOCKERHOST=$(ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1)
docker-compose -f docker-compose.yml up

docker-compose.yml:

myapp:
  build: .
  ports:
    - "80:80"
  extra_hosts:
    - "dockerhost:$DOCKERHOST"

Then change http://localhost to http://dockerhost in your code.

For a more advance guide of how to customize the DOCKERHOST script, take a look at this post with a explanation of how it works.

Olpe answered 3/8, 2016 at 21:30 Comment(5)
Depending on your use case, you might even just get away with using the DOCKERHOST value here instead of "localhost" or 0.0.0.0 in whatever service your docker container needs to connect to locally.Pawsner
I have slightly modified your solution to be compatible with custom networks: export DOCKERHOST=$(docker network inspect --format='{{range .IPAM.Config}}{{.Gateway}}{{end}}' <NETWORK-NAME> | awk -F "/" 'NR==1{print $1}') Where <NETWORK-NAME> could be bridge or the name of the network as defined by docker-compose (usually path-name-network_name).Zwolle
You need to add a comment: use dockerhost as the host for db connection (usualy replace for localhost in config file).Ridley
weird solution, but its the only that make xdebug work under docker with php cliPlutonian
Acl stopped working after this solution in haproxy. Any idea why?Oneil
H
60

Solution for Linux (kernel >=3.6).

Ok, your localhost server has a default docker interface docker0 with IP address 172.17.0.1. Your container started with default network settings --net="bridge".

  1. Enable route_localnet for docker0 interface:

    $ sysctl -w net.ipv4.conf.docker0.route_localnet=1
    
  2. Add these rules to iptables:

    $ iptables -t nat -I PREROUTING -i docker0 -d 172.17.0.1 -p tcp --dport 3306 -j DNAT --to 127.0.0.1:3306
    $ iptables -t filter -I INPUT -i docker0 -d 127.0.0.1 -p tcp --dport 3306 -j ACCEPT
    
  3. Create MySQL user with access from '%' that means - from anyone, excluding localhost:

    CREATE USER 'user'@'%' IDENTIFIED BY 'password';
    
  4. Change in your script the mysql-server address to 172.17.0.1.

From the kernel documentation:

route_localnet - BOOLEAN: Do not consider loopback addresses as martian source or destination while routing. This enables the use of 127/8 for local routing purposes (default FALSE).

Hydrops answered 27/9, 2017 at 13:22 Comment(5)
What is the purpose of second iptable command? I can understand the first one is to rewrite all tcp destination that match 172.17.0.1:3306 to 127.0.0.1:3306 but why is the second iptable command necessary?Ruelas
This made my day! Thank you for sharingSpecs
finally. I struggled to find the right IP Address. I didnt know there was a default interface for docker.Foresail
Strangely this is not stable for me, at least on latest CentOS 7 running kernel 3.10 with Docker 19.03.15. It works initially, then after some time stops working.Silo
Depending on your firewall/iptables configuration (-P DROP, ...), you might want to add a more generic entry that allows the whole docker range in. I ended up using this iptables -I INPUT -s 172.16.0.0/12 -j ACCEPT to allow connections to host.docker.internalGenerator
T
57

This worked for me on an NGINX/PHP-FPM stack without touching any code or networking where the app's just expecting to be able to connect to localhost

Mount mysqld.sock from the host to inside the container.

Find the location of the mysql.sock file on the host running mysql:
netstat -ln | awk '/mysql(.*)?\.sock/ { print $9 }'

Mount that file to where it's expected in the docker:
docker run -v /hostpath/to/mysqld.sock:/containerpath/to/mysqld.sock

Possible locations of mysqld.sock:

/tmp/mysqld.sock
/var/run/mysqld/mysqld.sock 
/var/lib/mysql/mysql.sock
/Applications/MAMP/tmp/mysql/mysql.sock # if running via MAMP
Townswoman answered 27/5, 2015 at 13:36 Comment(6)
This is a much cleaner solution, not exposing Mysql to the outside (if not using a firewall).Kathaleenkatharevusa
Sockets do not scale as well as TCP because they block more often and can cause weird behavior. Use TCP whenever possible.Atomy
@JoelESalas Do you have a source for that claim?Galvanoscope
I have found this to be the easiest solution. Thumbs up user833482! You should contribute more often to StackOverflow.Galvanoscope
@JoelESalas I think you're mistaken. The mysql client library even uses unix sockets by default when connecting to localhost rather than actually making a connection to localhost. A unix socket avoids the overhead of the TCP stack and routing, and should run faster.Yamada
This solution ultimately worked for my use case of connecting logstash running in a container to MariaDB running on my host using JDBC. However, there is a catch that you have to include JNA dependency. Leaving this as a breadcrumb for others: #25918916Arrogant
C
46

Until host.docker.internal is working for every platform, you can use my container acting as a NAT gateway without any manual setup:

https://github.com/qoomon/docker-host

Circumnutate answered 17/10, 2018 at 15:8 Comment(6)
I can confirm this doesn't work in Docker for Windows 19.03.2 with Windows container.Lizabethlizard
any idea how to fix that? (I'm not a windows user)Circumnutate
If you environment doesn't block port mysql uses, you can refer to the server by the computer name it is hosted. So in you connection string, use your computer name as a server name.Lizabethlizard
I can confirm this does NOT WORK on Docker for Windows v. 20.10.5. It's infuriating. The IP of the host (from the container) changes randomly. How the hell am I supposed to deploy a connection string when the host IP changes at runtime and host.docker.internal resolves to nothing?Gage
@Gage how do you determine the host ip currently?Circumnutate
@Gage may you can run following commands and tell me if any of these succeed? docker run --rm alpine ping host.docker.internal docker run --rm alpine ping docker.for.win.localhost docker run --rm alpine ping gateway.docker.internalCircumnutate
P
33

Simplest solution for Mac OSX

Just use the IP address of your Mac. On the Mac run this to get the IP address and use it from within the container:

ifconfig | grep 'inet 192'| awk '{ print $2}'

As long as the server running locally on your Mac or in another docker container is listening to 0.0.0.0, the docker container will be able to reach out at that address.

If you just want to access another docker container that is listening on 0.0.0.0 you can use 172.17.0.1

Prophets answered 17/5, 2017 at 18:15 Comment(2)
Docker for Mac exposes the docker.for.mac.host.internal hostname now.Scratches
host.docker.internal is available on mac. More info here: docs.docker.com/desktop/networking/…Institute
F
28

Several solutions come to mind:

  1. Move your dependencies into containers first
  2. Make your other services externally accessible and connect to them with that external IP
  3. Run your containers without network isolation
  4. Avoid connecting over the network, use a socket that is mounted as a volume instead

The reason this doesn't work out of the box is that containers run with their own network namespace by default. That means localhost (or 127.0.0.1 pointing to the loopback interface) is unique per container. Connecting to this will connect to the container itself, and not services running outside of docker or inside of a different docker container.

Option 1: If your dependency can be moved into a container, I would do this first. It makes your application stack portable as others try to run your container on their own environment. And you can still publish the port on your host where other services that have not been migrated can still reach it. You can even publish the port to the localhost interface on your docker host to avoid it being externally accessible with a syntax like: -p 127.0.0.1:3306:3306 for the published port.

Option 2: There are a variety of ways to detect the host IP address from inside of the container, but each have a limited number of scenarios where they work (e.g. requiring Docker for Mac). The most portable option is to inject your host IP into the container with something like an environment variable or configuration file, e.g.:

docker run --rm -e "HOST_IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')" ...

This does require that your service is listening on that external interface, which could be a security concern. For other methods to get the host IP address from inside of the container, see this post.

Slightly less portable is to use host.docker.internal. This works in current versions of Docker for Windows and Docker for Mac. And in 20.10, the capability has been added to Docker for Linux when you pass a special host entry with:

docker run --add-host host.docker.internal:host-gateway ...

The host-gateway is a special value added in Docker 20.10 that automatically expands to a host IP. For more details see this PR.

Option 3: Running without network isolation, i.e. running with --net host, means your application is running on the host network namespace. This is less isolation for the container, and it means you cannot access other containers over a shared docker network with DNS (instead, you need to use published ports to access other containerized applications). But for applications that need to access other services on the host that are only listening on 127.0.0.1 on the host, this can be the easiest option.

Option 4: Various services also allow access over a filesystem based socket. This socket can be mounted into the container as a bind mounted volume, allowing you to access the host service without going over the network. For access to the docker engine, you often see examples of mounting /var/run/docker.sock into the container (giving that container root access to the host). With mysql, you can try something like -v /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysql.sock and then connect to localhost which mysql converts to using the socket.

Flunky answered 30/6, 2019 at 15:28 Comment(2)
for option2 (the only thing that worked from all the answers) : the following command gets the the ip address of an interface other than local host on both osx and linux: ifconfig | grep 'inet ' | grep -v 127.0.0.1 | awk '{ print $2 }' | head -1 | sed -n 's/[^0-9]*\([0-9\.]*\)/\1/p'Ailsa
--add-host host.docker.internal:host-gateway is goldenPhraseograph
L
26

Very simple and quick, check your host IP with ifconfig (linux) or ipconfig (windows) and then create a docker-compose.yml:

version: '3' # specify docker-compose version
services:
  nginx:
    build: ./ # specify the directory of the Dockerfile
    ports:
      - "8080:80" # specify port mapping
    extra_hosts:
      - "dockerhost:<yourIP>"

This way, your container will be able to access your host. When accessing your DB, remember to use the name you specified before, in this case dockerhost and the port of your host in which the DB is running.

Leniency answered 22/3, 2019 at 2:40 Comment(2)
In HaProxy, this solution has stopped working of the acl's for some reason, only the default setting is working.Oneil
This worked for me! I'm using docker (not docker desktop) inside wsl 2 on Windows. I need to make request from inside container using HttpURLConnection (java) to localhost/test.php in host machine running by XAMPP. Get your local network IP of Windows PC and add to <yourIP> above, then use dockerhost as domain when making request.Mana
D
23

Solution for Windows 10

Docker Community Edition 17.06.0-ce-win18 2017-06-28 (stable)

You can use DNS name of the host docker.for.win.localhost, to resolve to the internal IP. (Warning some sources mentioned windows but it should be win)

Overview
I needed to do something similar, that is connect from my Docker container to my localhost, which was running the Azure Storage Emulator and CosmosDB Emulator.

The Azure Storage Emulator by default listens on 127.0.0.1, while you can change the IP its bound too, I was looking for a solution that would work with default settings.

This also works for connecting from my Docker container to SQL Server and IIS, both running locally on my host with default port settings.

Donaldson answered 9/11, 2017 at 10:49 Comment(1)
Unfortunately, currently, windows (at least docker desktop) is not supporting --net=host, check docs.docker.com/network/network-tutorial-host/#prerequisitesHit
P
22

For windows,

I have changed the database url in spring configuration: spring.datasource.url=jdbc:postgresql://host.docker.internal:5432/apidb

Then build the image and run. It worked for me.

Philippeville answered 7/2, 2020 at 15:51 Comment(1)
for me doesnt work. I have mac and trying from a php container to connect to localhost mysql. Any idea ?Balneology
I
17

None of the answers worked for me when using Docker Toolbox on Windows 10 Home, but 10.0.2.2 did, since it uses VirtualBox which exposes the host to the VM on this address.

Immaterial answered 9/11, 2018 at 22:23 Comment(3)
It works. Even no need to specify --network=host. looks like 10.0.2.2 is set as a default IP for the host. Thanks.Accommodative
But, Can I use this static IP for all the versions of Windows and Mac?, how can I handle it for multiple platforms via script?Roxi
This worked for me. Just run ipconfig on your host (windows) and get the IP address under Ethernet adapter vEthernet (DockerNAT)Consummate
C
17

This is not an answer to the actual question. This is how I solved a similar problem. The solution comes totally from: Define Docker Container Networking so Containers can Communicate. Thanks to Nic Raboy

Leaving this here for others who might want to do REST calls between one container and another. Answers the question: what to use in place of localhost in a docker environment?

Get how your network looks like docker network ls

Create a new network docker network create -d my-net

Start the first container docker run -d -p 5000:5000 --network="my-net" --name "first_container" <MyImage1:v0.1>

Check out network settings for first container docker inspect first_container. "Networks": should have 'my-net'

Start the second container docker run -d -p 6000:6000 --network="my-net" --name "second_container" <MyImage2:v0.1>

Check out network settings for second container docker inspect second_container. "Networks": should have 'my-net'

ssh into your second container docker exec -it second_container sh or docker exec -it second_container bash.

Inside of the second container, you can ping the first container by ping first_container. Also, your code calls such as http://localhost:5000 can be replaced by http://first_container:5000

Chema answered 13/7, 2019 at 13:12 Comment(2)
Exactly what i was looking for. Thanks :DDilly
If you know it is not the answer to the question (which it is not, because the question specifically asks about localhost), why are you posting it?Photometry
H
17

For Linux, where you cannot change the interface the localhost service binds to

There are two problems we need to solve

  1. Getting the IP of the host
  2. Making our localhost service available to Docker

The first problem can be solved using qoomon's docker-host image, as given by other answers.

You will need to add this container to the same bridge network as your other container so that you can access it. Open a terminal inside your container and ensure that you can ping dockerhost.

bash-5.0# ping dockerhost
PING dockerhost (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: seq=0 ttl=64 time=0.523 ms

Now, the harder problem, making the service accessible to docker.

We can use telnet to check if we can access a port on the host (you may need to install this).

The problem is that our container will only be able to access services that bind to all interfaces, such as SSH:

bash-5.0# telnet dockerhost 22
SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3

But services bound only to localhost will be inaccessible:

bash-5.0# telnet dockerhost 1025
telnet: can't connect to remote host (172.20.0.2): Connection refused

The proper solution here would be to bind the service to dockers bridge network. However, this answer assumes that it is not possible for you to change this. So we will instead use iptables.

First, we need to find the name of the bridge network that docker is using with ifconfig. If you are using an unnamed bridge, this will just be docker0. However, if you are using a named network you will have a bridge starting with br- that docker will be using instead. Mine is br-5cd80298d6f4.

Once we have the name of this bridge, we need to allow routing from this bridge to localhost. This is disabled by default for security reasons:

sysctl -w net.ipv4.conf.<bridge_name>.route_localnet=1

Now to set up our iptables rule. Since our container can only access ports on the docker bridge network, we are going to pretend that our service is actually bound to a port on this network.

To do this, we will forward all requests to <docker_bridge>:port to localhost:port

iptables -t nat -A PREROUTING -p tcp -i <docker_bridge_name> --dport <service_port> -j DNAT --to-destination 127.0.0.1:<service_port>

For example, for my service on port 1025

iptables -t nat -A PREROUTING -p tcp -i br-5cd80298d6f4 --dport 1025 -j DNAT --to-destination 127.0.0.1:1025

You should now be able to access your service from the container:

bash-5.0# telnet dockerhost 1025
220 127.0.0.1 ESMTP Service Ready
Horner answered 7/9, 2019 at 12:12 Comment(1)
This is the same as what @ray-d has mentioned above, but is nicely explained. Thank you! Additionally, when using docker-compose, I had to also add a DNAT rule in the PREROUTING chain for all packets arriving on the docker0 interface too: because, docker-compose seem to be using docker0 during builds (not during runs, surprisingly): sysctl -w net.ipv4.conf.docker0.route_localnet=1 and iptables -t nat -A PREROUTING -p tcp -i docker0 --dport 1025 -j DNAT --to-destination 127.0.0.1:1025Stringent
P
17

If you're running with --net=host, localhost should work fine. If you're using default networking, use the static IP 172.17.0.1.

See this - https://mcmap.net/q/40719/-what-is-the-linux-equivalent-of-quot-host-docker-internal-quot-duplicate

Pruter answered 4/2, 2022 at 12:3 Comment(2)
I confirm that --net=host solve the issueSummand
172.17.0.1 worked on Ubuntu 18.04. Bookmarked this answer because I have to look it up every year or so ;)Polybasite
E
15

For those on Windows, assuming you're using the bridge network driver, you'll want to specifically bind MySQL to the IP address of the hyper-v network interface.

This is done via the configuration file under the normally hidden C:\ProgramData\MySQL folder.

Binding to 0.0.0.0 will not work. The address needed is shown in the docker configuration as well, and in my case was 10.0.75.1.

Eyeshade answered 10/12, 2016 at 15:53 Comment(2)
You deserve a medal! I've been working on this for two full days. Thank you for helping out!Bentwood
I was also working on this for two full days. This is the only place on the web I've found it mentioned. Microsoft is completely silent on this when it talks about connecting to MSSQL from within a Docker container. It makes you wonder if they ever got it working themselves!Decarburize
D
13

First see this answer for the options that you have to fix this problem. But if you use docker-compose you can add network_mode: host to your service and then use 127.0.0.1 to connect to the local host. This is just one of the options described in the answer above. Below you can find how I modified docker-compose.yml from https://github.com/geerlingguy/php-apache-container.git:

 ---
 version: "3"
 services:
   php-apache:
+    network_mode: host
     image: geerlingguy/php-apache:latest
     container_name: php-apache
...

+ indicates the line I added.


[Additional info] This has also worked in version 2.2. and "host" or just 'host' are both worked in docker-compose.

 ---
 version: "2.2"

 services:
   php-apache:
+    network_mode: "host"
        or
+    network_mode: host
...
Davita answered 21/11, 2020 at 13:45 Comment(1)
Yup. Its works with: network_mode: host Now can access local /etc/hosts domain names who points to another project docker containers.Trollope
R
12

Edit: I ended up prototyping out the concept on GitHub. Check out: https://github.com/sivabudh/system-in-a-box


First, my answer is geared towards 2 groups of people: those who use a Mac, and those who use Linux.

The host network mode doesn't work on a Mac. You have to use an IP alias, see: https://mcmap.net/q/40392/-from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-machine

What is a host network mode? See: https://docs.docker.com/engine/reference/run/#/network-settings

Secondly, for those of you who are using Linux (my direct experience was with Ubuntu 14.04 LTS and I'm upgrading to 16.04 LTS in production soon), yes, you can make the service running inside a Docker container connect to localhost services running on the Docker host (eg. your laptop).

How?

The key is when you run the Docker container, you have to run it with the host mode. The command looks like this:

docker run --network="host" -id <Docker image ID>

When you do an ifconfig (you will need to apt-get install net-tools your container for ifconfig to be callable) inside your container, you will see that the network interfaces are the same as the one on Docker host (eg. your laptop).

It's important to note that I'm a Mac user, but I run Ubuntu under Parallels, so using a Mac is not a disadvantage. ;-)

And this is how you connect NGINX container to the MySQL running on a localhost.

Rafaelita answered 7/2, 2017 at 7:15 Comment(5)
It's important to note that the host mode offers better performance since it uses the OS network stack.Rafaelita
Very good point there. Although it IS possible to connect from a container to a host service with unused IP attach docs.docker.com/docker-for-mac/networking . Not a pleasant solution... but works.Together
Good stuff here. Once inside the container with --network="host", how does one connect to the host mysql for example?Noelyn
@Buccleuch Just use localhost. Check out my GitHub source code: github.com/sivabudh/system-in-a-box/blob/master/dj_host_docker/…. Search for 'HOST', you will see 127.0.0.1 to connect to Postgresql.Rafaelita
I was able to get access to the host mysql databases by mounting the volume as per @user833482, and after installing mysql-client and server on the docker container of course.Noelyn
B
9

I disagree with the answer from Thomasleveil.

Making mysql bind to 172.17.42.1 will prevent other programs using the database on the host to reach it. This will only work if all your database users are dockerized.

Making mysql bind to 0.0.0.0 will open the db to outside world, which is not only a very bad thing to do, but also contrary to what the original question author wants to do. He explicitly says "The MySql is running on localhost and not exposing a port to the outside world, so its bound on localhost"

To answer the comment from ivant

"Why not bind mysql to docker0 as well?"

This is not possible. The mysql/mariadb documentation explicitly says it is not possible to bind to several interfaces. You can only bind to 0, 1, or all interfaces.

As a conclusion, I have NOT found any way to reach the (localhost only) database on the host from a docker container. That definitely seems like a very very common pattern, but I don't know how to do it.

Bitthia answered 12/3, 2015 at 23:31 Comment(4)
from the docker host you could still connect to the MySQL server using the 172.17.42.1 address. But your note is right otherwise. Aso, I edited my answer with the host networking mode that allows to keep the MySQL server bound to 127.0.0.1 while allowing containers to connect to itFulfill
no, as I said, you can't connect to 172.17.42.1 if mysql is bound to localhost.Bitthia
"Making mysql bind to 172.17.42.1 will prevent other programs using the database on the host to reach it." - that is not true. Other programs can use mysql, they just have to connect to 172.17.42.1 instead of localhost/127.0.0.1.Eskridge
The solution to the problem in this answer generally is to get the MySQL server to bind to 0.0.0.0 and then set up a firewall so that the db is not accessible from the internet.Radom
S
9

Try this:

version: '3.5'
services:
  yourservice-here:
    container_name: container_name
    ports:
      - "4000:4000"
    extra_hosts: # <---- here
      - localhost:192.168.1.202
      - or-vitualhost.local:192.168.1.202

To get 192.168.1.202, uses ifconfig

This worked for me. Hope this help!

Stratfordonavon answered 25/4, 2020 at 5:26 Comment(1)
This is a neat little trick. I haven't tried it but that's a pretty handy quick solutionAppurtenance
D
7

In 7 years the question was asked, it is either docker has changed, or no one tried this way. So I will include my own answer.

I have found all answers use complex methods. Today, I have needed this, and found 2 very simple ways:

  • use ipconfig or ifconfig on your host and make note of all IP addresses. At least two of them can be used by the container.

    • I have a fixed local network address on WiFi LAN Adapter: 192.168.1.101. This could be 10.0.1.101. the result will change depending on your router
    • I use WSL on windows, and it has its own vEthernet address: 172.19.192.1
  • use host.docker.internal. Most answers have this or another form of it depending on OS. The name suggests it is now globally used by docker.

A third option is to use WAN address of the machine, or in other words IP given by the service provider. However, this may not work if IP is not static, and requires routing and firewall settings.

Dissertate answered 26/1, 2022 at 21:15 Comment(2)
This is huge for wsl users. host.docker.internal is usually the answer....but if you're using wsl, there will be multiple ip addresses that docker will have to disambiguate from. This can lead to SUPER annoying issues where you have a multiple servers running locally on different ports, but you can only hit a couple with host.docker.internal.Cornhusk
this answer is great, but in production, the local IP address might change, in local networks (network in our homes) we can set our IP address to static, and this method should work for development, but if you're for example going to send your project with the docker compose file fixed to a hardcoded IP address, then the project will not workChemarin
P
6

You need to know the gateway! My solution with local server was to expose it under 0.0.0.0:8000, then run docker with subnet and run container like:

docker network create --subnet=172.35.0.0/16 --gateway 172.35.0.1 SUBNET35
docker run -d -p 4444:4444 --net SUBNET35 <container-you-want-run-place-here>

So, now you can access your loopback through http://172.35.0.1:8000

Picturize answered 23/1, 2020 at 12:45 Comment(0)
R
5

Connect to the gateway address.

❯ docker network inspect bridge | grep Gateway
                    "Gateway": "172.17.0.1"

Make sure the process on the host is listening on this interface or on all interfaces and is started after docker. If using systemd, you can add the below to make sure it is started after docker.

[Unit]
After=docker.service

Example

❯ python -m http.server &> /dev/null &
[1] 149976

❯ docker run --rm python python -c  "from urllib.request import urlopen;print(b'Directory listing for' in urlopen('http://172.17.0.1:8000').read())" 
True
Rhinelandpalatinate answered 22/1, 2021 at 21:39 Comment(0)
M
5

On linux if you can ping the host.docker.internal from inside the container, but you can not reach the ports that are opened on your localhost, please make sure you have docker installed not in rootless mode. If it is in rootless mode it is limited. Quote from documentation:

IPAddress shown in docker inspect is namespaced inside RootlessKit's network namespace. This means the IP address is not reachable from the host without nsenter-ing into the network namespace.

Host network (docker run --net=host) is also namespaced inside RootlessKit.

Hope this helps someone, as I spend several days on it

Mallard answered 4/10, 2023 at 8:23 Comment(1)
You can add an entry to /etc/hosts with your IP address and the hostname host.docker.internal. To avoid having to update the entry each time you connect to a different network, it might be worth creating a virtual network interface with a static IP address. See also this issue.Shawm
R
4

Here is my solution : it works for my case

  • set local mysql server to public access by comment #bind-address = 127.0.0.1 in /etc/mysql/mysql.conf.d

  • restart mysql server sudo /etc/init.d/mysql restart

  • run the following command to open user root access any host mysql -uroot -proot GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES;

  • create sh script : run_docker.sh

    #!bin/bash

    HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print \$2}' | cut -d / -f 1`


      docker run -it -d --name web-app \
                  --add-host=local:${HOSTIP} \
                  -p 8080:8080 \
                  -e DATABASE_HOST=${HOSTIP} \
                  -e DATABASE_PORT=3306 \
                  -e DATABASE_NAME=demo \
                  -e DATABASE_USER=root \
                  -e DATABASE_PASSWORD=root \
                  sopheamak/springboot_docker_mysql

  
  • run with docker-composer

    version: '2.1'
    
    

    services:
    tomcatwar: extra_hosts: - "local:10.1.2.232" image: sopheamak/springboot_docker_mysql
    ports: - 8080:8080 environment: - DATABASE_HOST=local - DATABASE_USER=root - DATABASE_PASSWORD=root - DATABASE_NAME=demo - DATABASE_PORT=3306

Ribbonfish answered 17/7, 2017 at 7:47 Comment(0)
F
4

You can simply do ifconfig on host machine to check your host IP, then connect to the ip from within your container, works perfectly for me.

Faubert answered 30/11, 2020 at 6:27 Comment(1)
Worked for me. I used the ifconfig command and took thefirst inet ip and it seems rightAmato
E
3

You can get the host ip using alpine image

docker run --rm alpine ip route | awk 'NR==1 {print $3}'

This would be more consistent as you're always using alpine to run the command.

Similar to Mariano's answer you can use same command to set an environment variable

DOCKER_HOST=$(docker run --rm alpine ip route | awk 'NR==1 {print $3}') docker-compose up
Euraeurasia answered 11/6, 2018 at 8:47 Comment(0)
J
2

The CGroups and Namespaces are playing major role in the Container Ecosystem.

Namespace provide a layer of isolation. Each container runs in a separate namespace and its access is limited to that namespace. The Cgroups controls the resource utilization of each container, whereas Namespace controls what a process can see and access the respective resource.

Here is the basic understanding of the solution approach you could follow,

Use Network Namespace

When a container spawns out of image, a network interface is defined and create. This gives the container unique IP address and interface.

$ docker run -it alpine ifconfig

By changing the namespace to host, cotainers networks does not remain isolated to its interface, the process will have access to host machines network interface.

$ docker run -it --net=host alpine ifconfig

If the process listens on ports, they'll be listened on the host interface and mapped to the container.

Use PID Namespace By changing the Pid namespace allows a container to interact with other process beyond its normal scope.

This container will run in its own namespace.

$ docker run -it alpine ps aux

By changing the namespace to the host, the container can also see all the other processes running on the system.

$ docker run -it --pid=host alpine ps aux

Sharing Namespace

This is a bad practice to do this in production because you are breaking out of the container security model which might open up for vulnerabilities, and easy access to eavesdropper. This is only for debugging tools and understating the loopholes in container security.

The first container is nginx server. This will create a new network and process namespace. This container will bind itself to port 80 of newly created network interface.

$ docker run -d --name http nginx:alpine

Another container can now reuse this namespace,

$ docker run --net=container:http mohan08p/curl curl -s localhost

Also, this container can see the interface with the processes in a shared container.

$ docker run --pid=container:http alpine ps aux

This will allow you give more privileges to containers without changing or restarting the application. In the similar way you can connect to mysql on host, run and debug your application. But, its not recommend to go by this way. Hope it helps.

Jordain answered 6/9, 2017 at 12:5 Comment(0)
X
2

Until fix is not merged into master branch, to get host IP just run from inside of the container:

ip -4 route list match 0/0 | cut -d' ' -f3

(as suggested by @Mahoney here).

Xiomaraxiong answered 22/3, 2019 at 14:10 Comment(0)
E
2

I solved it by creating a user in MySQL for the container's ip:

$ sudo mysql<br>
mysql> create user 'username'@'172.17.0.2' identified by 'password';<br>
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on database_name.* to 'username'@'172.17.0.2' with grant option;<br>
Query OK, 0 rows affected (0.00 sec)

$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
<br>bind-address        = 172.17.0.1

$ sudo systemctl restart mysql.service

Then on container: jdbc:mysql://<b>172.17.0.1</b>:3306/database_name

Expressivity answered 27/9, 2019 at 2:32 Comment(1)
This the simplest solution. It has worked for me and I have referred manyAlexaalexander
D
2

To make everything work, you need to create a config for your server (caddy, nginx) where the main domain will be "docker.for.mac.localhost". For this replace in baseURL "http://localhost/api" on "http://docker.for.mac.localhost/api"

docker-compose.yml

backend:
  restart: always
  image: backend
  build:
    dockerfile: backend.Dockerfile
    context: .
  environment:
    # add django setting.py os.getenv("var") to bd config and ALLOWED_HOSTS CORS_ORIGIN_WHITELIST
    DJANGO_ALLOWED_PROTOCOL: http
    DJANGO_ALLOWED_HOSTS: docker.for.mac.localhost
    POSTGRES_PASSWORD: 123456
    POSTGRES_USER: user
    POSTGRES_DB: bd_name
    WAITDB: "1"
  volumes:
    - backend_static:/app/static
    - backend_media:/app/media
  depends_on:
    - db

frontend:
  restart: always
  build:
    dockerfile: frontend.Dockerfile
    context: .
  image: frontend
  environment:
    #  replace baseURL for axios
    API_URL: http://docker.for.mac.localhost/b/api
    API_URL_BROWSER: http://docker.for.mac.localhost/b/api
    NUXT_HOST: 0.0.0.0
  depends_on:
    - backend

caddy:
  image: abiosoft/caddy
  restart: always
  volumes:
    - $HOME/.caddy:/root/.caddy
    - ./Caddyfile.local:/etc/Caddyfile
    - backend_static:/static
    - backend_media:/media
  ports:
  - 80:80
  depends_on:
    - frontend
    - backend
    - db

Caddyfile.local

http://docker.for.mac.localhost {

  proxy /b backend:5000 {
    header_upstream Host {host}
    header_upstream X-Real-IP {remote}
    header_upstream X-Forwarded-For {remote}
    header_upstream X-Forwarded-Port {server_port}
    header_upstream X-Forwarded-Proto {scheme}
  }

  proxy / frontend:3000 {
    header_upstream Host {host}
    header_upstream X-Real-IP {remote}
    header_upstream X-Forwarded-For {remote}
    header_upstream X-Forwarded-Port {server_port}
    header_upstream X-Forwarded-Proto {scheme}
  }

  root /

  log stdout
  errors stdout
  gzip
}

http://docker.for.mac.localhost/static {
  root /static
}

http://docker.for.mac.localhost/media {
  root /media
}

django settings.py

ALLOWED_HOSTS = [os.getenv("DJANGO_ALLOWED_HOSTS")]
    
CORS_ORIGIN_WHITELIST = [f'{os.getenv("DJANGO_ALLOWED_PROTOCOL")}://{os.getenv("DJANGO_ALLOWED_HOSTS")}']

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": os.getenv("POSTGRES_DB"),
        "USER": os.getenv("POSTGRES_USER"),
        "PASSWORD": os.getenv("POSTGRES_PASSWORD"),
        "HOST": "db",
        "PORT": "5432",
    }
}

nuxt.config.js (the baseURL variable will override API_URL of environment)

axios: {
  baseURL: 'http://127.0.0.1:8000/b/api'
},
Discernible answered 2/7, 2020 at 5:55 Comment(0)
C
2

if you use docker-compose, maybe it can work:

iptables -I INPUT ! -i eth0 -p tcp --dport 8001 -j ACCEPT

the eth0 is your network interface that connect internet, and 8081 the host server port

the best way for iptables rule is iptables TRACE

Commutate answered 15/7, 2020 at 1:25 Comment(0)
A
2

you can use net alias for your machine

OSX

sudo ifconfig lo0 alias 123.123.123.123/24 up

LINUX

sudo ifconfig lo:0  123.123.123.123 up

then from the container you can see the machine by 123.123.123.123

Avalokitesvara answered 2/7, 2022 at 3:50 Comment(0)
H
2

Unfortunately, currently, Windows (at least docker desktop) is not supporting --net=host

Quoted from: https://docs.docker.com/network/network-tutorial-host/#prerequisites

The host networking driver only works on Linux hosts, and is not supported on Docker for Mac, Docker for Windows, or Docker EE for Windows Server.

You can try to use https://docs.docker.com/toolbox/

Hit answered 31/10, 2022 at 21:49 Comment(0)
I
2

An additional point might be useful, especially for NGINX server configuration within a docker container. So, if your host service is supposed to read a client's url for different purposes, there is a possibility to define it, just slightly modifying the approach mentioned before in the answers:

--add-host brand-A-client.local:host-gateway

or in a docker-compose.yml

extra_hosts:
- brand-B-client.local:host-gateway

And refer to them respectively within containers. Applicable for Docker Windows/Macos/Linux.

Inclining answered 15/11, 2022 at 19:43 Comment(0)
A
1

the server on the host is listening on port 5000; it will send a string response from host back as a response

echo "response from host" | nc -l -p 5000

the docker is running in bridge mode by default (that is more secure than running in host network mode); the process in the docker gets the ipv4 address for the first network interface on the host that is not 127.0.0.1; this ipv4 address is passed to the docker via environment variable HOSTIP ; inside the docker there is a nc command that connects to the server on the host ip and port 5000; the command also reads the response of the server.

HOST=$(ifconfig | grep 'inet ' | grep -v 127.0.0.1 | awk '{ print $2 }' | head -1 | sed -n 's/[^0-9]*\([0-9\.]*\)/\1/p'); docker run -e HOSTIP="${HOST}" --rm -it alpine /bin/sh -c 'echo "greetings from container" | nc $HOSTIP 5000 -v'

The expression that computes the ipv4 address of the first listed interface does work on osx and linux:

HOST=$(ifconfig | grep 'inet ' | grep -v 127.0.0.1 | awk '{ print $2 }' | head -1 | sed -n 's/[^0-9]*\([0-9\.]*\)/\1/p')

to make it even more general: you can run the server inside another docker container, that would look as follows:

docker run --expose 5000 -p :5000:5000 --rm -it alpine /bin/sh -c 'echo "response from host" | nc -l -p 5000'

--expose 5000 the docker container can accept connection on port 5000

-p :5000:5000 the docker engine on the host side is listening on port 5000 on any interface and is forwarding connections to port 5000 on the container network (where nc is running).

Ailsa answered 23/3, 2021 at 10:19 Comment(0)
R
1

Despite lot of long, confusing answers a short one. If your docker bridge network is 172.17.0.0/16, do the following things:

  1. Bind your container to 172.17.0.1 eg. -p 172.17.0.1:8080:8080 (whatever is the gateway in your Docker bridge network)
  2. Just access 172.17.0.1 port 8080 eg. curl http://172.17.0.1:8080/ from other container
Rosarosabel answered 25/5, 2021 at 19:0 Comment(0)
H
1

For me enetring this command on the host to get the ip did the trick:

docker run busybox ping -c 1 host.docker.internal | awk 'FNR==2 {print $4}' | sed s'/.$//'

or on mac

docker run busybox ping -c 1 docker.for.mac.localhost | awk 'FNR==2 {print $4}' | sed s'/.$//'

docker-compose.yml

version: '3.8'

services:
  spring-app-container:
    image: spring-app:1
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - "50800:50800"
Husking answered 14/6, 2022 at 8:41 Comment(0)
T
0

For Windows Machine :-

Run the below command to Expose docker port randomly during build time

$docker run -d --name MyWebServer -P mediawiki

enter image description here

enter image description here

In the above container list you can see the port assigned as 32768. Try accessing

localhost:32768 

You can see the mediawiki page

Thumping answered 17/9, 2018 at 15:18 Comment(1)
Though this answer may provide useful information to some users, it is the wrong way around! It is about accessing a service running in the container from the host machine. The question, however, was about accessing a service running on the host from the container.Raynard
D
0

The way I do it is pass the host IP as environment variable to container. Container then access the host by that variable.

Drifter answered 6/2, 2020 at 4:0 Comment(2)
Can you illustrate how you do this? I had attempted this approach but didn't find much successFun
` docker run -i -t -e HOST=10.145.2.123 ubuntu root@ce2a843da3ee:/tmp# ./telnet $HOST 22 Trying 10.145.2.123... Connected to 10.145.2.123. Escape character is '^]'. SSH-2.0-OpenSSH_7.4`Drifter
T
0

On Windows machines, it is common that ports are unreachable because of internal cache problems with network addresses translator. Restarting it might help:

net stop winnat
net start winnat
Tribadism answered 22/2, 2023 at 14:51 Comment(0)
H
0

If it's useful to anyone, when using NEXTJS on mac I had to use http://docker.for.mac.localhost:8000 on the CLIENT and plain old http://localhost:8000 on the server.

Hampton answered 25/3, 2023 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.