Docker Compose + Spring Boot + Postgres connection, resulting in error - java

I have a Java Spring Boot app which works with a Postgres database. I want to use Docker for both of them. Initially, I created a docker-compose.yml file as given below:
version: '3'
services:
db:
container_name: sample_db
image: postgres
volumes:
- sample_db:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
- POSTGRES_DB="employee"
- POSTGRES_USER="user"
- POSTGRES_PASSWORD="password"
- PGDATA=/var/lib/postgresql/data/pgdata
web:
build: .
expose:
- "8080"
depends_on:
- db
volumes:
sample_db: {}
Then, in my Spring Boot app, inside the application.properties file I defined the following properties.
spring.datasource.url=jdbc:postgresql://db:5432/employee
spring.datasource.username=user
spring.datasource.password=password
along with this,I created a Dockerfile in my project directory, which looks like this:
FROM openjdk:8-jdk-alpine
EXPOSE 8080
ARG JAR_FILE=target/employee-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} employee-demo.jar
ENTRYPOINT ["java","-jar","employee-demo.jar"]
I issued these commands and ended up in the error as given below
mvn clean package
docker-compose up
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:
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
web_1 | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at org.postgresql.Driver.makeConnection(Driver.java:454) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at org.postgresql.Driver.connect(Driver.java:256) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.2.0.jar!/:na]
web_1 | at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
web_1 | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
web_1 | at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:43) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
web_1 | ... 44 common frames omitted
web_1 | Caused by: java.net.UnknownHostException: sample_db
web_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_181]
web_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_181]
web_1 | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_181]
web_1 | at org.postgresql.core.PGStream.<init>(PGStream.java:70) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1 | ... 58 common frames omitted
Am new to docker, If any issues please inform

Your service name is db, which is what you'll need to reference as the hostname from your web container. The container name, sample_db, is not resolvable from the web container because you have not specified a network, and thus are using the default bridge network.
According to the docs, the default bridge network doesn't provide name resolution between containers, only IP. Using docker compose, you should be able to resolve using the service name.
https://docs.docker.com/network/bridge/
https://docs.docker.com/compose/networking/
version: '3'
services:
db:
container_name: sample_db
image: postgres
volumes:
- sample_db:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
- POSTGRES_DB="employee"
- POSTGRES_USER="user"
- POSTGRES_PASSWORD="password"
- PGDATA=/var/lib/postgresql/data/pgdata
networks:
- my_network
web:
build: .
expose:
- "8080"
depends_on:
- db
networks:
- my_network
volumes:
sample_db: {}
networks:
my_network:
driver: bridge
A link should also work instead of defining your own custom bridge network:
web:
build: .
expose:
- "8080"
depends_on:
- db
links:
- "db:sample_db"

It was because of the " " given mistakenly. I changed it to
version: '3'
services:
db:
restart: always
image: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_DB=employee
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
web:
build: .
ports:
- 8080:8080
Now it is working fine. Thank you for your valuable time.

Related

Spring-boot application in docker container is unable to connect to mongodb docker container

When I trying to run my spring-boot application on docker , it is not able to connect to the Mongodb container running in docker.
Application.properties
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
Dockerfile for spring application
FROM openjdk
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yaml
version: '3'
services:
mongo:
image: mongo:latest
container_name: docker-mongo
ports:
- "27017:27017"
wacaj_docker:
image: wacaj
depends_on:
- mongo
container_name: wacaj
build: .
ports:
- "8080:8080"
I am able to connect to the mongodb container from my local system , but the spring-application container is not able to connect to mongo container.
And also if I run the spring application in local it is able to connect to the mongo runneing in docker container.
I am unable to troubleshoot the problem.
I tried changing hostname to both servicename i.e. mongo
and container name i.e. docker-mongo, but it is still not working.
2023-01-07 08:50:03.146 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
wacaj |
wacaj | com.mongodb.MongoSocketOpenException: Exception opening socket
wacaj | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:180) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:193) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:157) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
wacaj | Caused by: java.net.ConnectException: Connection refused
wacaj | at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
wacaj | at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
wacaj | at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:539) ~[na:na]
wacaj | at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:594) ~[na:na]
wacaj | at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
wacaj | at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
wacaj | at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.6.1.jar!/:na]
You need to change the hostname:
spring.data.mongodb.host=mongo
Docker makes every service in a docker compose setup available under its service name, in your case mongo and wacaj_docker.
Please refer to the docs for further details.

