How to get the IP address of the docker host from inside a docker container?

Better Stack Team
Updated on April 14, 2023

To get the IP address of the Docker host from inside a Docker container, you can use the docker.for.mac.localhost hostname. This hostname resolves to the IP address of the Docker host when used from inside a Docker container on a Mac.

Here's an example of how you can get the IP address of the Docker host from inside a Docker container:

  1. Run the following command to start a Docker container:

     
    docker run -it alpine /bin/sh
    

    This will start a new Docker container based on the Alpine Linux image and open an interactive shell inside the container.

  2. Once inside the container, run the following command to get the IP address of the Docker host:

     
    ping docker.for.mac.localhost
    

    This will send a ping request to the docker.for.mac.localhost hostname, which will resolve to the IP address of the Docker host.

    Alternatively, you can run the following command to get the IP address directly:

     
    ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}'
    

    This command uses ifconfig to get a list of network interfaces and their IP addresses, then uses grep and awk to filter out the loopback address (127.0.0.1) and print the IP address of the first non-loopback interface. This should be the IP address of the Docker host.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack