From Inside of a Docker Container, How Do I Connect to the Localhost of the Machine?

Better Stack Team
Updated on October 7, 2024

When you're working inside a Docker container and need to connect to services running on the host machine (i.e., the Docker host), you need to handle network routing carefully because Docker containers are isolated from the host system. Here are several methods to achieve this, depending on your operating system and Docker configuration:

1. Use Docker’s Special DNS Name

Docker provides a special DNS name host.docker.internal for this purpose. This works on Docker Desktop for Windows and macOS, and with certain configurations on Linux.

Using host.docker.internal

 
# Inside the Docker container
curl <http://host.docker.internal:8080>

Replace 8080 with the port number on which your service is running on the host machine.

2. Using docker0 Network Interface (Linux Only)

On Linux, Docker creates a default bridge network interface called docker0. You can use the IP address of this interface to connect from a container to the host.

Find the Docker0 IP Address

  1. Get the IP address of docker0:

     
    ip addr show docker0
    

    Look for the inet entry, which might look like 172.17.0.1.

  2. Use this IP inside your Docker container:

     
    # Inside the Docker container
    curl <http://172.17.0.1:8080>
    

    Again, replace 8080 with the port number your service is using.

3. Use Host Network Mode (Linux Only)

If you need to connect to localhost on the host from within a container, and are running Docker on Linux, you can use the host network mode.

Run Container with Host Network

 
docker run --network host my_image

In this mode, the container shares the network stack with the host, meaning localhost inside the container will refer to the host’s localhost.

4. Use localhost or 127.0.0.1 Directly (Linux/Mac)

If you’re running Docker on Linux or macOS and you have exposed the necessary ports, you might be able to use localhost or 127.0.0.1, but this is not always reliable due to network isolation.

 
# Inside the Docker container
curl <http://localhost:8080>

5. Expose Ports Properly

Ensure that the service you’re trying to connect to on the host machine is properly exposed and accessible from outside. Check your firewall and service settings.

6. Custom Network Bridge

You can also create a custom network bridge and connect both the container and host to it, but this approach is more complex and less common for simple host-to-container communication.

Summary

  • For Docker Desktop (Windows/macOS): Use host.docker.internal.
  • For Linux: Use docker0 IP or -network host if suitable.
  • Ensure Ports are Exposed: Make sure the service on the host is exposed to the network and not restricted by firewalls.

These methods will help you connect to services running on the host from within a Docker container effectively.

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

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

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