Failed to connect to mongodb with docker-compose

I have a web server written in java that connect to mongo db
my docker-compose file is:
version: "3.2"
services:
web:
image: org/web-server:latest
restart: always
env_file: .env
depends_on:
- mongo
ports:
- "8080:9999"
networks:
- web-network
mongo:
image: mongo
restart: always
ports:
- 27017:27017
networks:
- web-network
volumes:
- "mongodbdata:/data/db"
networks:
web-network:
volumes:
mongodbdata:
Exception is:
org.mongodb.driver.cluster - Exception in monitor thread while connecting to server localhost:27017
bot_1 | com.mongodb.MongoSocketOpenException: Exception opening socket
bot_1 | at com.mongodb.internal.connection.AsynchronousSocketChannelStream$OpenCompletionHandler.failed(AsynchronousSocketChannelStream.java:124)
bot_1 | at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:128)
bot_1 | at sun.nio.ch.Invoker$2.run(Invoker.java:218)
bot_1 | at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
bot_1 | at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
bot_1 | at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
bot_1 | at java.lang.Thread.run(Thread.java:748)
bot_1 | Caused by: java.net.ConnectException: Connection refused
bot_1 | at sun.nio.ch.UnixAsynchronousSocketChannelImpl.checkConnect(Native Method)
bot_1 | at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishConnect(UnixAsynchronousSocketChannelImpl.java:252)
bot_1 | at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finish(UnixAsynchronousSocketChannelImpl.java:198)
bot_1 | at sun.nio.ch.UnixAsynchronousSocketChannelImpl.onEvent(UnixAsynchronousSocketChannelImpl.java:213)
bot_1 | at sun.nio.ch.EPollPort$EventHandlerTask.run(EPollPort.java:293)
bot_1 | ... 1 common frames omitted
When you try to connect to mongo with mongodb://localhost:27017 your application look for local container which is the one that host only the app name web in docker compose
The easiest way to solve this problem is to set your uri to mongodb://mongo:27017
But if you like you use mongodb://localhost:27017 you should use Link option in docker compose.
For more info look at this doc: https://docs.docker.com/compose/networking/#links

Springboot Microservice running on docker not able to connect to mysql

