Docker-compose does not recognice Postgres Host of SpringBoot App - postgresql

I'am trying to use docker-compose with a springboot app(uses JPA) and a postgres database, I am kinda new using docker and I don't know what I am doing wrong.
Problem
When I build the springboot app, if I change
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres(code that works) to host it in postgres container, spring.datasource.url=jdbc:postgresql://postgres:5432/postgres.
It throws me a PSQLException caused by: java.net.UnknownHostException: postgres.(More details below)
application.properties
server.port = 8080
## default connection pool
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5
## PostgreSQL
spring.datasource.url=jdbc:postgresql://postgres:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=create
Exception
2020-06-22 19:07:10.597 INFO 16088 --- [ main] c.a.a.AnnotationToolApplicationTests : Starting AnnotationToolApplicationTests on DESKTOP-P1E07PA with PID 16088 (started by Pedro in C:\Users\Pedro\git\tfg\annotation-tool\annotation-tool)
2020-06-22 19:07:10.598 INFO 16088 --- [ main] c.a.a.AnnotationToolApplicationTests : No active profile set, falling back to default profiles: default
2020-06-22 19:07:11.584 INFO 16088 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-06-22 19:07:11.719 INFO 16088 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 120ms. Found 3 JPA repository interfaces.
2020-06-22 19:07:12.235 INFO 16088 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-06-22 19:07:12.572 INFO 16088 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-06-22 19:07:12.746 INFO 16088 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.4.10.Final}
2020-06-22 19:07:13.002 INFO 16088 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-06-22 19:07:13.434 INFO 16088 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-06-22 19:07:16.873 ERROR 16088 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292) ~[postgresql-42.2.9.jar:42.2.9]
...
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126) ~[surefire-booter-2.22.2.jar:2.22.2]
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418) ~[surefire-booter-2.22.2.jar:2.22.2]
Caused by: java.net.UnknownHostException: postgres
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_222]
docker-compose.yml
version: '3'
services:
# SpringBoot App
postgres:
image: "postgres:9.6-alpine"
ports:
- 5432:5432
volumes:
- apiDB:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
api:
build: ./annotation-tool
ports:
- 8080:8080
depends_on:
- postgres
client:
container_name: client-container
build: ./client
ports:
- 8081:8081
volumes:
apiDB:
Api Dockerfile
FROM maven:3.6.1-jdk-8-slim AS build
RUN mkdir -p /workspace
WORKDIR /workspace
COPY pom.xml /workspace
COPY src /workspace/src
RUN mvn -f pom.xml clean package
FROM openjdk:8
COPY --from=build /workspace/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
Things that I think that happened/ tried
Maybe the DNS is not working, but I don't know how I can solve it
I tried differents docker-compose and Dockerfiles and it doesn't work
If I run the docker-compose the postgres container is created succesfully
I tried changing the url host to postgres-backend as my container was named that way
I tried by using links between my components.

I think springboot application is finding postgres db before its container is getting up...you can add depends_on property in springboot container ..so the postgres container starts first then springboot
version: '3'
services:
# SpringBoot App
postgres:
image: "postgres:9.6-alpine"
ports:
- 5432:5432
volumes:
- apiDB:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
api:
build: ./annotation-tool
ports:
- 8080:8080
depends_on:
- postgres
client:
container_name: client-container
build: ./client
ports:
- 8081:8081
volumes:
apiDB:

I don't know if I am thinking to simple right know but all container of your docker-compose file run on the same host, right? So then your url should point to jdbc:postgresql://localhost:5432/postgres.

I don't know if you already solved the problem but have you tried to use a bridge network on the setup?
version: '3'
services:
# SpringBoot App
postgres:
image: "postgres:9.6-alpine"
ports:
- 5432:5432
volumes:
- apiDB:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
networks:
- database-network
api:
build: ./annotation-tool
ports:
- 8080:8080
depends_on:
- postgres
networks:
- database-network
client:
container_name: client-container
build: ./client
ports:
- 8081:8081
networks:
database-network:
driver: bridge
external: false
volumes:
apiDB:

Related

Dockerized Spring boot app connect to mongoDB docker image not working

