Unable to deploy the application - mysql

i am trying to deploy the application using Tomcat with the help of docker for deployment and mariaDB as database.
The respective generated war file is getting deployed but when i try to access other pages(other than the base url/landing page) its showing the following error:
web-app_1 | 07:20:00.324 [http-apr-8080-exec-3] INFO com.company.controller.MainController - in Main controller
web-app_1 | 07:20:00.806 [http-apr-8080-exec-3] DEBUG org.hibernate.stat.internal.StatisticsInitiator - Statistics initialized [enabled=false]
web-app_1 | 07:20:00.952 [http-apr-8080-exec-3] DEBUG org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unable to acquire JDBC Connection [n/a]
web-app_1 | java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=localhost)(port=833)(type=master) : Connection refused (Connection refused)
web-app_1 | at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:156)
web-app_1 | at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.getException(ExceptionMapper.java:118)
web-app_1 | at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.throwException(ExceptionMapper.java:92)
web-app_1 | at org.mariadb.jdbc.Driver.connect(Driver.java:108)
web-app_1 | at java.sql.DriverManager.getConnection(DriverManager.java:664)
web-app_1 | at java.sql.DriverManager.getConnection(DriverManager.java:208)
web-app_1 | at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
web-app_1 | at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
web-app_1 | at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:196)
and hibernate.properties file contains:
jdbc.driverClassName = org.mariadb.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/ams_dbnew
jdbc.username = username
jdbc.password = password
hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.hbm2ddl.auto = none
hibernate.format_sql = true
hibernate.show_sql = true
Although am pointing to jdbc:mysql://localhost:3306/ but in tomcat logs its showing error as java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=localhost)(port=833)(type=master) : Connection refused (Connection refused)
and database is running on same 3306 port
sudo netstat -tulpen | grep mysql
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 121 410570 51950/mysqld
can anyone tell me where and which part am doing wrong?
and docker-compose file is:
version: '3'
services:
web-app:
build:
context: .
dockerfile: Dockerfile
ports:
- 8080:8080
network_mode: "host"

I suspect the port 833, try searching the number "833" in your tomcat home directory
cd /TOMCAT_HOME_DIRECTORY/
grep -rli 833 .
may be one of the file overriding the port number.

As seeing your logs, it seems there is another database service is configured on 833 port and it is overridden while running tomcat with database app.
Make sure any database you didn't configured with this port. stop all database service then also stop closed this port via following this command:
netstat -ap | grep :
it search the process running on this port
kill -9
kill that process id running on this port no.

Related

How can I identify the issue preventing a connection between my web application Docker container and the associated MySQL Docker container? [duplicate]