I am using spring-boot to run my microservice on docker which is using MySQL, but my app is not able to make the connection with MySQL
The exception is :
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
app_1 | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
app_1 | at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
app_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:121)
app_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)
app_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
app_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477)
app_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560)
app_1 | ... 69 more
app_1 | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
app_1 |
app_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
app_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
app_1 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
app_1 | at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
app_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
app_1 | at com.mysql.cj.NativeSession.connect(NativeSession.java:144)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
app_1 | ... 77 more
app_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
app_1 | at java.net.PlainSocketImpl.socketConnect(Native Method)
app_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
app_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
app_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
app_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
app_1 | at java.net.Socket.connect(Socket.java:589)
app_1 | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
app_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
app_1 | ... 80 more
the docker-compose file is :
version: '3'
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_DATABASE=inward
# So you don't have to use root, but you can if you like
- MYSQL_USER=root
# You can use whatever password you like
- MYSQL_PASSWORD=unroot
# Password for root access
- MYSQL_ROOT_PASSWORD=unroot
ports:
# <Port exposed> : < MySQL Port running inside container>
- 3307:3306
app:
build:
context: ./
dockerfile: Dockerfile
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/inward?allowPublicKeyRetrieval=true&useSSL=false
depends_on:
- db
working_dir: /app
command: [sh, -c, 'mkdir -p ~/logs/; cd /src ; mvn clean spring-boot:run -Dspring.profiles.active=dev -DLOG_DIR=/root/logs/ -DLOG_FILE=hubstamper.log']
ports:
- "8080:8080"
volumes:
- "${HOME}/.m2:/root/.m2"
Dockerfiule
FROM maven:3.5-jdk-8-alpine
COPY . /app
and application.properties file is :
jdbcUrl=jdbc:mysql://db:3306/inward?allowPublicKeyRetrieval=true&useSSL=false
dataSource.user=root
dataSource.password=unroot
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
driverClassName=com.mysql.jdbc.Driver
dataSource.driverClassName=com.mysql.jdbc.Driver
I have tried the solution mentioned in the link :
Communications link failure , Spring Boot + MySql +Docker + Hibernate
any help will be higly appriciable.
I could solve this problem with docker-compose file
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_DATABASE=inward
# So you don't have to use root, but you can if you like
- MYSQL_USER=root
# You can use whatever password you like
- MYSQL_PASSWORD=unroot
# Password for root access
- MYSQL_ROOT_PASSWORD=unroot
ports:
# <Port exposed> : < MySQL Port running inside container>
- 3307:3306
app:
build:
context: ./
dockerfile: Dockerfile
depends_on:
- db
working_dir: /app
command: [sh, -c, 'mkdir -p ~/logs/; cd /src ; mvn clean spring-boot:run -Dspring.profiles.active=local -DLOG_DIR=/root/logs/ -DLOG_FILE=hubstamper.log']
ports:
- 8080:8080
volumes:
- "${HOME}/.m2:/root/.m2"
and apllication-local.properties file
jdbcUrl=jdbc:mysql://db:3306/inward?allowPublicKeyRetrieval=true&useSSL=false
dataSource.user=root
dataSource.password=unroot
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
driverClassName=com.mysql.jdbc.Driver
dataSource.driverClassName=com.mysql.jdbc.Driver
and docker file
FROM maven:3.5-jdk-8-alpine
COPY . /app
And please make sure every time you change anything in code, remove the old docker image, and create a new one(This is also I was missing in my approach).

Docker + Spring Boot + Postgres-org.postgresql.util.PSQLException: The connection attempt failed

I have a Java Spring Boot app which works with a Postgres database. I want to use Docker for both of them. Initially, I created a docker-compose.yml file as given below:
version: '3.2'
services:
postgres:
restart: always
container_name: sample_db
image: postgres:10.4
ports:
- '5432:5432'
environment:
- POSTGRES_PASSWORD=root
- POSTGRES_USER=root
- POSTGRES_DB=test
# APP**
web:
build: .
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/test
expose:
- 8080
ports:
- 8080:8080
Then,inside the application.properties file I defined the following properties.
server.port=8080
spring.jpa.generate-ddl=true
spring.datasource.url=jdbc:postgresql://db:5432/test
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.flyway.baseline-on-migrate=true
spring.flyway.enabled=false
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = none
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults: true
Also,I created a Dockerfile in my project directory, which looks like this:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
EXPOSE 8080
RUN mkdir -p /app/
RUN mkdir -p /app/logs/
ADD target/household-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 issued these commands and ended up in the error as given below.
**mvn clean package
docker build ./ -t springbootapp
docker-compose up**
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 | org.postgresql.util.PSQLException: The connection attempt failed.
web_1 | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292) ~[postgresql-42.2.8.jar!/:42.2.8]
web_1 | at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.8.jar!/:42.2.8]
web_1 | at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.8.jar!/:42.2.8]
web_1 | at org.postgresql.Driver.makeConnection(Driver.java:458) ~[postgresql-42.2.8.jar!/:42.2.8]
web_1 | at org.postgresql.Driver.connect(Driver.java:260) ~[postgresql-42.2.8.jar!/:42.2.8]
web_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.2.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:354) ~[HikariCP-3.4.2.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202) ~[HikariCP-3.4.2.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473) [HikariCP-3.4.2.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:554) [HikariCP-3.4.2.jar!/:na]
web_1 | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.4.2.jar!/:na]
web_1 | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.4.2.jar!/:na]
web_1 | at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) [hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
web_1 | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) [hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
web_1 | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) [hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
web_1 | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) [hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
web_1 | at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101) [hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
web_1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) [hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
web_1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) [hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
web_1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) [hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
web_1 | at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152) [hibernate-core-5.4.12.Final.jar!/:5.4.12.Final]
The application works fine in local but facing issue while dockerizing it. Please let me know if any issues.
In the docker-compose.yml, your postgres service is called "postgres"
services:
postgres:
so the hostname in your jdbc connection string should also be "postgres", not "db"
spring.datasource.url=jdbc:postgresql://postgres:5432/test

