Run Spring Boot application using Playwright Java in Docker container - java

I have a Spring Boot application where I use Playwright Java in.
I can get it to work when I running it locally on my Windows machine but when I try to run in a docker container I get the following error:
Host system is missing dependencies to run browsers. ║
2023-02-16 23:30:37 ║ Missing libraries: ║
2023-02-16 23:30:37 ║ libnss3.so ║
2023-02-16 23:30:37 ║ libnssutil3.so
I try to run:
RUN apt-get install -y libnss3.so \
libnssutil3.so \
libsmime3.so \
libnspr4.so \
libatk-1.0.so.0 \
but it only comes back with the same error.
My Dockerfile looks like this:
FROM ubuntu:22.04
RUN apt-get install -y libnss3.so \
libnssutil3.so \
libsmime3.so \
libnspr4.so \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
FROM mcr.microsoft.com/playwright/java:v1.30.0-focal
FROM maven:3.8.3-openjdk-17
RUN mkdir /project
COPY . /project
WORKDIR /project
RUN mvn clean package -DskipTests
CMD ["java", "-jar", "target/controller-1.jar"]
I have alos tried this Dockerfile
FROM ubuntu:22.04
RUN apt-get update && playwright install-deps
FROM mcr.microsoft.com/playwright/java:v1.30.0-focal
FROM maven:3.8.3-openjdk-17
RUN mkdir /project
COPY . /project
WORKDIR /project
RUN mvn clean package -DskipTests
CMD ["java", "-jar", "target/controller-1.jar"]
with the same error.
Currently I just doing some test to see if I can get it to work so the class in the project where I use Playwright looks like this:
#Service
public class PlaywrightTest {
Playwright playwright;
public void testPlaywright() {
playwright = Playwright.create();
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(true));
BrowserContext context = browser.newContext();
Page page = context.newPage();
page.navigate("https://www.google.com/");
System.out.println(page.title());
}
}
When the application starts in docker it looks like this
2023-02-17 00:07:51 2023-02-16T23:07:51.131Z INFO 1 --- [ main] s.g.a.ControllerApplication : Started ControllerApplication in 2.693 seconds (process running for 3.142)
2023-02-17 00:07:52 BEWARE: your OS is not officially supported by Playwright; downloading fallback build.
2023-02-17 00:07:52 Downloading Chromium 110.0.5481.38 (playwright build v1045) from https://playwright.azureedge.net/builds/chromium/1045/chromium-linux.zip
.....downloading....
2023-02-17 00:08:35 Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-02-17 00:08:35 2023-02-16T23:08:35.032Z ERROR 1 --- [ main] o.s.boot.SpringApplication : Application run failed
2023-02-17 00:08:35
2023-02-17 00:08:35 java.lang.IllegalStateException: Failed to execute ApplicationRunner
2023-02-17 00:08:35 at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:761) ~[spring-boot-3.0.2.jar!/:3.0.2]
2023-02-17 00:08:35 at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:748) ~[spring-boot-3.0.2.jar!/:3.0.2]
2023-02-17 00:08:35 at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-3.0.2.jar!/:3.0.2]
2023-02-17 00:08:35 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[spring-boot-3.0.2.jar!/:3.0.2]
2023-02-17 00:08:35 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[spring-boot-3.0.2.jar!/:3.0.2]
2023-02-17 00:08:35 at se.demo.controller.controllerApplication.main(controllerApplication.java:15) ~[classes!/:1]
2023-02-17 00:08:35 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
2023-02-17 00:08:35 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
2023-02-17 00:08:35 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
2023-02-17 00:08:35 at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
2023-02-17 00:08:35 at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[controller-1.jar:1]
2023-02-17 00:08:35 at org.springframework.boot.loader.Launcher.launch(Launcher.java:95) ~[controller-1.jar:1]
2023-02-17 00:08:35 at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[controller-1.jar:1]
2023-02-17 00:08:35 at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65) ~[controller-1.jar:1]
2023-02-17 00:08:35 Caused by: com.microsoft.playwright.PlaywrightException: Error {
2023-02-17 00:08:35 message='
2023-02-17 00:08:35 ╔══════════════════════════════════════════════════════╗
2023-02-17 00:08:35 ║ Host system is missing dependencies to run browsers. ║
2023-02-17 00:08:35 ║ Missing libraries: ║
2023-02-17 00:08:35 ║ libnss3.so ║
2023-02-17 00:08:35 ║ libnssutil3.so ║
2023-02-17 00:08:35 ║ libsmime3.so ║
2023-02-17 00:08:35 ║ libnspr4.so ║
2023-02-17 00:08:35 ║ libatk-1.0.so.0 ║
2023-02-17 00:08:35 ║ libatk-bridge-2.0.so.0 ║
2023-02-17 00:08:35 ║ libcups.so.2 ║
2023-02-17 00:08:35 ║ libdrm.so.2 ║
2023-02-17 00:08:35 ║ libdbus-1.so.3 ║
2023-02-17 00:08:35 ║ libatspi.so.0 ║
2023-02-17 00:08:35 ║ libX11.so.6 ║
2023-02-17 00:08:35 ║ libXcomposite.so.1 ║
2023-02-17 00:08:35 ║ libXdamage.so.1 ║
2023-02-17 00:08:35 ║ libXext.so.6 ║
2023-02-17 00:08:35 ║ libXfixes.so.3 ║
2023-02-17 00:08:35 ║ libXrandr.so.2 ║
2023-02-17 00:08:35 ║ libgbm.so.1 ║
2023-02-17 00:08:35 ║ libxcb.so.1 ║
2023-02-17 00:08:35 ║ libxkbcommon.so.0 ║
2023-02-17 00:08:35 ║ libpango-1.0.so.0 ║
2023-02-17 00:08:35 ║ libcairo.so.2 ║
2023-02-17 00:08:35 ║ libasound.so.2 ║
2023-02-17 00:08:35 ╚══════════════════════════════════════════════════════╝
2023-02-17 00:08:35 name='Error
2023-02-17 00:08:35 stack='Error:

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.

Docker compose: Springboot connect to mysql8 databases

Im having an issue with docker-compose.
I try to implement some really basic application:
A spring boot application
Mysql
I created a Dockerfile for my springboot:
FROM maven:3.8.1-openjdk-11
FROM openjdk:11
WORKDIR /app
COPY target/app-ws.jar /app/app-ws.jar
CMD mvn clean install -DskipTests
EXPOSE 8092
# CMD ls
ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=docker", "app-ws.jar"]
In my docker compose:
version: '3'
services:
# build mysql
mysqldb:
container_name: mysqldb
image: mysql:8.0.28
restart: unless-stopped
env_file:
- .env
environment:
- MYSQL_ROOT_PASSWORD:root
- MYSQL_ROOT_USER:root
- MYSQL_DATABASE:mydbname
ports:
- 3309:3309
expose:
- 3309
volumes:
- dbdata:/var/lib/mysql
networks:
- app-network
# build app-ws
app-ws:
container_name: app-ws
restart: on-failure
build:
context: ../app-ws/
dockerfile: Dockerfile
ports:
- 8092:8092
expose:
- 8092
environment:
- SPRING_DATASOURCE_URL:jdbc:mysql://mysqldb:3309/mydbname?autoReconnect=true&useSSL=false
- SPRING_DATASOURCE_USERNAME:root
- SPRING_DATASOURCE_PASSWORD:root
depends_on:
- mysqldb
links:
- mysqldb:mysqldb
networks:
- app-network
# # # Create the private network
networks:
app-network:
driver: bridge
volumes:
dbdata:
driver: local
My application-docker.properties contains the following informations:
spring.datasource.url=jdbc:mysql://mysqldb:3309/mydbname?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=create
spring.sql.init.mode=always
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.generate-ddl=true
I have 2 issues: When running docker compose up --build I do have the following errors concerning the springboot application:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
at com.mysql.cj.NativeSession.connect(NativeSession.java:120) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:949) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:819) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
... 57 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:412) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:156) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]
My second issue is that i can't connect to the database using the port 3309. I don't know what's going wrong: i use depends on to specify my app depends on mysql, i also added the environment variables with SPRING_DATASOURCE_URL, etc.

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).

