I have an Nginx running inside a docker container. I have a MySql running on the host system. I want to connect to MySql from within my container. MySql only binds 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 the docker container" because the IP address of the docker host may be a public IP or a private IP in the network, which may or may not be accessible from within the docker container (I means public IP if hosted on AWS or elsewhere). Even if you have the IP address of the docker host, it does not mean that you can connect to the docker host from within the container, because your Docker network may be an overlay network, a host network, a bridge network, a macvlan network, a none network, etc., which limits The container's reachability to this IP address.
Available on all platforms
Docker v 20.10 and later (as of December 14, 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 purposes and is not intended for use in production environments outside of Docker Desktop.
Linux Warning
To enable this feature inDockeron Linux, add
--add-host=host.docker.internal:host-gateway
to yourThe docker
command enables this feature.To enable this feature inDocker Composeon Linux, add the following line to the container definition:
According to some users, the special DNS names only work in Docker's default
bridge
network and not in custom networks.For older macOS and Windows versions of Docker
Docker v 18.03 and later (as of March 21, 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 is pendinghttps://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 using
docker.for.mac.host.internal
.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 lower
To access the host from a docker container, you must attach an IP alias to your network interface. You can bind any IP you want, just make sure you're not using it for anything else.
sudo ifconfig lo0 alias 123.123.123.123/24
Then make sure your server is listening on the above IP or
0.0.0.0
. If it is listening on localhost127.0.0.1
, it will not accept connections.Then just point your docker container to this IP to access the host!
To test, you can run something like
curl -X GET 123.123.123.123:3000
inside the container.Aliases will be reset on every reboot, so create a startup script if necessary.
Solution and more documentation here:https://docs.docker.com/desktop/networking/#use-cases-and-workarounds-for-all-platforms
edit:
If you are usingDocker-for-macorDocker-for-Windows18.03, use host
host.docker.internal
(instead of127.0.0.1 in your connection string).
If you are usingDocker-for-Linux20.10.0, you can also use host
host.docker.internal
ifYou use--add-host host.docker.internal:host-gateway
option starts the Docker container, or the following code snippet is added to the docker-compose.yml file:Otherwise, please read below
TLDR
Use
--network="host"
in thedocker run
command, then127.0.0.1
in the docker container will point to your docker host .Note: This mode is only available for Linux versions of Docker,according to the documentation. p>
Notes on docker container network mode
Docker providesdifferent network modeswhen running containers. Depending on the mode you choose, you will connect to the 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 container have an IP address on the bridge.On the Docker host, enter
sudo ip addr show docker0
You will get output like this:So, my docker host's IP address on the
docker0
network interface is172.17.42.1
.Now start a new container and get a shell on it:
docker run --rm -it ubuntu:trusty bash
and type in the containerip addr show eth0
to know its master How to set up the network interface:The IP address of my container here is
172.17.1.192
. Now view the routing table:Therefore, the docker host's IP address
172.17.42.1
is set as the default route and is accessible from your container.docker run --network="host"
Alternatively, you can run a Docker container and set thenetwork settings to
host
. Such a container will share the network stack with the docker host, and from the container's perspective,localhost
(or127.0.0.1
) will refer to the docker host. p>Please note that any port opened in the docker container will be opened on the docker host. This does not require
-p
or -Pdocker run
option.IP configuration on my docker host:
And docker containers fromhostmode:
As you can see, the docker host and the docker container share the exact same network interface and therefore have the same IP address.
Connect to MySQL from container
Bridge Mode
To access MySQL running on a docker host from a container inbridge mode, you need to ensure that the MySQL service is listening for connections on the
172.17.42.1
IP address.To do this, make sure you have
bind-address = 172.17.42.1
orbind-address = 0.0.0.0
) in your MySQL configuration file (my.cnf).If you need to set environment variables using the IP address of the gateway, you can run the following code in the container:
Then in your application, use the
DOCKER_HOST_IP
environment variable to open a connection to MySQL.NOTE:If you use
bind-address = 0.0.0.0
, your MySQL server will listen for connections on all network interfaces. This means your MySQL server is accessible from the Internet; make sure to set firewall rules accordingly.Note 2:If you use
bind-address = 172.17.42.1
, your MySQL server will not listen to the one established with127.0.0.1
Connect>. Processes running on the docker host that want to connect to MySQL must use the172.17.42.1
IP address.Host Mode
To access MySQL running on a docker host from a container in host mode, you can leave
bind-address = 127.0.0.1
in the MySQL configuration and connect to127.0.0.1
From your container:Note:Please use
mysql -h 127.0.0.1
instead ofmysql -h localhost
; otherwise the MySQL client will try to use the unix socket Make a connection.