Eureka server cannot discover clients when running services on docker containers - java

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

Related

docker-compose fails on host mashine

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!

Spring Boot SQL with Docker - Could not connect to adress - Connection refused

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?

Access images saved in a Docker Tomcat container

I have the following Docker project running on a Linux VM. When I log into localhost, thanks to the website, I can save a photo on the Tomcat container on the server. I want to access the images uploaded to Tomcat to make a backup. How can I do? Why is my code not working?
- ./volumes/tomcat:/usr/local/tomcat/webapps/
Below I report all my project:
/home/gi/Dropbox/SERVER/DOCKER-LINUX/ServerDocker02/https/localhost-rsa-cert.pem
/home/gi/Dropbox/SERVER/DOCKER-LINUX/ServerDocker02/https/localhost-rsa-key.pem
/home/gi/Dropbox/SERVER/DOCKER-LINUX/ServerDocker02/https/server.xml
/home/gi/Dropbox/SERVER/DOCKER-LINUX/ServerDocker02/tomcat/Dockerfile
FROM tomcat:9.0.63-jdk11
LABEL Author="Nome Cognome"
EXPOSE 8080
EXPOSE 8443
COPY ./serverfile/*.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]
/home/gi/Dropbox/SERVER/DOCKER-LINUX/ServerDocker02/docker-compose.yaml
version: '3.8'
services:
postgresql-postgis:
...
pgadmin:
...
tomcat:
build: ./tomcat
image: image-tomcat-eb:v.1.0
container_name: container-tomcat-eb
ports:
- 6003:8080
- 7003:8443
volumes:
- ./https/localhost-rsa-cert.pem:/usr/local/tomcat/ssl/localhost-rsa-cert.pem
- ./https/localhost-rsa-key.pem:/usr/local/tomcat/ssl/localhost-rsa-key.pem
- ./https/server.xml:/usr/local/tomcat/conf/server.xml
- ./volumes/tomcat:/usr/local/tomcat/webapps/
depends_on:
- postgresql-postgis
- pgadmin
restart: on-failure
networks:
- eb
mysql:
...
phpmyadmin:
...
php:
...
volumes:
data-postgresql:
data-mysql:
networks:
eb:

Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. 2

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"]

Unable to connect to mysql in docker container from another docker container

I have two docker containers: mysql and spring boot service.
Service container is not connecting to mysql container(failing on the deploy) with following exception:
CommunicationsException: Communications link failure
So far I have tried using docker --link, docker networks, running everything using/without docker compose.
I am able to connect to DB container outside of docker.
Connection string: jdbc:mysql://db:3306/somedb
docker-compose.yml
version: '3.7'
services:
app:
build: .
restart: unless-stopped
ports:
- "80:8060"
links:
- db
depends_on:
- db
db:
image: "mysql:latest"
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_DATABASE=somedb
- MYSQL_ROOT_PASSWORD=somepassword
ports:
- "3306:3306"
volumes:
- /Users/someuser/someproject/mysql/data:/var/lib/mysql
I had the same problem earlier and i tried with by changing the mysql image version. Here is my compose yaml file where i am able to connect it with.
version: "3.0"
services:
spring-boot-container:
build: .
ports:
- "8086:8086"
links:
- mysql-standalone
mysql-standalone:
image: mysql:5.6
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: springsecurity
MYSQL_USER: sa
MYSQL_PASSWORD: password
So in my service dockerfile I say:
ADD target/app-1.0-SNAPSHOT.jar /app-service.jar
RUN sh -c 'touch /app-service.jar'
But I forgot to rebuild jar after making changes in service config files
TL;DR I'm retarded

Categories