Cannot connect to Mysql server in docker from another container

I'm sitting now and looking for solution over 6 hours. Please help me. First of all I will try explain my problem. I created MySql database using image (in dcoker), but when I want create another container (Sprig Boot) and connect to the Database I cannot. I tried created only image of MySql and conntect from intellij and everything is correct. Only when I try creat project in container is problem...
My docer-compose
version: '3'
services:
api:
build: ./springboot-api-docker
ports:
- "9090:9090"
depends_on:
- database
database:
container_name: 'localhost'
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_PASSWORD: password
MYSQL_USER: user
MYSQL_DATABASE: animaldb
ports:
- "3307:3306"
restart: always
application properties
server.port = 9090
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3307/animaldb?autoReconnect=true&useSSL=false
spring.datasource.username=user
spring.datasource.password=password
and DocerFile
FROM openjdk:8u201-jdk-alpine3.9
ADD target/springboot-api-docker-0.0.1-SNAPSHOT.jar .
EXPOSE 9090
CMD java -jar springboot-api-docker-0.0.1-SNAPSHOT.jar
It's funny because... first I wantet change name of container "localhost" to another. But when I was changing that I couldn't run Spring Bot Application even from intellij. I created image of MySql in docker and I was trying to conntect in intellij. It was warking but when I want use SpringBoot Application in caontainer then is problem. I didn't find solve for me yet. I will be very thankful for each help.
My errors here.
java.sql.SQLNonTransientConnectionException: Could not create
connection to database server. Attempted reconnect 3 times. Giving up.
And all text from console. Maybe you will find it faster.
Caused by: org.hibernate.service.spi.ServiceException: Unable to
create requested service
[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] api_1 | at
org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)
~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
Caused by: com.mysql.cj.exceptions.CJCommunicationsException:
Communications link failure api_1 | api_1 | The last
packet sent successfully to the server was 0 milliseconds ago. The
driver has not received any packets from the server. api_1 |
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method) ~[na:1.8.0_201] api_1 | at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
~[na:1.8.0_201] api_1 | at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
~[na:1.8.0_201] api_1 | at
java.lang.reflect.Constructor.newInstance(Constructor.java:423)
~[na:1.8.0_201] api_1 | at
com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.NativeSession.connect(NativeSession.java:144)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:850)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | ... 58
common frames omitted api_1 | Caused by:
java.net.ConnectException: Connection refused (Connection refused)
api_1 | at java.net.PlainSocketImpl.socketConnect(Native
Method) ~[na:1.8.0_201] api_1 | at
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
~[na:1.8.0_201] api_1 | at
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
~[na:1.8.0_201] api_1 | at
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
~[na:1.8.0_201] api_1 | at
java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
~[na:1.8.0_201] api_1 | at
java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_201] api_1 |
at
com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at
com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | ... 60
common frames omitted
It's not good to use localhost as you docker container name.
Change localhost to database like it.
localhost is always your local machine or a local docker, not external resource.
docker-compose
version: '3'
services:
api:
build: ./springboot-api-docker
ports:
- "9090:9090"
depends_on:
- database
links:
- database
database:
container_name: 'database'
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_PASSWORD: password
MYSQL_USER: user
MYSQL_DATABASE: animaldb
ports:
- "3307:3306"
restart: always
application.properties:
server.port = 9090
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://database: 3306/animaldb?autoReconnect=true&useSSL=false
spring.datasource.username=user
spring.datasource.password=password
I know it's late after 2 years, but you should create network first and add two containers to the network.
docker network create network name
docker run yourcontainter --net network name

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