Using Docker network and the extra_hosts property
Resolve host aliases inside Docker containers! Learn to use docker network and the extra_hosts property in docker compose to seamlessly connect your services.
This article is over a year old but was reviewed on July 30, 2026 — the content is still accurate.

This article explains why one Docker container can't reach another unless both run on the same Docker network, walking through creating a shared network, finding its gateway IP with docker network inspect, and connecting a second container to it via compose.yaml. It also shows how to reuse a host-side hosts-file alias inside a container with the extra_hosts property.
When you're running a Docker container on a different network than the standard one (called bridge) and you wish to run a second container that needs to access the first container, you need to run the second container on the same network.
Let's say, you're running a MySQL database on a network called my_network and you wish to be able start a second container like phpMyAdmin (see Using Adminer, pgadmin or phpmyadmin to access your Docker database container) and get access to the database, then you need to use the --network CLI option when running the second container using docker run.
Now, imagine the first container is a web application and the second container should be able to access its web page and, too, reuse the same alias?
The two-line fix
Here is the whole answer, at the top of the article rather than at the end. Add extra_hosts to the service that needs to reach your host alias:
services: my_second_container: build: context: . networks: - my_network //highlight-next-line extra_hosts: //highlight-next-line - "my_site.local:172.20.0.1"
networks: my_network: external: trueThis Docker Compose configuration defines a service called my_second_container that builds from the current directory, connects to an existing external network called my_network, and adds a custom hostname-to-IP mapping so the container can reach my_site.local at address 172.20.0.1.
Jump into the container and look at what Docker did with it:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #fff; color: #222; font-family: sans-serif;}
pre {margin: 0; font-family: monospace;}
[...]
The alias my_site.local is now a real line in the container's /etc/hosts, and curl http://my_site.local:8080 returns the page served by the other container.
Why it works
- Both containers must share a network. A container on the default
bridgenetwork simply cannot reach a container running on another one — that's a protection, not a bug. 172.20.0.1is the gateway of that network, i.e. the address from which containers see your host machine. It's the value we'll retrieve withdocker network inspectfurther down.- Your host's hosts file is invisible from inside a container.
extra_hostsis the way to re-declare the alias where the container can see it: Docker writes the line into/etc/hostsat startup.
The rest of this article builds the whole scenario from scratch, including the two failures you'd hit on the way.
Some preparation work
Skip this step if you already have a dedicated network and its running container
If you don't already have a running web application on its own network, please follow this step.
Please start a Linux shell and run mkdir -p /tmp/network && cd $_ to create a folder called network in your Linux temporary folder and jump in it.
Please then create a index.php file in that folder with this content:
<?php
phpinfo();This is a simple PHP script that outputs detailed information about the PHP installation and its configuration. It's commonly used to diagnose PHP setup issues or verify that PHP is installed and working correctly.
Here is the content of your current directory:
Since we need a Docker network, please create one:
And run the script using docker run -d -p 8080:80 -u $(id -u):$(id -g) -v "$PWD":/var/www/html --network my_network php:8.2-apache.
That command will run an Apache container and we can surf to our local website using http://127.0.0.1:8080

Creating our second container
Now, we can create a second container and just try to curl our website.
Please create a file called Dockerfile with the content below. We'll use a very small Linux image and we'll install curl in the image.
FROM alpine:3.14
RUN apk add --no-cache curlThis Dockerfile creates a minimal Docker image based on Alpine Linux 3.14 and installs the curl command-line tool for making HTTP requests. Alpine is chosen because it's very small, and the --no-cache flag keeps the image size down by not storing package manager metadata.
And a second file called compose.yaml with this content:
services: my_second_container: build: context: .This is a Docker Compose configuration that defines a service called 'my_second_container' and tells Docker to build it using a Dockerfile located in the current directory (the dot means 'here').
To make things clear, here is the content of our current directory:
We need to create our image. To do this, simply run docker compose build.
Then we'll start an interactive bash shell in our second container and we'll try to access our local website:
When it doesn't work (and why)
It's not working... **as expected**
We can confirm our container is not able to access our local site http://127.0.0.1:8080 while that website is well configured. If you exit the container and try to refresh the website, it's working well.
We need to run the second container on the same network
Retrieve the network used by a container
In case you don't know the name of the used network, simply run docker inspect xxxx where xxxx is the name of the container. You'll get a JSON answer with a Networks entry. To get more information, please read the Docker inspect - Retrieve network's information article.
Please edit your compose.yaml file like this:
services: my_second_container: build: context: . //highlight-next-line networks: //highlight-next-line - my_network
//highlight-next-linenetworks: //highlight-next-line my_network: //highlight-next-line external: trueThis Docker Compose file defines a service called 'my_second_container' that builds from the current directory and connects to an external network named 'my_network'. The network section at the bottom declares that 'my_network' is external, meaning it already exists and wasn't created by this Compose file.
Replace my_network by yours if you've a different one.
We need to find the IP of the network
But, there is something else to do now: we need to obtain the Gateway IP address of the network.
Back on your machine (not from inside the container), please run:
The IP we need is 172.20.0.1 (Gateway) as illustrated above.
Try again
Now, we can try again, please start an interface shell once more. It'll still not work with the local 127.0.0.1 IP but well, now, using the Gateway IP address of the network:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #fff; color: #222; font-family: sans-serif;}
pre {margin: 0; font-family: monospace;}
Extra use case - aliases
And now the final part, imagine you've defined an alias in the hosts file (for Windows, in file C:\Windows\System32\drivers\etc\hosts).
Imagine you've created an alias like:
127.0.0.1 localhost// highlight-next-line127.0.0.1 mysite.localThis is a hosts file configuration that maps IP addresses to domain names on the local machine. Line 1 sets up the standard localhost entry, and line 3 adds a custom domain 'mysite.local' that will also resolve to your own computer instead of going out to the internet.
and thus, on your host, you're not using http://127.0.0.1:8080 but http://mysite.local:8080
If we try to access it from inside the second container, it doesn't work:
And this is normal since my_site.local is an alias defined on your host machine; not in the container:
The last thing we need to do in this case is to edit our compose.yaml file and add the extra_hosts property — the two lines shown at the very beginning of this article, now with the gateway IP we retrieved above. Jump in the container one last time, cat /etc/hosts, and the alias is there: curl http://my_site.local:8080 works.
Conclusion
The alias you've been typing for months in your browser is a host-machine thing. Nothing carries it into a container, and nothing tells you that's the problem — you just get Could not resolve host and start doubting your network. Two lines of extra_hosts pointing at the network gateway close the gap, and your compose.yaml now documents that dependency for whoever clones the project next.
When everything looks correctly configured and the connection still fails, Troubleshooting for Docker containers - Accessing the other one walks through the diagnosis layer by layer.





