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
I was finally able to launch docker and connect to the localhost. What did I do:
I've installed Ubuntu as a second system
Rewrote the docker-compos and docker file as attached below.
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 Dockerfilehere is my docker-compose.yaml file
Thank you so much for your help, @Tschösi
I’m editing this answer to summarize what we found out so far (see comments below).
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).
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):docker-compose.yml
:docker-compose up --build backend
(ordocker compose up --build backend
, depending on your compose version)docker exec server_container curl http://localhost:8099/api/v1/midas/category
curl http://localhost:8181/api/v1/midas/category
(we mapped 8099 to 8181).http://localhost:8181/api/v1/midas/category
in a browser.Please let me know in the comments at which step your problem occurs.