unable to connect to running postgresql container from local spring boot project - java

I have created a docker-compose.yml to run a postgresql database . While the image is pulled and can run succesfully I am unable to connect to my database through a spring boot project I have . When I try to run my spring boot project after succesfully running my postgresql container with docker-compose up I get the error org.postgresql.util.PSQLException: The connection attempt failed. and a huge text of error logs underneath where I spotted the cause.
Caused by: java.net.UnknownHostException: postgresqldb
which is the name of my db image .
My docker-compose.yml at the root of my spring project
version: '3.1'
services:
postgresqldb:
image: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=booksdb
My application.properties in my spring boot project .
server.port=8081
server.servlet.context-path=/rest
#Postgres
spring.datasource.url=jdbc:postgresql://postgresqldb:5432/booksdb
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
#JWT
jwt.secret-key=someKey
I would appreciate your help .

Maybe you should add a hostname to the container:
version: '3.1'
services:
postgresqldb:
image: postgres
hostname: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=booksdb
docker uses its own network and resolves hostname internally. You should assign a hostname to the running container that may be found for the rest of the containers.

Related

Spring Boot MariaDB Docker Connection

I'm stuck with SQLNonTransientConnectionException in Spring Boot application with MariaDB database using docker.
Docker-compose.yaml
version: '3.1'
services:
stats-server:
container_name: stats-server
build:
dockerfile: Dockerfile-statsserver
context: ./stats-server
ports:
- "9090:9090"
depends_on:
- stats-db
stats-db:
container_name: stats-db
image: mariadb
ports:
- "9091:3306"
environment:
- MARIADB_ROOT_PASSWORD=root
- MARIADB_DATABASE=stats
- MARIADB_USER=stats_user
- MARIADB_PASSWORD=stats_password
application.properties
#port
server.port=9090
#MariaDB
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://stats-db:9091/stats
spring.datasource.username=stats_user
spring.datasource.password=stats_password
spring.jpa.hibernate.ddl-auto=update
After docker-compose up command I've got the SQLNonTransientConnectionException error:
java.sql.SQLNonTransientConnectionException: Socket fail to connect to host:address=(host=stats-db)(port=9091)(type=primary). Connection refused
Database container runs well.
With Intellij Idea I can connect by this url:
jdbc:mariadb://localhost:9091/stats
my bad, should been use internal container port
spring.datasource.url=jdbc:mariadb://stats-db:3306/stats

Docker: Springboot container cannot connect to PostgreSql Container

I am building a Spring Boot application which uses PostgreSQL with docker-compose.
When I run my containers using docker-compose up --build, my Spring Boot application fails to start because it does not find the PostgreSQL container's hostname.
Spring Boot Dockerfile
FROM maven:3.6.3-openjdk-14-slim AS build
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean package
FROM openjdk:14-slim
COPY --from=build /usr/src/app/target/server-0.0.1-SNAPSHOT.jar /usr/app/server-0.0.1-SNAPSHOT.jar
EXPOSE 9000
ENTRYPOINT ["java","-jar","/usr/app/server-0.0.1-SNAPSHOT.jar"]
docker-compose.yml
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: my_db
ports:
- "5432:5432"
volumes:
- db-data:/var/lib/postgresql/data
networks:
- db-network
restart: always
server:
build: './server'
depends_on:
- db
restart: always
ports:
- "9000:9000"
networks:
- db-network
volumes:
- ./server:/server
networks:
db-network:
volumes:
db-data:
application.properties
spring.datasource.url=jdbc:postgresql://db:5432/my_db
spring.datasource.username=postgres
spring.datasource.password=postgres
Error output
Caused by: java.net.UnknownHostException: db
My guess is that docker-compose's virtual network isn't created yet during the build stage of the Spring Boot Dockerfile.
Any idea on how to solve this issue ?
Lots of info here: https://docs.docker.com/compose/networking/
Within the web container, your connection string to db would look like
postgres://db:5432, and from the host machine, the connection string
would look like postgres://{DOCKER_IP}:8001.
What this is saying is db:5432 is fine to use within docker-compose.yaml and the IP address will be passed (not "db"), but using it externally within your application code isn't going to work. You could however pass from docker-compose.yaml db as an application input variable, which your application could fill in in the configuration file. This would enable you then to connect.
Externalising configuration like this is fairly common practice so should be a relatively easy fix.
eg:
Docker-compose.yaml
version: '3'
services:
db:
container_name: db
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: my_db
ports:
- "5432:5432"
volumes:
- db-data:/var/lib/postgresql/data
networks:
- db-network
restart: always
server:
build: './server'
depends_on:
- db
environment:
DB_HOST: db # untested, but there should be a way to pass this in
DB_PORT: 5432
DB_DATABASE: my_db
DB_USER: postgres
DB_PASSWORD: postgres
restart: always
ports:
- "9000:9000"
networks:
- db-network
volumes:
- ./server:/server
networks:
db-network:
volumes:
db-data:
Then have an application.properties file located under src/main/java/resources/application.properties
spring.datasource.url=jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_DATABASE}
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
This post completely solved my issue.
It turns out that maven was trying to establish the connection to the database while building the .jar file. All I had to do is modify my Dockerfile with this line RUN mvn -f /usr/src/app/pom.xml clean package -DskipTests.
Please do note while building the images the service will not have access to the database as it is not yet running . Only after the images are built and the containers are running do the services have access . So when you try to pass a host as db , it is not yet available in the build stage . It is available only once the db container starts running .