This question already has answers here:
Spring Boot + docker-compose + MySQL: Connection refused
(3 answers)
Closed 5 months ago.
I am trying to deploy a small personal website (and backing MySQL) to Docker using Docker Compose. While I have managed to get the web application running on Docker, I cannot get it to connect to the database. This project is the first time I have been using Docker, so I believe I cannot spot the issue due to my inexperience. I wonder if anyone more experienced could see what I am doing wrong.
I have been going at it for days now. However, none of the solutions to similar questions has worked for me. So far, I understand that the 'Communications link failure' error I keep getting is very generic. Thus, it is difficult to pinpoint the exact problem.
I have provided the contents of my Dockerfile, docker-compose.yml, the database configurations in my application.properties, and part of the output log. I have also provided a Pastebin URL for the entire log.
Many Thanks.
Complete Log Output: https://pastebin.com/aYjWnck8
Dockerfile
FROM openjdk:17
COPY target/personal-website-0.0.1-SNAPSHOT.jar personal-website-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "personal-website-0.0.1-SNAPSHOT.jar"]
docker-compose.yml
version: "3.8"
services:
mysqldb:
container_name: personal-website-database
image: mysql:8.0.29
restart: always
ports:
- 3307:3306
environment:
MYSQL_DATABASE: skills
MYSQL_USER: chizzy
MYSQL_PASSWORD: AbcXyz
MYSQL_ROOT_PASSWORD: AbcXyz
server:
build: .
container_name: personal-website
restart: always
ports:
- 8081:8080
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysqldb:3306/skills?useSSL=false&allowPublicKeyRetrieval=true
depends_on:
- mysqldb
application.properties (Database Configuration Excerpt)
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/skills
spring.datasource.username=root
spring.datasource.password=AbcXyz
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
Log (Partial)
personal-website | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
personal-website |
personal-website | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
personal-website | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
personal-website | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
personal-website | at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
personal-website | at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
personal-website | at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
personal-website | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.NativeSession.connect(NativeSession.java:120) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:948) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | ... 57 common frames omitted
personal-website | Caused by: java.net.ConnectException: Connection refused
personal-website | at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
personal-website | at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
personal-website | at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
personal-website | at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
personal-website | at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
personal-website | at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
personal-website | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
personal-website | ... 60 common frames omitted
When you start services with docker-compose, they start at the same time. Even if you use depends_on that only insures that the service it depends on receive the start signal, not that it is actually ready to receive connections.
A java application starts quicker than a database. Unfortunately java also tries to connect to the database during startup and doesn't retry if that fails.
You should have a retry mechanism in your application or a waiting loop in your docker-compose to fix this. The waiting loop would delay the starting of the java application until the database is actually ready to receive requests. The retry mechanism in the Java application would try to connect to the database everytime it fails because a connection is not readily available.
I suggest the wait loop and you can find a very good example here: https://docs.docker.com/compose/startup-order/
I could paste that code here but it would be a good practice for you to actually read the article there and implement it. You only need to change the command of your java application and add the wait-for-it functionality (you need the script as well in the docker image ;) )

Change default path to chrome execution in karate-chrome to use chromium [duplicate]

