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

Better Stack Team
Updated on October 5, 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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github