(Docker+SpringBoot+Mysql) Mysql connection refused

Problem Description
Spring Boot(app) container cant connect to "mysql" container
Problem Output
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
application.properties
spring.datasource.username = root
spring.datasource.url = jdbc:mysql://mysql:3306/fms?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
spring.datasource.password = manager#123
docker-compose.yml
version: '3.7'
services:
mysql:
image: mysql:latest
restart: always
command: --default-authentication-plugin=mysql_native_password
ports:
- "33061:3306"
networks:
- spring-boot-mysql-network
environment:
MYSQL_DATABASE: fms
MYSQL_ROOT_PASSWORD: manager#123
volumes:
- ./database_storage:/docker-entrypoint-initdb.d
app:
build:
context: .
dockerfile: app.Dockerfile
ports:
- "8091:8080"
networks:
- spring-boot-mysql-network
depends_on:
- mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
restart: always
depends_on:
- mysql
environment:
PMA_HOST: database
PMA_PORT: 3306
ports:
- "9091:80"
networks:
spring-boot-mysql-network:
driver: bridge
I had done a silly mistake.
I was updating my application.property file and docker-compose up
--build.But I never repackaged war file.So I was reading my old war file and thus reading my old property file
Your settings look correct to me. However, you are using the latest docker image of MySQL (which is 8.0.x at the time of writing). So, the exception you got indicates that there is a compatibility issue between your MySQL connector and MySQL server version. Which version of MySQL Connector/J are you using in your Spring Boot application? You need to update the connector or downgrade the MySQL docker image version.
Could you try updating mysql connector version to mysql-connector-java-8.0.11

Docker compose with dependent mysql gives connection refused

I have a simple Java application using a mysql.
This is my docker-compose.yaml:
version: '2.1'
services:
docker-mysql:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test2
- MYSQL_PASSWORD=root
ports:
- 3306:3306
my-app:
image: 0935a6b56fe5
depends_on:
- docker-mysql
ports:
- 8080:8080
environment:
- DATABASE_HOST=docker-mysql
- DATABASE_USER=root
- DATABASE_PASSWORD=root
- DATABASE_NAME=test2
- DATABASE_PORT=3306
Image with id 0935a6b56fe5 is a mysql image.
And this is my application.properties:
## Server config
server.port=8080
server.tomcat.uri-encoding=utf-8
## MYSQL JPA
spring.jpa.database=MYSQL
spring.jpa.show-sql=true
spring.datasource.url=jdbc:mysql://localhost:3306/test2?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=
spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=true
spring.datasource.validationQuery=SELECT 1
#Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=none
spring.datasource.driverClassName=com.mysql.jdbc.Driver
When i try docker-compose up application tries to run but it gives me a Connection Refused error.
What is wrong with this?
Most likely this line
spring.datasource.url=jdbc:mysql://localhost:3306/test2?characterEncoding=utf8
should read
spring.datasource.url=jdbc:mysql://docker-mysql:3306/test2?characterEncoding=utf8
because your my-app is running inside of the docker container and localhost is not what you think it is. You can access other docker containers inside of docker-compose with the name of the service you have chosen inside your docker-compose.yml.
Your spring.datasource.url=jdbc:mysql://localhost:3306/test2?characterEncoding=utf8 refers to localhost. Can you try spring.datasource.url=jdbc:mysql://docker-mysql:3306/test2?characterEncoding=utf8
Also the spring.datasource.password= is blank. How do you provide the password?

Issue linking containers in Docker using Docker-compose

I'm using Docker to get my micro services architecture ready.
I'm facing some problem trying to link one container with another using docker-compose.
Basically I have a container for a postgressql image, and the a java micro service developed with spring boot that should connect to the database container.
So I'm setting a link in docker-compose.yml and referencing the db container ip as 'db' using :
- "JAVA_OPTS=-Dpostgres.host=db"
However I'm getting the following error starting the microservice with docker:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'postgress.host' in value "jdbc:postgresql://${postgress.host}:5432/docker"
So basically this placeholder cannot be resolved, but normally docker-composed should take care of setting this system variable to point to the db container IP address right?
What I'm doing wrong?
Below the files involved:
docker-compose.yml:
version: "2"
services:
microservices:
build: ./microservices
container_name: microservices
links:
- db
- consul
environment:
- "JAVA_OPTS=-Dpostgres.host=db"
consul:
image: consul
container_name: consul
ports:
- "8500:8500"
db:
image: postgres
container_name: local-postgres9.6
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: docker
POSTGRES_USER: docker
application.yml of the microservice:
server:
port: 8081
project:
jdbc:
url: jdbc:postgresql://${postgres.host}:5432/docker
driver: org.postgresql.Driver
username: docker
password: docker
Try ${db.host}, according to the documentation:
Containers for the linked service will be reachable at a hostname
identical to the alias, or the service name if no alias was specified.

Categories