I try to figure out why the communication between dockerized Spring boot app and mongoDB is not working. First things first:
The whole microservice is running localy perfectly.
I create an application.properties with the profile name docker:
application-docker.properties
server.port=8091
spring.application.name=customer
# ============================================================================================================
# EUREKA
# It's not possible to use localhost in the docker container. Instate use the container name.
# In this case "eureka-server". See the file docker-compose.yml to check the container name.
eureka.client.service-url.defaultZone=http://eureka-server:8761/eureka
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
# ============================================================================================================
# ZIPKIN
# It's not possible to use localhost in the docker container. Instate use the container name.
# In this case "zipkin". See the file docker-compose.yml to check the container name.
spring.zipkin.base-url=http://zipkin:9411
# ============================================================================================================
# RABBIT_MQ
# It's not possible to use localhost in the docker container. Instate use the container name.
# In this case "rabbitmq". See the file docker-compose.yml to check the container name.
spring.rabbitmq.addresses=rabbitmq:5672
rabbitmq.exchanges.internal=internal.exchange
rabbitmq.queue.notification=notification.queue
rabbitmq.routing-keys.internal-notification=internal.notification.routing-key
# ============================================================================================================
# MONGO-DB
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
#spring.data.mongodb.database=data
#spring.data.mongodb.host=spring
#spring.data.mongodb.port=27017
spring.data.mongodb.uri=mongodb://mongodb:27017/data
and then the docker-compose.yml
services:
mongodb:
container_name: mongodb
image: mongo:latest
hostname: mongo
restart: unless-stopped
ports:
- 27017:27017
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=rootuser
- MONGO_INITDB_ROOT_PASSWORD=rootpass
networks:
- spring
mongo-express:
container_name: mongo-express
image: mongo-express
restart: always
ports:
- 8095:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
- ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
- ME_CONFIG_MONGODB_SERVER=mongodb
networks:
- spring
zipkin:
image: openzipkin/zipkin
container_name: zipkin
ports:
- 9411:9411
networks:
- spring
rabbitmq:
image: rabbitmq:3.9.11-management-alpine
container_name: rabbitmq
ports:
- 5672:5672
- 15672:15672
networks:
- spring
eureka-server:
image: koulombus/eureka-server:latest
container_name: eureka-server
ports:
- 8761:8761
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
api-gateway:
image: koulombus/api-gateway:latest
container_name: api-gateway
ports:
- 8090:8090
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
customer:
image: koulombus/customer:latest
container_name: customer
ports:
- 8091:8091
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
- mongodb
fraud:
image: koulombus/fraud:latest
container_name: fraud
ports:
- 8092:8092
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
notification:
image: koulombus/notification:latest
container_name: notification
ports:
- 8093:8093
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
volumes:
postgres:
pgadmin:
data: {}
networks:
postgres:
driver: bridge
spring:
driver: bridge
after starting docker...
mvn clean package -P "build-docker-image"
docker compose up -d
Facts
Eureka-Server:
Ziplin:
RabbitMQ:
Mongo-express:
Problem
When I check now the customer log in docker i facet following:
,-----. ,--. ,--. ,---. ,--------. ,-----. ,--. ,--. ,------. ,------.
' .--./ | | | | ' .-' '--. .--' ' .-. ' | `.' | | .---' | .--. '
| | | | | | `. `-. | | | | | | | |'.'| | | `--, | '--'.'
' '--'\ ' '-' ' .-' | | | ' '-' ' | | | | | `---. | |\ \
`-----' `-----' `-----' `--' `-----' `--' `--' `------' `--' '--'
Application Name:
Application Version:
:: Spring Boot :: (v2.6.3)
2022-03-03 08:58:55.187 INFO [customer,,] 1 --- [ main] c.k.customer.CustomerApplication : Starting CustomerApplication using Java 17.0.1 on cf6b5b41507d with PID 1 (/app/classes started by root in /)
2022-03-03 08:58:55.241 INFO [customer,,] 1 --- [ main] c.k.customer.CustomerApplication : The following profiles are active: docker
2022-03-03 08:59:01.557 INFO [customer,,] 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2022-03-03 08:59:02.239 INFO [customer,,] 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 667 ms. Found 1 MongoDB repository interfaces.
2022-03-03 08:59:04.589 INFO [customer,,] 1 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=9e7fc4f4-3641-3895-b9a0-4754b711f194
2022-03-03 08:59:08.906 INFO [customer,,] 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8091 (http)
2022-03-03 08:59:08.952 INFO [customer,,] 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-03-03 08:59:08.953 INFO [customer,,] 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-03-03 08:59:09.454 INFO [customer,,] 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-03-03 08:59:09.454 INFO [customer,,] 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 13490 ms
2022-03-03 08:59:12.729 INFO [customer,,] 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[mongodb:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2022-03-03 08:59:13.071 INFO [customer,,] 1 --- [}-mongodb:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server mongodb:27017
com.mongodb.MongoSocketException: mongodb
at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:180) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:152) ~[mongodb-driver-core-4.4.1.jar:na]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: java.net.UnknownHostException: mongodb
at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:801) ~[na:na]
at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:887) ~[na:na]
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1509) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1367) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1301) ~[na:na]
at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203) ~[mongodb-driver-core-4.4.1.jar:na]
... 6 common frames omitted
Somewhere I make a mistake. I would be very grateful for a hint.
You're missing an 's' in spring.data.mongodb.uri in your config file.
OK, working solution for me:
application-docker.properties
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
spring.data.mongodb.host=mongodb
spring.data.mongodb.port=27017
spring.data.mongodb.database=data
docker-compose.yml
services:
mongodb:
container_name: mongodb
image: mongo:latest
hostname: mongo
restart: unless-stopped
ports:
- 27017:27017
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=rootuser
- MONGO_INITDB_ROOT_PASSWORD=rootpass
networks:
- spring
mongo-express:
container_name: mongo-express
image: mongo-express
restart: always
ports:
- 8095:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
- ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
- ME_CONFIG_MONGODB_SERVER=mongodb
depends_on:
- mongodb
networks:
- spring
zipkin:
image: openzipkin/zipkin
container_name: zipkin
ports:
- 9411:9411
networks:
- spring
rabbitmq:
image: rabbitmq:3.9.11-management-alpine
container_name: rabbitmq
ports:
- 5672:5672
- 15672:15672
networks:
- spring
eureka-server:
image: koulombus/eureka-server:latest
container_name: eureka-server
ports:
- 8761:8761
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
api-gateway:
image: koulombus/api-gateway:latest
container_name: api-gateway
ports:
- 8090:8090
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
customer:
image: koulombus/customer:latest
container_name: customer
ports:
- 8091:8091
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
- mongodb
- fraud
fraud:
image: koulombus/fraud:latest
container_name: fraud
ports:
- 8092:8092
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
- mongodb
notification:
image: koulombus/notification:latest
container_name: notification
ports:
- 8093:8093
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
- mongodb
volumes:
postgres:
pgadmin:
data: {}
networks:
postgres:
driver: bridge
spring:
driver: bridge

