Docker Community Forums

Share and learn in the Docker community.

How to access a website running in a container when you´re using network_mode: host

I have a very tricky topic because I need to access a private DB in AWS. In order to connect to this DB, first I need to create a bridge like this:

ssh -L 127.0.0.1:LOCAL_PORT:DB_URL:PORT -N -J ACCOUNT@EMAIL.DOMAIN -i ~/KEY_LOCATION/KEY_NAME.pem PC_USER@PC_ADDRESS

Via 127.0.0.1:LOCAL_PORT:DB_URL I can connect to the DB in my Java app. Let´s say the port is 9991 for this case.

My docker files more or less look this:

docker-compose.yml

version: '3.4'
services:
  api:
    image: fanmixco/example:v0.01
    build:
      context: .
    network_mode: host
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://host.docker.internal:9991/MY_DB

Dockerfile

FROM openjdk:11
RUN mkdir /home/app/
WORKDIR /home/app/
RUN mkdir logs
COPY ./target/MY_JAVA_APP.jar .

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "MY_JAVA_APP.jar"]

The image runs properly. However, if I try:

  • using localhost:8080/MY_APP fails
  • using 127.0.0.1/MY_APP fails
  • getting the container’s IP and use it later fails
  • using host.docker.internal/MY_APP fails

I´m wondering how I can test my app. I know it´s running because I get a successful message in the console and the new data was added to the DB, but I don´t know how I can test it or access it. Any idea of the proper way to do it? Thanks.

P.S.:

  1. I´m running my Images in Docker Desktop for Windows.

  2. I have another case using tomcat 9 and running CMD ["catalina.sh", "run"] and I know it’s working because I get this message in the console:

INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [9905] milliseconds

But I cannot access it again.

Use --network=“host” in your docker run command, then 127.0. 0.1 in your docker container will point to your docker host. Note: This mode only works on Docker for Linux, per the documentation.

Hi Lewish, it works in Windows because I can add data to the AWS DB but I cannot access the website in any way.