I want to setup a headless chrome driver for UI Test Automation in jenkins.
But to run the test command
sudo -E java -jar karate-0.9.3.jar karate_GUI.feature
I have to run as root and it requires --no-sandbox, which, if I'm not wrong, it's still not supported in v0.9.3.
If possible, how can I include --no-sandbox option?
I checked https://intuit.github.io/karate/karate-core/ and there is no --no-sandbox option.
My feature configuration:
Feature: message end-point
Background:
* configure driver = { type: 'chrome', executable: '/usr/bin/google-chrome', headless: true }
# Login Url
* def browserManagementUrl = 'http://localhost:8000/login/'
Scenario: GUI Testing for Login page
Given driver browserManagementUrl
And eval driver.input('input[name=name]', 'admin')
And eval driver.input('input[name=password]', 'adminadmin')
And driver.submit('#login-button')
When driver.submit('#login-button')
Then match driver.location == 'http://localhost:8000/select/'
The linux command and it's results
sudo -E java -jar karate-0.9.3.jar karate_GUI.feature
07:15:56.296 [main] INFO com.intuit.karate.Main - Karate version: 0.9.3
07:15:57.345 [ForkJoinPool-1-worker-1] WARN com.intuit.karate - skipping bootstrap configuration: could not find or read file: classpath:karate-config.js
07:15:57.418 [chrome_1560323757416] DEBUG c.i.k.driver.chrome_1560323757416 - command: [/usr/bin/google-chrome, --remote-debugging-port=9222, --no-first-run, --user-data-dir=/var/jenkins_home/workspace/my-karate_GUI#2/integrations/target/chrome_1560323757416, --disable-popup-blocking, --headless]
07:15:57.419 [ForkJoinPool-1-worker-1] DEBUG c.i.k.driver.chrome_1560323757416 - poll attempt #0 for port to be ready - localhost:9222
07:15:57.420 [chrome_1560323757416] DEBUG c.i.k.driver.chrome_1560323757416 - env PATH: /sbin:/bin:/usr/sbin:/usr/bin
07:15:57.423 [ForkJoinPool-1-worker-1] DEBUG c.i.k.driver.chrome_1560323757416 - sleeping for millis: 250
07:15:57.674 [ForkJoinPool-1-worker-1] DEBUG c.i.k.driver.chrome_1560323757416 - poll attempt #1 for port to be ready - localhost:9222
07:15:57.675 [ForkJoinPool-1-worker-1] DEBUG c.i.k.driver.chrome_1560323757416 - sleeping for millis: 250
07:15:57.793 [chrome_1560323757416] DEBUG c.i.k.driver.chrome_1560323757416 - [0612/071557.791933:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
07:15:57.810 [chrome_1560323757416] DEBUG c.intuit.karate.shell.CommandThread - command complete, exit code: 1 - [/usr/bin/google-chrome, --remote-debugging-port=9222, --no-first-run, --user-data-dir=/var/jenkins_home/workspace/my-karate_GUI#2/integrations/target/chrome_1560323757416, --disable-popup-blocking, --headless]
07:15:57.926 [ForkJoinPool-1-worker-1] DEBUG c.i.k.driver.chrome_1560323757416 - poll attempt #2 for port to be ready - localhost:9222
07:15:57.927 [ForkJoinPool-1-worker-1] DEBUG c.i.k.driver.chrome_1560323757416 - sleeping for millis: 250
07:15:58.178 [ForkJoinPool-1-worker-1] DEBUG c.i.k.driver.chrome_1560323757416 - poll attempt #3 for port to be ready - localhost:9222
[...]
07:16:02.206 [ForkJoinPool-1-worker-1] DEBUG c.i.k.driver.chrome_1560323757416 - poll attempt #19 for port to be ready - localhost:9222
07:16:02.207 [ForkJoinPool-1-worker-1] DEBUG c.i.k.driver.chrome_1560323757416 - sleeping for millis: 250
07:16:02.848 [ForkJoinPool-1-worker-1] DEBUG c.i.k.driver.chrome_1560323757416 - request:
1 > GET http://localhost:9222/json
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Host: localhost:9222
1 > User-Agent: Apache-HttpClient/4.5.5 (Java/1.8.0_212)
07:16:02.862 [ForkJoinPool-1-worker-1] ERROR c.i.k.driver.chrome_1560323757416 - org.apache.http.conn.HttpHostConnectException: Connect to localhost:9222 [localhost/127.0.0.1] failed: Connection refused (Connection refused), http call failed after 13 milliseconds for URL: http://localhost:9222/json
07:16:02.863 [ForkJoinPool-1-worker-1] ERROR c.i.k.driver.chrome_1560323757416 - http request failed:
org.apache.http.conn.HttpHostConnectException: Connect to localhost:9222 [localhost/127.0.0.1] failed: Connection refused (Connection refused)
07:16:02.918 [pool-1-thread-1] INFO com.intuit.karate.Runner - <<fail>> feature 1 of 1: karate_GUI.feature
---------------------------------------------------------
feature: karate_GUI.feature
report: target/karate_GUI.json
scenarios: 1 | passed: 0 | failed: 1 | time: 5.4993
---------------------------------------------------------
Karate version: 0.9.3
======================================================
elapsed: 6.39 | threads: 1 | thread time: 5.50
features: 1 | ignored: 0 | efficiency: 0.86
scenarios: 1 | passed: 0 | failed: 1
======================================================
failed features:
karate_GUI: karate_GUI.feature:8 -
org.apache.http.conn.HttpHostConnectException: Connect to localhost:9222 [localhost/127.0.0.1] failed: Connection refused (Connection refused)
Exception in thread "main" picocli.CommandLine$ExecutionException: there are test failures
at com.intuit.karate.Main$1.handleExecutionException(Main.java:133)
at picocli.CommandLine.parseWithHandlers(CommandLine.java:1157)
at com.intuit.karate.Main.main(Main.java:139)
I guess you do know that the UI automation pieces are still experimental and yes I don't think we support --no-sandbox - feel free to open a ticket and help us with some links to why this is needed, what it does etc.
A suggested workaround is you can pass a batch file as the executable key to the configure driver call. In this batch file you can then call the chromedriver executable with whatever custom parameters or arguments you need.
Do let us know if that works. It also sounds to me that a way to pass any custom flags is a needed feature, do add this to your feature request.
EDIT: for those landing here in future, I'm not 100% sure, but maybe the info here will help: https://github.com/intuit/karate/issues/1134#issuecomment-638990087

Trying to connect Jira (docker) to Mysql (non docker) 5.6.44 but have Connection refused

I'm trying to configure jira on a CentOS 7 virtual machine.
I managed to launch Jira's docker image without any problems with a docker-compose :
jira:
image: 'cptactionhank/atlassian-jira-software:latest'
container_name: jira
restart: unless-stopped
healthcheck:
disable: true
volumes:
- '/var/atlassian/jira:/var/atlassian/jira'
I also installed mySQL on my virtual machine (without using a docker) while following the installation procedure described here:
https://confluence.atlassian.com/adminjiraserver/connecting-jira-applications-to-mysql-5-6-938846854.html
Including :
CREATE DATABASE jiradb CHARACTER SET utf8 COLLATE utf8_bin;
CREATE USER 'jiradbuser'#'%' IDENTIFIED BY 'secret';
GRANT ALL PRIVILEGES ON jiradb.* TO 'jiradbuser'#'%' IDENTIFIED BY 'secret';
flush privileges;
MySQL is accessible on my localhost port 3306 (the command telnet 0.0.0.0 3306 answers me well)
I also downloaded the 5.1.38 connector-j from MySQL and since I didn't have a lib folder in my /var/atlassian/jira, I created it with a mkdir and unzipped the jar in it.
I restart Jira with:
docker-compose stop jira
docker-compose rm jira
docker-compose start jira
Now, I go on my browser to the jira software address, at the time of the Database setup, I entered this information :
Database Connection: My Own Database
Database Type: MySQL 5.6
Hostname: localhost
Port: 3306
Database: jiradb
Username: jiradbuser
Password: secret
When I click on "Test Connection", it doesn't work. The answer is :
Error connecting to database
Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Connection refused (Connection refused)
The log of jira when I click on "Test Connection" is below :
==> atlassian-jira.log <==
2019-07-19 14:42:37,906 http-nio-8080-exec-21 ERROR anonymous 882x13x1 b45eq1 10.107.135.18,172.18.0.3 /secure/SetupDatabase!connectionCheck.jspa [c.a.config.bootstrap.DefaultAtlassianBootstrapManager] Could not successfully test your database:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:981)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
... 3 filtered
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.atlassian.config.bootstrap.DefaultAtlassianBootstrapManager.getTestDatabaseConnection(DefaultAtlassianBootstrapManager.java:347)
at com.atlassian.jira.config.database.JdbcDatasource.getConnection(JdbcDatasource.java:211)
at com.atlassian.jira.config.database.DatabaseConfig.testConnection(DatabaseConfig.java:88)
at com.atlassian.jira.web.action.setup.SetupDatabase.testConnection(SetupDatabase.java:230)
at com.atlassian.jira.web.action.setup.SetupDatabase.doValidation(SetupDatabase.java:194)
at com.atlassian.jira.web.action.setup.SetupDatabase.doConnectionCheck(SetupDatabase.java:126)
... 3 filtered
at java.lang.reflect.Method.invoke(Method.java:498)
at webwork.util.InjectionUtils$DefaultInjectionImpl.invoke(InjectionUtils.java:70)
at webwork.util.InjectionUtils.invoke(InjectionUtils.java:56)
... 2 filtered
at com.atlassian.jira.action.JiraActionSupport.execute(JiraActionSupport.java:63)
... 7 filtered
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
... 49 filtered
at com.atlassian.jira.security.JiraSecurityFilter.lambda$doFilter$0(JiraSecurityFilter.java:66)
... 1 filtered
at com.atlassian.jira.security.JiraSecurityFilter.doFilter(JiraSecurityFilter.java:64)
... 31 filtered
at com.atlassian.jira.servermetrics.CorrelationIdPopulatorFilter.doFilter(CorrelationIdPopulatorFilter.java:30)
... 24 filtered
at com.atlassian.jira.servermetrics.MetricsCollectorFilter.doFilter(MetricsCollectorFilter.java:25)
... 25 filtered
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)
... 175 more
Is there anything I'm not doing right?
Thank you in advance.
Hugo
Mysql database is running on host (non-docker) at port 3306.
While Jira is running inside container. By-default container runs in bridge networking mode, where container network is different from host network. Hence localhost inside container is not the same as on host.
Here you can do two things:
In your Jira container refer to mysql database using private/public-ip:3306
OR
Run Jira docker container in host networking mode, so that localhost inside container is same as that on host. Because in this mode container uses network of host.
jira:
image: 'cptactionhank/atlassian-jira-software:latest'
container_name: jira
restart: unless-stopped
healthcheck:
disable: true
volumes:
- '/var/atlassian/jira:/var/atlassian/jira'
network_mode: "host"
NOTE: network_mode: "host" option will tell docker container to use host networking mode.
Hello and thank you for your answer. The network_mode: "host" didn't work for me because I also use Traefik. But you put me on the right track and this outcome helped me: How to connect locally hosted MySQL database with the docker container
The solution to my problem was to use this command :
docker inspect <container-id-or-name> | grep Gateway
"Gateway": "",
"IPv6Gateway": "",
"Gateway": "172.18.0.1",
"IPv6Gateway": "",
And use 172.18.0.1 instead of "localhost" for the "host" field of my local mySQL database
My solution was to set host.docker.internal instead of localhost in the connection url in the dbconfig.xml file. In the Dockerfile I have the following lines:
EXPOSE 3306
VOLUME ["/var/atlassian/application-data/jira"]
I hope it helps

How to connect spring-boot and mysql using docker?

I tried to make some rest API using spring-boot and MySQL.
And I finished it in my eclipse environment.
The next step is to create a docker image.
However, when I run the MySQL server and my rest API in docker, it gives me a connection refused error.
I found other basic spring-boot and MySQL tutorials. But I can't solve this problem.
This is mysql setting of spring-boot project.
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/users?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = root
#spring.ldap.embedded.base-dn=dc=example,dc=org
#spring.ldap.base=dc=example,dc=org
#spring.ldap.password=admin
#spring.ldap.username=cn=admin,dc=example,dc=org
#spring.ldap.urls=ldap://localhost:389
##Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
#Open session in View
#spring.jpa.open-in-view=true
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate.ddl-auto=update
This is command to run mysql in the docker.
docker urn -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root --name mysql mysql
This is my docker file to make docker image for rest API.
FROM java:8
VOLUME /tmp
EXPOSE 8080
ADD target/userManageWithRest-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
This is error message.
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.mysql.cj.NativeSession.connect(NativeSession.java:152) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
... 56 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_111]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_111]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_111]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_111]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_111]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_111]
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
... 59 common frames omitted
I already tried to modify spring.datasource.url to jdbc:mysql://mysql:3306/users?....
How can I solve this problem?
Please help me.
As your MySQL database and Spring Boot app are running in a separate Docker container, access localhost or 127.0.0.1 within a Docker container isn't referring to the localhost of your host machine.
I would suggest to use docker-compose to link the Spring Boot application to your MySQL container and make it accessible via mysql as hostname as you have already tried it (jdbc:mysql://mysql:3306/users?...):
version: "3"
services:
backend:
build: .
depends_on:
- "mysql"
links:
- mysql
ports:
- "8080:8080"
mysql:
image: mysql:latest
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
Save this in the folder where your Spring Boot Dockerfile is located as docker-compose.yml and the run docker-compose build && docker-compose run.
With the links attribute to give access to your MySQL container via the container name and as they are sharing the same Docker network, the mysql hostname is resolved correctly
Even though this question is a bit old, I believe this explanation might be useful.
The reason for your issue is that you can control the order of service startup and shutdown with the depends_on option. Compose always starts and stops containers in dependency order, where dependencies are determined by depends_on, links...
However, for startup Compose does not wait until a container is “ready” (whatever that means for your particular application) - only until it’s running.
That may cause some issues, e.g. your spring-boot app starts very fast and tries to connect to MySQL, but even though MySQL container has started - MySQL inside is not ready to accept connections yet.
There are some workarounds for this. If you are interested in them, you might want to check this question.

Windows container failed to start with error, "failed to create endpoint on network nat: HNS failed with error : Failed to create endpoint."

I have been trying Windows Containers on windows server 2016 TP5. Suddenly I started getting error while running a container with port maping option -p 80:80
c:\>docker run -it -p 80:80 microsoft/iis cmd
docker: Error response from daemon: failed to create endpoint sharp_brahmagupta on network nat: HNS failed with error : Failed to create endpoint.
I made sure that no other container is running and port 80 on host machine is not being used by any other service.
Did anyone face same issue?
After searching around I stunbled upon this issue on github. This seemed to be a known issue with Windows containers on Windows server TP5.
Then thanks to this forum, I found the solution
You can check active static port mapping with below command
C:\>powershell
PS C:\>Get-NetNatStaticMapping
StaticMappingID : 3
NatName : Hda6caca4-06ec-4251-8a98-1fe0b4c5af88
Protocol : TCP
RemoteExternalIPAddressPrefix : 0.0.0.0/0
ExternalIPAddress : 0.0.0.0
ExternalPort : 80
InternalIPAddress : 172.31.181.4
InternalPort : 80
InternalRoutingDomainId : {00000000-0000-0000-0000-000000000000}
Active : True
From above output it seemed that even though container was removed the static port mapping was not removed and was still active.
But I removed it with below command.
PS C:\> Get-NetNatStaticMapping | ? ExternalPort -eq 80 | Remove-NetNatStaticMapping
Then simply rebooted the system and the error was gone.
For me these steps solved the problem:
Stop-Service docker
Get-ContainerNetwork | Remove-ContainerNetwork
Get-NetNat | Remove-NetNat
Get-VMSwitch | Remove-VMSwitch
Start-Service docker
(suggested by JMesser81 at:https://github.com/Microsoft/Virtualization-Documentation/issues/273)
I had similar error.
$ docker --version
Docker version 1.13.0-rc3, build 4d92237
$ docker-compose -f .\docker-compose.windows.yml up
Starting musicstore_db_1
ERROR: for db Cannot start service db: {"message":"failed to create endpoint musicstore_db_1 on network nat: HNS failed with error : Unspecified error"}
ERROR: Encountered errors while bringing up the project.
Static mapping removal did not work, only network removal helped:
Get-ContainerNetwork -Name nat | Remove-ContainerNetwork
Execute the command in PowerShell as administrator, then restart Docker.
Update:
Use CleanupContainerHostNetworking.ps1 script to resolve Docker 17 networking issues.
.\CleanupContainerHostNetworking.ps1 -Cleanup -ForceDeleteAllSwitches
I had a docker and docker-compose which were already working on Centos.
I did the following changes to make it work on windows server 2016:
Stop the docker service, remove nat, start the docker service.
ps>stop-service docker
ps>Get-ContainerNetwork | Remove-ContainerNetwork -Force -ea SilentlyContinue
ps>start-service docker
Configure network in your docker-compose.yml
version: '3.7'
networks:
default:
external:
name: nat
That's It!