application.properties
spring.datasource.url=jdbc:postgresql://db:5432/test
spring.datasource.username=postgres
spring.datasource.password=123
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.id.new_generator_mappings=true
server.port=8000
docker-compose
version: '3.7'
services:
db:
image: postgres:11.4
ports:
- "5432:5432"
expose:
- 5432
environment:
POSTGRES_PASSWORD: "root"
POSTGRES_DB: test
lady:
depends_on:
- db
build: ./test/
ports:
- "8000:8000"
expose:
- 8000
environment:
DB_USER: "postgres"
DB_PASSWORD: "123"
DB_TYPE: "postgres"
command: ["java","-jar","test.jar"]
logs error
com.women.lady.LadyApplicationTests > contextLoads() FAILED
java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: javax.persistence.PersistenceException
Caused by: org.hibernate.exception.JDBCConnectionException
Caused by: org.postgresql.util.PSQLException
Caused by: java.net.UnknownHostException
I can't connect to postgres or db when I run docker compose: docker-compose up
Question
How to connect db on docker-compose.
Please give me example.
Related
I revised an example of Spring Boot Microservices and I have a problem about running all services on docker.
After creating the image file of each services, I devised a docker-compose.yml to run all services on Docker. I noticed some of services throw ConnectException regarding configserver but I have no idea how to fix it. Here are the code snippets to create Docker image for each service.
1 ) service-registry
- mvn clean install
- docker build -t microservicedailybuffer/serviceregistry:0.0.1 .
2 ) configserver
- mvn clean install
- docker build -t microservicedailybuffer/configserver:0.0.1 .
3 ) apigateway
- mvn clean install -D skipTests
- docker build -t microservicedailybuffer/apigateway:0.0.1 .
4 ) auth-service
- mvn clean install -D skipTests
- docker build -t microservicedailybuffer/authservice:0.0.1 .
5 ) orderservice
- mvn clean install -D skipTests
- docker build -t microservicedailybuffer/orderservice:0.0.1 .
6 ) productservice
- mvn clean install -D skipTests
- docker build -t microservicedailybuffer/productservice:0.0.1 .
7 ) paymentservice
- mvn clean install -D skipTests
- docker build -t microservicedailybuffer/paymentservice:0.0.1 .
Here is my docker-compose.yml shown below
services:
serviceregistry:
image: 'microservicedailybuffer/serviceregistry:0.0.1'
container_name: serviceregistry
ports:
- '8761:8761'
networks:
- backend
configserver:
image: 'microservicedailybuffer/configserver:0.0.1'
container_name: configserver
ports:
- '9296:9296'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
healthcheck:
test: [ "CMD", "curl", "-f", "http://configserver:9296/actuator/health" ]
interval: 10s
timeout: 5s
retries: 5
depends_on:
- serviceregistry
networks:
- backend
apigateway:
image: 'microservicedailybuffer/apigateway:0.0.1'
container_name: apigateway
ports:
- '9090:9090'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- ZIPKIN_URL=http://zipkin:9411
- REDIS_URL=redis://redis:6379
depends_on:
- configserver
- zipkin
- redis
- serviceregistry
networks:
- backend
zipkin:
image: openzipkin/zipkin
ports:
- "9411:9411"
networks:
- backend
redis:
image: redis
ports:
- "6379:6379"
networks:
- backend
authservice:
image: 'microservicedailybuffer/authservice:0.0.1'
container_name: authservice
ports:
- '7777:7777'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- spring.datasource.url=jdbc:mysql://database:3306/userdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
- spring.datasource.username=root
- spring.datasource.password=ippavlova_1990
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
networks:
- backend
productservice:
image: 'microservicedailybuffer/productservice:0.0.1'
container_name: productservice
ports:
- '8081:8081'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- spring.datasource.url=jdbc:mysql://database:3306/productdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
- spring.datasource.username=root
- spring.datasource.password=ippavlova_1990
- ZIPKIN_URL=http://zipkin:9411
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
zipkin:
condition: service_healthy
networks:
- backend
orderservice:
image: 'microservicedailybuffer/orderservice:0.0.1'
container_name: orderservice
ports:
- '8082:8082'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- spring.datasource.url=jdbc:mysql://database:3306/orderdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
- spring.datasource.username=root
- spring.datasource.password=ippavlova_1990
- ZIPKIN_URL=http://zipkin:9411
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
zipkin:
condition: service_healthy
networks:
- backend
paymentservice:
image: 'microservicedailybuffer/paymentservice:0.0.1'
container_name: paymentservice
ports:
- '8083:8083'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- spring.datasource.url=jdbc:mysql://database:3306/paymentdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
- spring.datasource.username=root
- spring.datasource.password=ippavlova_1990
- ZIPKIN_URL=http://zipkin:9411
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
zipkin:
condition: service_healthy
networks:
- backend
database:
container_name: mysql-database
image: 'mysql:latest'
ports:
- "3306:3306"
restart: always
environment:
#MYSQL_USER: root
MYSQL_PASSWORD: ippavlova_1990
MYSQL_ROOT_PASSWORD: ippavlova_1990
volumes:
- db-data:/var/lib/mysql
networks:
- backend
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10
networks:
backend:
driver: bridge
volumes:
db-data:
When I run the docker-compose file through docker-compose up, I get some problems in api gateway.
Api Gateway Issue
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://configserver:9296/API-GATEWAY/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
How can I fix all these issue?
Here is the repo : Link
Here is the git based system file : Link
After revising the api gateway shown below, the issue disappeared.
apigateway:
image: 'microservicedailybuffer/apigateway:0.0.1'
container_name: apigateway
ports:
- '9090:9090'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- ZIPKIN_URL=http://zipkin:9411
- REDIS_URL=redis://redis:6379
depends_on:
configserver:
condition: service_healthy
zipkin:
condition: service_healthy
redis:
condition: service_started
networks:
- backend
You have to update your application.yml and docker-compose.yml files.
These lines should be present to override the EUREKA_SERVER_ADDRESS
eureka:
instance:
prefer-ip-address: true
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: ${EUREKA_SERVER_ADDRESS:http://localhost:8761/eureka}
and to communicate within the container you need to use a common network.
So it should look like this.
serviceregistry:
image: 'microservicedailybuffer/serviceregistry:0.0.1'
container_name: serviceregistry
ports:
- '8761:8761'
networks:
- backend
configserver:
image: 'microservicedailybuffer/configserver:0.0.1'
container_name: configserver
ports:
- '9296:9296'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
healthcheck:
test: [ "CMD", "curl", "-f", "http://configserver:9296/actuator/health" ]
interval: 10s
timeout: 5s
retries: 5
depends_on:
- serviceregistry
networks:
- backend
I want to containerize my spring-boot rest api together with the postgres database. Here is my Dockerfile for the api:
FROM openjdk:17
WORKDIR /app
COPY ./target/docker-0.0.1-SNAPSHOT.jar ./docker-0.0.1-SNAPSHOT.jar
CMD ["java","-jar","docker-0.0.1-SNAPSHOT.jar"]
I've created a .jar file using mvm. Here is my application.yml file.
# postgres sql connection
spring:
datasource:
password: password
url: jdbc:postgresql://postgresdb:5432/todos
username: admin
jpa:
hibernate:
ddl-auto: create #create
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show-sql: true
# Configuring the port for our server
server:
port: 3001
Here is my docker-compose file:
version: "3.9"
services:
postgresdb:
image: postgres:14.4-alpine
container_name: pgsql
restart: always
volumes:
- ./db/init.sql:/docker-entrypoint-initdb.d/0_init.sql
- $HOME/database:/var/lib/postgresql/data
ports:
- "5432:5432"
expose:
- "5432"
environment:
POSTGRES_DB: todos
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
SERVICE_NAME: postgresdb
networks:
- internalnet
springboot:
container_name: springbootserver
build: .
image: springboot:1.0
ports:
- "3001:3001"
expose:
- "3001"
environment:
DB_USER: "admin"
DB_PASSWORD: "password"
depends_on:
- postgresdb
networks:
- internalnet
networks:
internalnet:
driver: bridge
If i run docker logs springbootserver I'm getting the following error:
org.postgresql.util.PSQLException: Connection to postgresdb:5232 refused. Check that the hostname and port are correct and that the postmaster is accepting
TCP/IP connections.
What am i missing here?
when I build my docker image and run it it gives this and error however if I run the jar file everything runs fine if I had it guess it would have to do with my docer-compose.yml file but idk
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.2.3.jar!/:na]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: java.net.ConnectException: Connection refused
at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.2.3.jar!/:na]
... 4 common frames omitted
Dockerfile
FROM openjdk:17
ADD target/docker-document-rest-api.jar docker-document-rest-api.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "docker-document-rest-api.jar"]
application.properties
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
spring.data.mongodb.database=example
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
docer-compose.yml
version: "3.8"
services:
mongodb:
image: mongo
container_name: mongodb
ports:
- 27017:27017
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=rootuser
- MONGO_INITDB_ROOT_PASSWORD=rootpass
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
- ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
- ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
data: {}
networks:
default:
name: mongodb_network
By default docker containers are using bridge networking, so you cannot connect to localhost from a container. There can be several ways to solve this, one would be to connect your container, which is running the Java application, to the network where MongoDB is placed.
Since in your docker-compose you explicitly specify the name of the network (mongodb_network), we can do the following:
In the properties file we have to change localhost to mongodb, so:
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
spring.data.mongodb.database=example
spring.data.mongodb.port=27017
spring.data.mongodb.host=mongodb
The reason for this is that name of the container can be used as hostname if we connect to the network in which the container is placed.
Rebuild the docker image for the Java application, run it as such:
docker run --network=mongodb_network -p 8080:8080 <docker-image-name>
(fill in your docker image name there)
While these steps might work, I suggest to add the your application to the docker-compose:
version: "3.8"
services:
mongodb:
image: mongo
container_name: mongodb
expose:
- 27017
ports:
- 27017:27017
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=rootuser
- MONGO_INITDB_ROOT_PASSWORD=rootpass
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
- ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
- ME_CONFIG_MONGODB_SERVER=mongodb
spring_application:
build: .
container_name: spring_application
ports:
- 8080:8080
depends_on:
mongodb:
condition: service_started
volumes:
data: {}
networks:
default:
name: mongodb_network
This has to be build first with docker-compose build and afterwards can be executed with docker-compose up.
You must use docker internal host network to connect with mongodb. So from your dockerized application you have to connect with the service name (as that is the hostname) instead of connecting with localhost. In your case: http://mongodb:27017 is the service (and thus hostname) of your mongo db container.
My docker-compose.yml file
version: "3.9"
services:
webapp:
image: springbootproject
build: .
ports:
- "8085:8085"
depends_on:
- postgres
- pgadmin
postgres:
container_name: postgres_container
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 12345
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
PGADMIN_CONFIG_SERVER_MODE: 'False'
volumes:
- pgadmin:/root/.pgadmin
ports:
- "${PGADMIN_PORT:-80}:80"
networks:
- postgres
restart: unless-stopped
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:
Dockerfile
FROM adoptopenjdk:11-jre-hotspot
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} springmvcrestapi.jar
EXPOSE 8085
ENTRYPOINT ["java","-jar","springmvcrestapi.jar"]
Hi everyone my simple project using docker inteelij ide terminal writing docker-compose up postgre and pgadmin working container but my project images Error: Unable to access jarfile springmvcrestapi-0.0.1-SNAPSHOT.jar What is the problem ??
I have a problem with Docker-compose. On my machine, I have a Ubuntu. And, when I deploy my app into container, I see that exception:
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:307) ~[liquibase-core-3.6.3.jar!/:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
... 26 common frames omitted
Caused by: org.postgresql.util.PSQLException: Connection to 0.0.0.0:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280) ~[postgresql-42.2.5.jar!/:42.2.5]
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.5.jar!/:42.2.5]
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.5.jar!/:42.2.5]
at org.postgresql.Driver.makeConnection(Driver.java:454) ~[postgresql-42.2.5.jar!/:42.2.5]
at org.postgresql.Driver.connect(Driver.java:256) ~[postgresql-42.2.5.jar!/:42.2.5]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) ~[HikariCP-3.2.0.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) ~[HikariCP-3.2.0.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.2.0.jar!/:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.2.0.jar!/:na]
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:302) ~[liquibase-core-3.6.3.jar!/:na]
... 28 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:240) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:591) ~[na:na]
at org.postgresql.core.PGStream.<init>(PGStream.java:70) ~[postgresql-42.2.5.jar!/:42.2.5]
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91) ~[postgresql-42.2.5.jar!/:42.2.5]
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192) ~[postgresql-42.2.5.jar!/:42.2.5]
... 40 common frames omitted
I have configuration of PostgreSQL:
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 100.200.300.50/32 trust
And that option set "listen_addresses = 'localhost'";
Docker-compose config:
version: '3'
services:
web:
service
image: webserviceimage
ports:
- 8080:8080
depends_on:
- db
- redis
environment:
POSTGRES_URL:
POSTGRES_USER:
POSTGRES_PASSWORD:
redis:
image: "redis:alpine"
restart: unless-stopped
environment:
REDIS_URL: redis:6379
db:
image: postgres
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD:
POSTGRES_USER:
How I can resolve it?
Step: 1 Create Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Step:2 Run command to create docker file
docker build -t webapp:latest -f Dockerfile .
Step:3 Add docker image into docker-compose
version: '3'
services:
web:
service
image: webapp:latest
ports:
- 8080:8080
depends_on:
- db
- redis
environment:
POSTGRES_URL:
POSTGRES_USER:
POSTGRES_PASSWORD:
redis:
image: "redis:alpine"
restart: unless-stopped
environment:
REDIS_URL: redis:6379
db:
image: postgres
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD:
POSTGRES_USER:
Notes : web app environment variable user into application.properties file like
spring.datasource.url = ${POSTGRES_URL}:${POSTGRES_PORT}/"nameofDB"
Make sure you have Postgres configured to listen on all available IP addresses. The configuration file postgresql.conf (typically located in /var/lib/postgresql/data/postgresql.conf) must have:
listen_addresses = '*'
(this is the default configuration). More here
The file pg_hba.conf contains the configuration for the host based authentication. The posted configuration means that connections coming from the IPs with the defined mask or unix sockets are trusted (don't require authentication). You don't have to change this file unless you have specific security requirements. By default remote clients must supply an MD5-encrypted password for authentication:
host all all all md5
For more info see The pg_hba.conf File
What was suggested in the comments is to change the URL of the database in the configuration of the spring boot application. From the logs the application tries to connect to 0.0.0.0 which is not a valid outbound address (see here).
Change the URL in the application properties to something like this:
spring.datasource.url=jdbc:postgresql://db:5432/<database_name>
spring.datasource.username=<database_user>
spring.datasource.password=<password>
db is the name of the database container which can be resolved by the service container to the correct database IP address.
Hope it helps