I have service that is running at port 127.0.0.1:5433 in linux host machine, I want to access this service from the container using host.docker.internal
but i could not.
version: "3"
services:
streamlit:
image: xxx/xxx
build:
context: .
dockerfile: Dockerfile
target: final
ports:
- "8501:8501"
env_file:
- .env
entrypoint: scripts/app_entrypoint.sh
volumes:
- .:/app
extra_hosts:
- "host.docker.internal:host-gateway"
here is the output of ss -ltn
which shows that 127.0.0.1:5433
is LISTENING, but when i try to access this port from inside the container, It says that port is closed. I doubt that this might be issues related to 127.0.0.1
and 0.0.0.0
so i started python http server on port 5433
and bind 0.0.0.0
then i was able to access it from inside the container, but when i start python http server and bind 127.0.0.1
instead i could not access from inside the container.
But i want to access service running on 127.0.0.1
from the container. My collegue on MAC is able to do so but I am using Linux machine and having this issue.
as you can see i have also used extra_hosts
in my docker compose file as per documentation.
extra_hosts:
- "host.docker.internal:host-gateway"
Isn’t this sufficient to access service running on 127.0.0.1
? and not only 0.0.0.0
2
Answers
Setting
extra_hosts
ashost.docker.internal:host-gateway
does not enable access to the host computer’s127.0.0.1
(localhost) from within the Docker container.When you set
extra_hosts
ashost.docker.internal:host-gateway
, it enables the container to access the host’s network gateway. This allows the container to communicate with other devices on the local network or the internet. However, it does not grant access to the loopback address127.0.0.1
on the host.The loopback address
127.0.0.1
is specific to each network namespace, and the container has its own separate network namespace from the host. Consequently, the container’s loopback address is distinct from the host’s loopback address. Therefore, settingextra_hosts
does not provide direct access to the host’s127.0.0.1
.If you need to access services running on the host’s
127.0.0.1
, you would typically use port mapping when running the container. By mapping the host’s port to a specific port on the container, you can access the services running on the host. For example:With this configuration, you can access the service running on
127.0.0.1:8080
on the host by usinglocalhost:8080
within the container.To further understand more about loopback address, check out the following:
I am not pretty sure, But I guess you’ve misunderstood
0.0.0.0
and127.0.0.1
so here is a small clarification.127.0.0.1
is a special IP address that is used to refer to the loopback network interface of the local machine, it will only accept connections from processes running on the same machine.while on the other hand,
0.0.0.0
is used to refer to all available network interfaces on the local machine. When you run a server process on your local machine and bind it to 0.0.0.0, it will accept connections from any IP address.and here is what you have done wrong, if you type in your terminal
docker network inspect bridge
you can check what is the IP that docker network is using. And I Think and am not sure that its172.17.0.0/16
by default.You can check you container IP with this command, and you can access it throw the external port
docker inspect --format '{{ .NetworkSettings.Networks.bridge.IPAddress }}' my-container
if you want to communicate with the container from another container , you can use
{container_name}:{internal_port}
as from your example this will bestreamlit:8501