Spring Boot connection to Postgres fails when deploying with docker compose [duplicate]

I have a Java Spring Boot app which works with a Postgres database. I want to use Docker for both of them. I initially put just the Postgres in Docker, and I had a docker-compose.yml file defined like this:
version: '2'
services:
db:
container_name: sample_db
image: postgres:9.5
volumes:
- sample_db:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=sample
- POSTGRES_USER=sample
- POSTGRES_DB=sample
- PGDATA=/var/lib/postgresql/data/pgdata
ports:
- 5432:5432
volumes:
sample_db: {}
Then, when I issued the commands sudo dockerd and sudo docker-compose -f docker-compose.yml up, it was starting the database. I could connect using pgAdmin for example, by using localhost as server and port 5432. Then, in my Spring Boot app, inside the application.properties file I defined the following properties.
spring.datasource.url=jdbc:postgresql://localhost:5432/sample
spring.datasource.username=sample
spring.datasource.password=sample
spring.jpa.generate-ddl=true
At this point I could run my Spring Boot app locally through Spring Suite, and it all was working fine. Then, I wanted to also add my Spring Boot app as Docker image. I first of all created a Dockerfile in my project directory, which looks like this:
FROM java:8
EXPOSE 8080
ADD /target/manager.jar manager.jar
ENTRYPOINT ["java","-jar","manager.jar"]
Then, I entered to the directory of the project issued mvn clean followed by mvn install. Next, issued docker build -f Dockerfile -t manager . followed by docker tag 9c6b1e3f1d5e myuser/manager:latest (the id is correct). Finally, I edited my existing docker-compose.yml file to look like this:
version: '2'
services:
web:
image: myuser/manager:latest
ports:
- 8080:8080
depends_on:
- db
db:
container_name: sample_db
image: postgres:9.5
volumes:
- sample_db:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=sample
- POSTGRES_USER=sample
- POSTGRES_DB=sample
- PGDATA=/var/lib/postgresql/data/pgdata
ports:
- 5432:5432
volumes:
sample_db: {}
But, now if I issue sudo docker-compose -f docker-compose.yml up command, the database again starts correctly, but I get errors and exit code 1 for the web app part. The problem is the connection string. I believe I have to change it to something else, but I don't know what it should be. I get the following error messages:
web_1 | 2017-06-27 22:11:54.418 ERROR 1 --- [ main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
web_1 |
web_1 | 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
Any ideas?
Each container has its own network interface with its own localhost. So change how Java points to Postgres:
spring.datasource.url=jdbc:postgresql://localhost:5432/sample
To:
spring.datasource.url=jdbc:postgresql://db:5432/sample
db will resolve to the proper Postgres IP.
Bonus. With docker-compose you don't need to build your image by hand. So change:
web:
image: myuser/manager:latest
To:
web:
build: .
I had the same problem and I lost some time to understand and solve this problem:
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.
I show all the properties so that everyone understands.
application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL82Dialect
spring.jpa.hibernate.ddl-auto=update
docker-compose.yml:
version: "3"
services:
springapp:
build: .
container_name: springapp
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/testdb
ports:
- 8000:8080
restart: always
depends_on:
- db
db:
image: postgres
container_name: db
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=testdb
- PGDATA=/var/lib/postgresql/data/pgdata
ports:
- 5000:5432
volumes:
- pgdata:/var/lib/postgresql/data
restart: always
volumes:
pgdata:
For start spring application with local database we use url localhost.
For connect to container with database we need change 'localhost' on your database service, in my case 'localhost' to 'db'.
Solution: add SPRING_DATASOURCE_URL environment in docker-compose.yml wich rewrite spring.datasource.url value for connect:
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/testdb
I hope this helps someone save his time.
You can use this.
version: "2"
services:
sample_db-postgresql:
image: postgres:9.5
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=sample
- POSTGRES_USER=sample
- POSTGRES_DB=sample
volumes:
- sample_db:/var/lib/postgresql/data
volumes:
sample_db:
You can use ENV variable to change the db address in your docker-compose.
Dockerfile:
FROM java:8
EXPOSE 8080
ENV POSTGRES localhost
ADD /target/manager.jar manager.jar
ENTRYPOINT exec java $JAVA_OPTS -jar manager.jar --spring.datasource.url=jdbc:postgresql://$POSTGRES:5432/sample
docker-compose:
`
container_name: springapp
environment:
- POSTGRES=db`

docker compose error "Connection to localhost:5432 refused."

When I run my docker-compose, I face this error:
Error message
app | 2021-09-23 11:52:51.860 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
app |
app | 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.
app | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280) ~[postgresql-42.2.5.jar!/:42.2.5]
app | at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.5.jar!/:42.2.5]
app | at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.5.jar!/:42.2.5]
I tried to change the url in my application.properties file and the error is the same.
This is my application.properties file:
application.properties
spring.datasource.url=jdbc:postgresql://postgres:5432/employees
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.show-sql=true
server.port=8086
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
`
This is my Dockerfile:
Dockerfile
from openjdk:8
copy ./target/springboot2-postgresql-jpa-hibernate-crud-example-0.0.1-SNAPSHOT.jar employee-jdbc-0.0.1-SNAPSHOT.jar
CMD ["java","-jar","employee-jdbc-0.0.1-SNAPSHOT.jar"]
And this is my docker-compose:
docker-compose
version: "3"
services:
postgres:
image: postgres:latest
network_mode: bridge
container_name: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
expose:
- 5430
ports:
- 5430:5430
environment:
- POSTGRES_PASSWORD=root
- POSTGRES_USER=postgres
- POSTGRES_DB=employees
# APP*****************************************
app:
image: app
network_mode: bridge
container_name: app
expose:
- 8081
ports:
- 8081:8081
depends_on:
- postgres
links:
- postgres
environment:
- POSTGRES_PASSWORD=root
- POSTGRES_USER=postgres
- POSTGRES_DB=employees
- POSTGRES_URL=jdbc:postgresql://postgres:5432/employees
volumes:
postgres-data:
Can you run this command to check
sudo lsof -n -u postgres |grep LISTEN or sudo netstat -ltnp | grep postgres
should show the TCP/IP addresses and ports PostgreSQL is listening on
It can be postgres is already running and your image is not running using docker. Maybe killing the process will help you if it is already running.

Docker compose with micronaut trying to connect to postgres in localhost

I have a micronaut app that I'm trying to put in a docker container and have it connect to another docker container that has a postgres image. I think I followed the procedure to tell it to connect to postgres in another container but no, when I try to run my docker image it fails because it can't connect to postgres on localhost:5432. Even though:
Dockerfile:
FROM openjdk:14
COPY target/time-*.jar time.jar
EXPOSE 8080
CMD ["java", "-Dmicronaut.environments=docker", "-Dcom.sun.management.jmxremote", "-Xmx128m", "-jar", "time.jar"]
I told it to run with my docker profile.
My application-docker.yml looks like this:
micronaut:
application:
name: time
server:
netty:
access-logger:
enabled: true
logger-name: access-logger
datasources:
default:
url: jdbc:postgresql://db:5432/postgres
driverClassName: org.postgresql.Driver
username: postgres
password: postgres
schema-generate: CREATE_DROP
dialect: POSTGRES
schema: time
jpa.default.properties.hibernate.hbm2ddl.auto: update
flyway:
datasources:
default:
enabled: true
schemas: time
...
When I ran my web container it says it's running with the docker profile but fails to connect to localhost! Why??
18:41:57.947 [main] INFO i.m.context.env.DefaultEnvironment - Established active environments: [docker]
18:41:58.625 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
18:41:59.656 [main] ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization.
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.
This is my docker-compose.yml:
version: "3.3"
services:
web:
image: time
build: .
ports:
- "8080:8080"
depends_on:
- db
links:
- db
environment:
JDBC_URL: jdbc:postgresql://db:5432/time
JDBC_HOST: db
JDBC_PORT: 5432
db:
image: "postgres"
ports:
- "5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
What am I missing?
It was trying localhost because my application-docker.yml wasn't in the jar. Someone here helped me out: Micronaut not connecting to db in yml

docker-compose: my containers connect to each other but don't connect to the host

I have a spring-boot app and a mongodb in two containers managed by docker-compose. The app seems to connect nicely to the mongodb, but neither of them can be reached by the outside, i.e. my local machine. I think I need some expert advice, here...
1) installed base :
Docker version 19.03.4, build 9013bf583a
docker-compose version 1.24.1, build 4667896b
2) docker-compose.yml:
version: "3.7"
services:
app:
build: .
restart: unless-stopped
container_name: project-service
ports:
- target: 8080
published: 8080
networks:
- project-net
working_dir: /app
depends_on:
- db
db:
image: mongo
container_name: project-db
volumes:
- type: volume
source: project-data
target: /data/db
volume:
nocopy: true
restart: unless-stopped
ports:
- target: 27017
published: 27017
networks:
- project-net
networks:
project-net:
volumes:
project-data:
3) the apps Dockerfile:
FROM maven:latest AS service
WORKDIR /src/fscl/project-service
COPY . .
RUN mvn -B -f pom.xml dependency:resolve
RUN mvn -B package -DskipTests
FROM openjdk:8-jdk-alpine AS runtime
RUN apk update && apk add bash
RUN mkdir -p /app
WORKDIR /app
COPY --from=service /src/fscl/project-service/target/project-service-0.2.0.jar .
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/project-service-0.2.0.jar"]
4) status when running :
$ docker-compose up &
relevant logs:
project-service | 2019-11-08 15:33:09.688 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[project-db:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
project-db | 2019-11-08T15:33:09.755+0000 I NETWORK [listener] connection accepted from 172.20.0.3:41964 #1 (1 connection now open)
project-db | 2019-11-08T15:33:09.771+0000 I NETWORK [conn1] received client metadata from 172.20.0.3:41964 conn1: { driver: { name: "mongo-java-driver|legacy", version: "3.11.1" }, os: { type: "Linux", name: "Linux", architecture: "amd64", version: "4.4.0-124-generic" }, platform: "Java/IcedTea/1.8.0_212-b04" }
project-service | 2019-11-08 15:33:09.809 INFO 1 --- [roject-db:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:1}] to project-db:27017
project-service | 2019-11-08 15:33:11.305 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
project-service | 2019-11-08 15:33:11.310 INFO 1 --- [ main] f.p.ProjectServiceApplication : Started ProjectServiceApplication in 5.538 seconds (JVM running for 6.383)
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aecb3d4bb468 project-service_app "java -jar /app/proj…" About an hour ago Up 2 minutes 0.0.0.0:8080->8080/tcp project-service
257985d48264 mongo "docker-entrypoint.s…" 3 hours ago Up 2 minutes 0.0.0.0:27017->27017/tcp project-db
from this I take all went well, but it didn't:
5) The problems:
curl localhost:8080/api/v4/projects
should return a JSON object, but it doesn't. The app shows no logging reaction, either. Nothing ever gets through to my service app.
$ mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
just hangs forever
$ mongo project-db:27017
MongoDB shell version v3.4.10
connecting to: project-db:27017
2019-11-08T16:42:26.869+0100 I NETWORK [thread1] getaddrinfo("project-db") failed: Name or service not known
2019-11-08T16:42:26.869+0100 E QUERY [thread1] Error: couldn't initialize connection to host project-db, address is invalid :
connect#src/mongo/shell/mongo.js:237:13
#(connect):1:6
exception: connect failed
$ mongo localhost:27017
MongoDB shell version v3.4.10
connecting to: localhost:27017
also hangs.
I believe I have followed all the examples out there, but what in the world do I need to do different ?