If you’ve ever run into issues accessing your local development server from inside a Docker container, you’re not alone.
Many developers face challenges when their application running in a container needs to interact with services running on their host machine.
Luckily, Docker provides a simple way to do this: host.docker.internal
🚀 What is host.docker.internal
?
host.docker.internal
is a Docker Desktop feature.host.docker.internal
is a special hostname that allows a Docker container to reach the host machine’s network.
This is useful when:
A backend service running locally needs to be accessed from a containerized front-end application.
A microservices architecture requires services to communicate between local and containerized environments.
You’re testing APIs or running a development database outside the container.
Using host.docker.internal
Accessing Localhost from a Docker Container
If your local service runs on port 3000, you can use this inside your container:
http://host.docker.internal:3000
For example, if you’re running a Next.js app locally and a Dockerized backend, your front-end could communicate with the backend like this:
const API_URL = "http://host.docker.internal:3000/api";
Alternatively, if using Docker Compose, you can add this to your docker-compose.yml
:
services:
my-service:
build: .
ports:
- "3000:3000"
extra_hosts:
- "host.docker.internal:host-gateway"
This ensures the container recognizes host.docker.internal
just like on Mac and Windows.
Final Thoughts
Docker’s host.docker.internal
is a powerful tool for connecting local services to containerized applications. While it's natively supported on Mac and Windows, Linux users need to add some extra configuration.
If you’re working with microservices, front-end-backend communication, or local database connections, mastering this technique can save you a lot of headaches!
© 2025 Kristiyan Velkov. All rights reserved.