skip to Main Content

Perhaps the question sounds vague and unclear. Now I will try to clarify the situation. I have a backend on Spring Boot and Postgresql. I am trying to deploy docker for development with a frontend developer. The containers are running, but I can’t access the page either through the browser or through the postman.
Sorry for stupid question, that’s my first time setting up Docker.

I’ve read "try to 0.0.0.0 or 172.18.0.1", but it doesn’t work either.
And also, when i’m trying to specify ports (8181:8181 for example) Tomcat starts 8099 port.

CONTAINER ID   IMAGE            COMMAND                  CREATED         STATUS         PORTS      NAMES
0cacf9541580   server-backend   "java -jar /server.j…"   6 minutes ago   Up 8 seconds   8089/tcp   server_container
7aa3902d97e6   postgres         "docker-entrypoint.s…"   6 minutes ago   Up 8 seconds   5432/tcp   postgresql_container

Thats docker ps for this configuration

services:
  db:
#    ports:
#      - 5432:5432
    container_name: postgresql_container
    image: postgres
    env_file:
      - .env
    environment:
      - POSTGRES_DB=midas_db
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_USER=${DB_USERNAME}
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
  backend:
    build:
      context: .
    container_name: server_container
#    ports:
#      - 8181:8181
#    extra_hosts:
#      - "host.docker.internal:172.18.0.1"
    env_file:
      - .env
    environment:
      - spring.datasource.url=${DB_URL_DOCKER}
      - spring.datasource.username=${DB_USERNAME}
      - spring.datasource.password=${DB_PASSWORD}
    depends_on:
      - db
    restart: always
 ONTAINER ID   IMAGE            COMMAND                  CREATED          STATUS          PORTS                              NAMES
    58b63f2cc3b3   server-backend   "java -jar /server.j…"   11 seconds ago   Up 9 seconds    8089/tcp, 0.0.0.0:8181->8181/tcp   server_container
    ed5efdfc4cf4   postgres         "docker-entrypoint.s…"   11 seconds ago   Up 10 seconds   0.0.0.0:5432->5432/tcp             postgresql_container

Thats for this configuration

services:
  db:
    ports:
      - 5432:5432
    container_name: postgresql_container
    image: postgres
    env_file:
      - .env
    environment:
      - POSTGRES_DB=midas_db
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_USER=${DB_USERNAME}
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
  backend:
    build:
      context: .
    container_name: server_container
    ports:
      - 8181:8181
#    extra_hosts:
#      - "host.docker.internal:172.18.0.1"
    env_file:
      - .env
    environment:
      - spring.datasource.url=${DB_URL_DOCKER}
      - spring.datasource.username=${DB_USERNAME}
      - spring.datasource.password=${DB_PASSWORD}
    depends_on:
      - db
    restart: always

And here is my Dockerfile

FROM maven:3.9.5-openjdk-11-slim
LABEL maintainer="[email protected]"
VOLUME /tmp
EXPOSE 8181
ARG JAR_FILE=target/server-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} server.jar
ENTRYPOINT ["java", "-jar", "/server.jar"]

2

Answers


  1. Chosen as BEST ANSWER

    I was finally able to launch docker and connect to the localhost. What did I do:

    1. I've installed Ubuntu as a second system

    2. Rewrote the docker-compos and docker file as attached below.

    3. Changed one of the environment variables.

    i had: DB_URL_DOCKER=jdbc:postgresql://postgres:postgres@pghost:5433/midas_db

    now i have: DB_URL_DOCKER=jdbc:postgresql://db:5432/midas_db Here is my Dockerfile

    FROM openjdk:17-jdk-slim
    LABEL maintainer="[email protected]"
    EXPOSE 8181
    ARG JAR_FILE=target/server-0.0.1-SNAPSHOT.jar
    ADD ${JAR_FILE} server.jar
    ENTRYPOINT ["java", "-jar", "/server.jar"]
    

    here is my docker-compose.yaml file

    services:
      db:
        ports:
          - 5433:5432
        container_name: postgresql_container
        image: postgres
        env_file:
          - .env
        environment:
          - POSTGRES_DB=midas_db
          - POSTGRES_PASSWORD=${DB_PASSWORD}
          - POSTGRES_USER=${DB_USERNAME}
        volumes:
          - ./postgres-data:/var/lib/postgresql/data
        restart: always
      backend:
        build: .
        container_name: server_container
        ports:
          - 8181:8181
        env_file:
          - .env
        environment:
          - spring.datasource.url=${DB_URL_DOCKER}
          - spring.datasource.username=${DB_USERNAME}
          - spring.datasource.password=${DB_PASSWORD}
        depends_on:
          - db
        restart: always
    
    

    Thank you so much for your help, @Tschösi


  2. I’m editing this answer to summarize what we found out so far (see comments below).

    • The tomcat server runs on port 8099 (bash-4.4# curl "http://127.0.0.1:8099" within the container reaches the server)

    Now please perform the following troubleshooting steps (we will move inwards out).

    1. Adapt your Dockerfile to expose the correct port (shouldn’t be strictly necessary as we publish the port through compose, but might have an impact on the firewall):
    FROM maven:3.9.5-openjdk-11-slim
    LABEL maintainer="[email protected]"
    VOLUME /tmp
    EXPOSE 8099
    ARG JAR_FILE=target/server-0.0.1-SNAPSHOT.jar
    ADD ${JAR_FILE} server.jar
    ENTRYPOINT ["java", "-jar", "/server.jar"]
    
    1. Adapt your docker-compose.yml:
    services:
      db:
        ports:
          - 5432:5432
        container_name: postgresql_container
        image: postgres
        env_file:
          - .env
        environment:
          - POSTGRES_DB=midas_db
          - POSTGRES_PASSWORD=${DB_PASSWORD}
          - POSTGRES_USER=${DB_USERNAME}
        volumes:
          - ./postgres-data:/var/lib/postgresql/data
      backend:
        build:
          context: .
        container_name: server_container
        ports:
          - "8181:8099"
        env_file:
          - .env
        environment:
          - spring.datasource.url=${DB_URL_DOCKER}
          - spring.datasource.username=${DB_USERNAME}
          - spring.datasource.password=${DB_PASSWORD}
        depends_on:
          - db
        restart: always
    
    1. restart your application docker-compose up --build backend (or docker compose up --build backend, depending on your compose version)
    2. Check whether you can reach the server in the docker container docker exec server_container curl http://localhost:8099/api/v1/midas/category
    3. Check whether you can reach the server from within WSL. In a WSL console execute curl http://localhost:8181/api/v1/midas/category (we mapped 8099 to 8181).
    4. Check whether you can reach the server from your host system (Windows). Open http://localhost:8181/api/v1/midas/category in a browser.

    Please let me know in the comments at which step your problem occurs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search