I'm trying to dockerize my spring boot application connected with PostgresSQL DB. When I try to bring up the application using docker compose, app container is failing to connect to PostgresSQL running container. I searched about it and tried most of the possible fix (for DB dependency used in docker compose file) but still not able to make it connect.
I could see from logs following -
Caused by: org.postgresql.util.PSQLException: Connection to postgresqldb:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. springboot-postgresql | Caused by: java.net.ConnectException: Connection refused springboot-postgresql | at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
I've following in my local application.properties file -
spring.datasource.url= jdbc:postgresql://localhost:5432/ecommerce
spring.datasource.username= postgres
spring.datasource.password= root
spring.datasource.driver-class-name= org.postgresql.Driver
spring.sql.init.mode=always
#spring.batch.initialize-schema=always
logging.level.org.hibernate.SQL= DEBUG
spring.jpa.defer-datasource-initialization=true
spring.jpa.show-sql= true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto= create
spring.jpa.hibernate.database= postgresql
enter code here
docker-compose -
version: '3.1'
services:
app:
container_name: springboot-postgresql
command: >
sh -c "/wait && java -jar /spring-boot-ecommerce.jar"
image: springboot-postgresql
extra_hosts:
- 'postgresqldb:127.0.0.1'
environment:
- RP_DB_HOST=postgres
- dbhost=localhost
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgresqldb:5432/ecommerce?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&useSSL=false
build: ./
ports:
- '9090:8080'
depends_on:
- postgresqldb
postgresqldb:
container_name: postgresqldb
image: 'postgres:13.1-alpine'
ports:
- '5432:5432'
environment:
- POSTGRES_PASSWORD=root
- POSTGRES_USER=postgres
- POSTGRES_DB=ecommerce
volumes:
- './sql/data.sql:/docker-entrypoint-initdb.d/data.sql'
expose:
- 5432
restart: always
DB container is up with same name and port as shown below but app container isn't able to connect. One possibility is app container is starting early than DB, I added wait command for the same, but it's not creating difference.
[Update] - Now app container is up and running and I could see that it has updated DB as well as expected. I'm not able to access API's using http://localhost:9090/api/. Also tried replacing localhost with container inet addr. Any pointers?
CodePudding user response:
TL;DR
Get rid of this:
extra_hosts:
- 'postgresqldb:127.0.0.1'
I see that you tried to apply all possible fixes to your config. Let's elaborate step by step.
- The
EXPOSE
instruction does not actually publish the port. It's just a hint for the devs. So, you can remove it from your compose and keep config cleaner. Read more container_name: postgresqldb
is not used to resolve DNS names within Docker network. In your case, domain names such aspostgresqldb
provided by service ids in your compose file: Read more
services:
app: # so that, you get the "app" domain name
postgresqldb: # so that, you get the "postgresqldb" domain name
ports: '5432:5432'
is not required to reach a database from your java application. This is needed to route your host tcp ingress to container's using the iptables record. In your case, this directive lets you to use your localhost:5432 directly on your host machine outside docker. E.g., you can use this address in some GUI which is supposed to manage the database. Read moreextra_hosts
directive adds additional DNS records to a container. In your case, this is the breaking point because it overwritespostgresqldb
domain with wrong address127.0.0.1
. As I mentioned in the beginning, just get rid of this directive. If you are curious which IPpostgresqldb
receives, you are welcome to inspect docker networks. Read more