I am unable to connect to rabbitmq server in docker

I am using docker compose to run a sbt process which connect to rabbitmq-server . Following is my docker compose file : -
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: abhishekkumargaya/messanger
ports:
- "1883:1883"
links:
- rabbit-server
- redis
- mysql
networks:
- webnet
rabbit-server:
image: "rabbitmq:3-management"
hostname: localhost
environment:
#RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG"
RABBITMQ_DEFAULT_USER : guest
RABBITMQ_DEFAULT_PASS : guest
ports:
- "5672:5672"
- "15672:15672"
networks:
- webnet
redis:
image: "redis:alpine"
mysql:
image: mysql
container_name: database.dev
command: mysqld --user=root --verbose
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: "user_messages"
MYSQL_USER: "test"
MYSQL_PASSWORD: "root"
MYSQL_ROOT_PASSWORD: "root"
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
networks:
webnet:
volumes:
redis-data:
I am getting connection refused error. I am using default value to connect to rabbit server in my code .
private lazy val factory = new ConnectionFactory
val connection = factory.newConnection()
My docker file : -
FROM openjdk:8
ENV SBT_VERSION 0.13.16
RUN \
curl -L -o sbt-$SBT_VERSION.deb http://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \
dpkg -i sbt-$SBT_VERSION.deb && \
rm sbt-$SBT_VERSION.deb && \
apt-get update && \
apt-get install sbt && \
sbt sbtVersion
WORKDIR /app
ADD target/scala-2.11/messanger-assembly-1.0.jar /app
EXPOSE 1883
CMD java -jar messanger-assembly-1.0.jar
The error which I am getting is as follows :-
Caused by: java.net.ConnectException: Connection refused (Connection refused)
web_1 | at java.net.PlainSocketImpl.socketConnect(Native Method)
web_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
web_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
web_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
web_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
web_1 | at java.net.Socket.connect(Socket.java:589)
web_1 | at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60)
web_1 | at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:62)
web_1 | at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:99)
web_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:948)
web_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:907)
web_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:865)
web_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1018)
web_1 | at com.abhishek.rabbitmq.RabbitMqConnectionFactory$.<init>(RabbitMqConnectionFactory.scala:15)
web_1 | at com.abhishek.rabbitmq.RabbitMqConnectionFactory$.<clinit>(RabbitMqConnectionFactory.scala)
I am not properly understanding how these variable works :-
hostname: localhost
environment:
#RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG"
- RABBITMQ_DEFAULT_USER = guest
- RABBITMQ_DEFAULT_PASS = guest
I kept this variable like this because by default my client is using host as localhost , user as guest and password as guest to connect to rabbitmq-server
If I am just running rabbitmq-server image with command -
docker-compose up --build
I am able to connect it through my localmachine .
I am able to connect now. To connect to rabbit container, I need to put host as rabbit in my web container.Similarily for connection to mysql and redis , I need to use host as mysql and redis respectively.
You can depend on localhost if you set network_mode for your web to "host"

Categories

Resources