Skip to content
Advertisement

Access localhost from docker container

I have an assignment to set up 3 docker container on localhost:8081, localhost:8082 and localhost:8083 which i’ve done succesfully.Then there is a last container that is a java app on localhost:8080 and it needs to send requests using HttpClient and HttpRequest to the other containers i’ve done this creating a bridge with “docker network create web_server –driver brigde” and im running the containers with –network web_server and this way they can communicate using the container names and it works. But my teacher told me to send the request to http://localhost:8081, 8082 etc. Is there a way to make containers access localhost? Im using docker for linux

Advertisement

Answer

On Linux containers, your can access the host using IP address 172.17.0.1. So from inside your Java app you should be able to reach the other containers on 172.17.0.1:8081, 172.17.0.1:8082 and 172.17.0.1:8083. That’s equivalent to using localhost:8081, localhost:8082 and localhost:8083 on your host machine.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement