1

I'm attempting to run a Spring Boot app that connects a Postgres DB using:

docker-compose.yml (for Postgres) :

  version: '3'
  services:
  postgres-db:
  container_name: postgres-db
  image: postgres:latest
  restart: always
  ports:
  - "5432:5432"
  environment:
  POSTGRES_USER: my_user
  POSTGRES_PASSWORD: my_password
  POSTGRES_DB: shorten-db

To run the Postgres DB:

docker-compose up

.Dockerfile (for the Spring Boot app) :

  FROM openjdk:12-jdk-alpine
  RUN addgroup -S spring && adduser -S spring -G spring
  USER spring:spring
  ARG JAR_FILE=target/*.jar
  COPY ${JAR_FILE} app.jar
  ENTRYPOINT ["java","-jar","/app.jar"]

In order to run the Spring app using Docker I use:

  mvn package
  
  docker build -t url-shorten/url-shorten-docker .
  
  docker run -p 8080:8080 url-shorten/url-shorten-docker

But I receive the error when starting when running above docker command:

Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

In Spring application.properties I connect to the DB using:

spring.datasource.url=jdbc:postgresql://localhost:5432/shorten-db

I think this error is due to the Spring Boot app is running in a different container to DB so it cannot find the DB on localhost. Is there an idiomatic way of connecting the Spring Book docker container to the DB container. Or do I have do access the IP address of my machine and use this address to connect to the Postgres DB running on Docker?

6
  • If you're running a container in the same network, it can be accessed by its container name, e.g. postgres-db:5432 Commented Sep 2, 2020 at 12:55
  • The answers that you got (to use the container name postgres-db directly) will work only if you run the containers at the same user-defined bridge. The way you are doing it now, the db container runs at the bridge created automatically when you docker-compose up and the other (with docker run ...) will run at the default bridge. Commented Sep 2, 2020 at 13:03
  • More here: Differences between user-defined bridges and the default bridge Commented Sep 2, 2020 at 13:04
  • 1
    I would put the Spring-boot container details inside the docker-compose.yaml file and let docker-compose deal with the networking. When you docker-compose up / down it creates/deletes automatically a custom bridge for your convenience. Check more here: Networking in Compose Commented Sep 2, 2020 at 13:09
  • 1
    The other option is what you are proposing, yes, to deal with the networks yourself a little bit more "manually". Commented Sep 2, 2020 at 13:20

3 Answers 3

1

Yes, you can't use localhost in this situation

spring.datasource.url=jdbc:postgresql://postgres-db:5432/shorten-db
Sign up to request clarification or add additional context in comments.

Comments

1

In Spring application.properties, try to change DB config to:

spring.datasource.url=jdbc:postgresql://postgres-db:5432/shorten-db

In container networks, You need to use the container name as a host.

Comments

0

You can add both DB and app containers to one Docker network and change PostgreSQL host in datasource URL to postgres-db. Then Spring app will work with your DB.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.