Easy way to connect Docker to localhost

To connect Docker to localhost, running Docker for Windows or Docker for Mac, you can use the special DNS name host.docker.internal which resolves to the internal IP address used by the host.

Within your container, all you need to do to access the localhost is point your request to http://host.docker.internal:<port>

This will not work in a production environment outside of Docker Desktop. So only use this in your development environment. For Linux users, just use docker run with –network=”host” and use 127.0.0.1 to point to your localhost.

A practical use case to connect docker to localhost

A typical example of using a connection to localhost is connecting to a database you have installed locally. I’ll use a PostgreSQL installation local on my Mac to demonstrate. For the PostgreSQL installation, I have installed the default settings and haven’t edited any configuration files to perform the following tasks.

I have a table Users in database bernieslearnings and I’ll run a container that can retrieve the data in the table. We’ll start by running an Ubuntu docker image in interactive mode and installing the postgres command line tools.

docker run -it ubuntu:latest
apt-get update &amp;&amp; apt-get install -y postgresql-client

root@53ddd71e92c3:/# psql --user postgres --host host.docker.internal
Password for user postgres: 
psql (12.4 (Ubuntu 12.4-0ubuntu0.20.04.1))
Type "help" for help.

postgres=# \c bernieslearnings
You are now connected to database "bernieslearnings" as user "postgres".
bernieslearnings=# select * from Users;
 pk |  name  | age 
----+--------+-----
  1 | Bernie |  38
(1 row)

What about non-Windows/MacOS Docker users?

The above approach is the current way to connect docker to localhost for Docker Desktop For Windows and Docker Desktop for Mac users. But if you’re using a Linux system the approach is a little different.

In order to reach the hosts localhost you need to use –network=”host” in your docker run command. Then, when sending requests to 127.0.0.1 your request will be sent the host machines localhost.

A Linux Example

In this example, I will use a CentOS 7 installation to demonstrate how to connect docker to localhost. You should start by removing any older version of docker that may be installed so that you can use the latest for the following example. Enter the following to remove old docker installations.

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

In order to install docker on CentOS you need to have the yum-utils package installed.

sudo yum install -y yum-utils

Next we’ll add the stable repository to yum

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

To install the latest version of Docker Engine and containerd

sudo yum install docker-ce docker-ce-cli containerd.io

Next, set your user to be part of the newly created docker group.

usermod -a -G docker username

Now we need to start the docker engine

sudo systemctl start docker

Now that we have docker setup, we’ll need to run something to check our connection to localhost is working. This server has a MySQL instance running so we’ll use that as our test case.

Connect docker to localhost MySQL instance

Let’s run an Ubuntu docker container in interactive mode and connect that to the host network

docker run --network="host" -it ubuntu:latest

Next, we need to update apt-get and then install mysql-client so we can query the database running on localhost outside of the container

apt-get update && apt-get install mysql-client

Now that the command-line client for MySQL is installed, all we need to do is to connect to our localhost MySQL instance

mysql -host 127.0.0.1 -u <username> -p

Enter your password and show the databases available

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mail               |
| mysql              |
| performance_schema | 
+--------------------+
4 rows in set (0.00 sec)

A history trying to connect docker to localhost

For previous iterations of Docker Desktop (for Windows and Mac) there was a different special DNS entry for connecting the hosts localhost.

The previous entry for MacOS was docker.for.mac.localhost and the entry for Windows was docker.for.win.localhost


For more information on any of the above topics, check out the following links:

For a quick overview of Docker itself, check out Docker – A concise, quick overview

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh