I have a few microservices runins on spring boot, each of them have they own postgres database. When I run docker-compose up on development windows mashin it works correctly. But when I deploy it on linux VPS host, spring can not connect to database. Individually (without environment variables) the images are launched on VPS.
Here is my docker-compose.yml
version: "3.9"
services:
gateway:
image: gateway:latest
container_name: gateway
ports:
- "8080:8080"
depends_on:
- postgres
environment:
- DATASOURCE_URL=jdbc:postgresql://postgres:5432/gateway_db
- DATASOURCE_USERNAME=postgres
- DATASOURCE_PASSWORD=postgres
- JPA_HIBERNATE_DDL_AUTO=update
restart: unless-stopped
networks:
- postgres
postgres:
image: postgres:14.4
container_name: gateway_db
environment:
POSTGRES_DB: "gateway_db"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
volumes:
- pgdata:/var/lib/postgresql/data
expose:
- 5432
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres -d postgres" ]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
restart: unless-stopped
networks:
- postgres
volumes:
pgdata:
networks:
postgres:
driver: bridge
And this is my application.properties
spring.datasource.driver-class-name=org.postgresql.Driver
spring.sql.init.mode=always
spring.sql.init.platform=postgres
spring.datasource.url=${DATASOURCE_URL}
spring.datasource.username=${DATASOURCE_USERNAME}
spring.datasource.password=${DATASOURCE_PASSWORD}
spring.jpa.database=postgresql
spring.jpa.hibernate.ddl-auto=${JPA_HIBERNATE_DDL_AUTO}
So, here's spring logs from docker container on host machine
I would be very grateful for any help!
Related
This was working last 3 months ago and now i check its error. The error show that Could not connect to database using connectionString
Already try this Problem with connecting mongo express service in docker
Docker-compose.yaml
version: '3.8'
networks:
default:
name: product-poc-project-net
external: true
services:
loggermicroservice:
image: loggermicroservice
container_name: loggermicroservice
build:
context: ./
dockerfile: Dockerfile
ports:
- 8087:80
depends_on:
- mongodb
#----------------------------------------------------------------
mongodb:
image: mongo:latest
container_name: mongodb
restart: unless-stopped
ports:
- 27017:27017
environment:
MONGO_INITDB_DATABASE: LoggerActivity
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
volumes:
- ./database-data:/data/db
#----------------------------------------------------------------
mongo-express:
image: mongo-express:latest
container_name: mongo-express
restart: unless-stopped
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: root
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_PORT: 27017
ME_CONFIG_MONGODB_URL: mongodb://root:root#mongodb:27017/
application.properties
spring.data.mongodb.host=mongodb
spring.data.mongodb.port=27017
spring.data.mongodb.database=LoggerActivity
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=root
spring.data.mongodb.password=root
Error 1
Could not connect to database using connectionString: mongodb://root:root#mongodb:27017/"
mongo-express | (node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [Error: getaddrinfo EAI_AGAIN mongodb
Error 2
Caused by: java.net.UnknownHostException: mongodb: Temporary failure in name resolution
fix in mongo-express side by removing ME_CONFIG_MONGODB_PORT: 27017
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: root
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_URL: mongodb://root:root#mongodb:27017/"
I try to implement a SpringBoot Application with a MariaDB Connection running on Docker.
But unfortunately SpringBoot is not able to connect with the DB Container.
This is my docker-compose.yml:
version: "3.4"
services:
db:
image: mariadb:10.6
environment:
MYSQL_ROOT_PASSWORD: 'admin'
MYSQL_DATABASE: 'ghp_board'
MYSQL_USER: 'ghp_board_admin'
MYSQL_PASSWORD: 'ghp_board'
volumes:
- ./data/mariadb:/docker-entrypoint-initdb.d
ports:
- "33006:3306"
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "8081:8080"
container_name: ghp_board_frontend
volumes:
- ./frontend:/usr/src/app/ghp_board_frontend
depends_on:
- db
backend:
build:
context: ./backend
dockerfile: Dockerfile
depends_on:
- db
ports:
- "8886:8080"
mailhog:
image: mailhog/mailhog:latest
ports:
- "8025:8025"
- "1025:1025"
And this is my application.properties:
spring.datasource.url=jdbc:mariadb://db:3306/ghp_board
spring.datasource.driverClassName=org.mariadb.jdbc.Driver
spring.datasource.username=ghp_board_admin
spring.datasource.password=ghp_board
logging.level.org.hibernate.SQL=debug
spring.flyway.enabled=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
The DB-Container is running. I can connect to it via docker-compose exec db /bin/bash and then mysql -u ghp_board_admin -p
But if I am running docker-compose up --build or gradlew clean build I will get the following error:
java.sql.SQLNonTransientConnectionException: Socket fail to connect to host:address=(host=db)(port=3306)(type=primary). db
Can anyone tell me what I did wrong?
I already tried everything i found, i know there are a few more questions about this but nothing worked.
I tried to change listen addresses from postgresql.conf, i tried changing methods to TRUST in pg_hba.conf, i tried to restart Postgresql servers...
When i execute docker compose-up my spring boot app works but is doesnt connect to the database, always saying that the port 5432 is already used.If i stop postgresql running on 5432 and i try to run again docker compose-up it says that 5432 is not found and it cannot connect, so basically i need to start it.
application.properties
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username= postgres
spring.datasource.password=abc
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
dockerfile-compose.yml
version: "3"
services:
postgres:
image: postgres:latest
network_mode: bridge
container_name: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
expose:
- 5432
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=abc
- POSTGRES_USER=postgres
- POSTGRES_DB=books
restart: unless-stopped
# APP*****************************************
springbootapp:
image: spring-app:latest
network_mode: bridge
container_name: spring-app
expose:
- 8080
ports:
- 8081:8080
restart: unless-stopped
depends_on:
- postgres
links:
- postgres
volumes:
postgres-data:
Dockerfile
FROM openjdk:12-jdk-alpine
VOLUME /tmp
EXPOSE 8081
RUN mkdir -p /app/
RUN mkdir -p /app/logs/
ADD target/spring-app-0.0.1-SNAPSHOT.jar /app/app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=container", "-jar", "/app/app.jar"]
I'm trying to develop application which have 3 microservices, postgres database and eureka server for discovering clients. Locally all works perfect. The problem appears when i'm deploying services on docker containers. One container per service, for postgres and for eureka. I'm create a network in docker and join all containers. The question is what should I do to discover my microservices in eureka server? If anyone will want to help I will provide more details to resolve the problem.
That's my docker-compose
version: "3.7"
networks:
default:
name: microservices_nw
driver: bridge
services:
postgresService:
image: postgres:latest
container_name: postgres
expose:
- 5432
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
- POSTGRES_PASSWORD=xxx
- POSTGRES_USER=xxx
- POSTGRES_DB=work
restart: unless-stopped
# EUREKA SERVER#####
eureka-server:
image: eureka-server:latest
container_name: eureka-server
expose:
- 8761
restart: unless-stopped
# APP*****************************************
credit-microservice:
image: credit-microservice:latest
container_name: credit-microservice
expose:
- 8600
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgresService/work
- SPRING_DATASOURCE_USERNAME=xxx
- SPRING_DATASOURCE_PASSWORD=xxx
restart: unless-stopped
product-micorservice:
image: product-microservice:latest
container_name: product-micorservice
expose:
- 8080
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgresService/work
- SPRING_DATASOURCE_USERNAME=xxx
- SPRING_DATASOURCE_PASSWORD=xxx
restart: unless-stopped
customer-microservice:
image: customer-microservice:latest
container_name: customer-microservice
expose:
- 8081
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgresService/work
- SPRING_DATASOURCE_USERNAME=xxx
- SPRING_DATASOURCE_PASSWORD=xxx
- EUREKA_INSTANCE_PREFER_IP_ADDRESS=true
- EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server/eureka
depends_on:
- eureka-server
restart: unless-stopped
Dockerfile for customer-microservice
FROM openjdk:8
ADD target/customer-service.jar customer-service.jar
EXPOSE 8081
CMD java -jar customer-service.jar
Dockerfile for product-microservice
FROM openjdk:8
ADD target/product-service.jar product-service.jar
EXPOSE 8080
CMD java -jar product-service.jar
Dockerfile for eureka-server
FROM openjdk:8
ADD target/eureka-server.jar eureka-server.jar
EXPOSE 8761
ENTRYPOINT ["java", "-jar", "eureka-server.jar"]
Dockerfile for credit-microservice
FROM openjdk:8-jre
ADD target/credit-service.jar credit-service.jar
EXPOSE 8600
ENTRYPOINT ["java","-jar", "credit-service.jar"]
And application.yml for customer-service
server:
port: 8081
eureka:
client:
service-url:
default-zone: ${EUREKA_SERVER:http://localhost:8761/eureka}
## PostgreSQL
spring:
application:
name: customer-service
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/work
username: xxx
password: xxx
jpa:
hibernate.ddl-auto: validate
properties:
hibernate.jdbc.lob.non_contextual_creation: true
I'm using Docker-compose in my project when i'm trying to inside a project to connect to a host in my VBox "localhost:22000" it causes an Exception connection refused
version: '2'
services:
mongodb:
image: mongo
ports:
- "27017:27017"
command: mongod --smallfiles
rabbitmq:
image: rabbitmq:3.5.3-management
ports:
- "5672:5672"
- "15672:15672"
broker:
image: java:openjdk-8u91-jdk
depends_on:
- mongodb
- rabbitmq
working_dir: /app
volumes:
- ./blockchain-rabbitmq/target/:/app
command: java -jar /app/blockchain-rabbitmq-0.0.1.jar
ports:
- "8484:8484"
links:
- rabbitmq
- mongodb
environment:
SPRING_DATA_MONGODB_URI: mongodb://mongodb/ethereum
RABBIT_HOST: rabbitmq
SPRING_RABBITMQ_HOST: rabbitmq
nfb:
image: java:openjdk-8u91-jdk
depends_on:
- mongodb
- broker
working_dir: /app
volumes:
- ./coordinator/target/:/app
command: java -jar /app/coordinator-0.0.1.jar
ports:
- "8383:8383"
links:
- mongodb
environment:
SPRING_DATA_MONGODB_URI: mongodb://mongodb/ethereum
is there a way to expose some hosts or port ( i got this kind of exception before that when i'm dealing with mongo and rabbit mq and i resolved it buy using links and env vars )