How to connect spring-boot and mysql using docker? - mysql

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.

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

AWS EC2, Jenkins, Docker, Spring-boot+mysql I suspect my spring.datasource.url is wrong?

I'm very new to Docker and I've tried everything I can think of and have gotten desperate. I really hope one of you will know what might be happening. I'm working on a project where I've created a CI pipeline using Jenkins and Docker.
I've tried many things with my spring.datasource.url.
using the name 'db' as that is what the service is named in docker-compose.yml
using grep to find the ip address of my docker0 service, my docker container, etc.
trying localhost as it is
trying different environment variables in the docker-compose file. (using username and password, not using it.)
trying environment variables. I can't recall the exact format, but it's something like this: jdbc:mysql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}?createDatabaseIfNotExist=true&useSSL=false
trying to install mysql on the EC2 machine.
Many many other things (30 tries tonight, about 60 last night). If it's on stackoverflow, I probably tried it.
Being such a noob, I'm super likely to just not know something very basic. Honestly I'm about to give up and just try to run it with an H2 database. I would hope that would work easier, but I'm still holding a sliver of hope that this may help.
My project works on my local machine using localhost in my spring.datasource.url.
localhost success on mysql
On EC2 however, I get a variation of 'refused to connect' error.
docker sad times
In spring-boot I get similar errors to these:
if I try a hard coded name in the connection string for the host:
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(Unknown Source) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:na]
at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:na]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
at com.mysql.cj.NativeSession.connect(NativeSession.java:120) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:948) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
... 57 common frames omitted
Caused by: java.net.UnknownHostException: db
at java.base/java.net.InetAddress$CachedAddresses.get(Unknown Source) ~[na:na]
at java.base/java.net.InetAddress.getAllByName0(Unknown Source) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(Unknown Source) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(Unknown Source) ~[na:na]
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:133) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
... 60 common frames omitted
The closest I got was using an IP address in the host slot similar to this:
jdbc:mysql://172.17.0.0:3306/foodboxdb?createDatabaseIfNotExist=true&useSSL=false
I tried 172.17.0.0, 172.18.0.2,172.18.0.3 based on what little I knew to look for:
using docker inspect
route on the ec2 root
This approach would timeout, so again, did not work - but didn't give a blatant failure error like the others where it would say the password was wrong, or the host was wrong.
I did find a comment saying that someone changed their RUN command somehow like this:
java -Dspring.profiles.active=dockerembbed,oauth-security -jar myapp.jar
but my command is formatted super differently and I'm not 100% sure how I can try to put that command into my Dockerfile. This is what I have right now:
ENTRYPOINT ["java","-jar","foodbox-service-rest-0.0.1-SNAPSHOT.jar"]
If anyone knows what this person was doing with the -Dspring.profiles.active=dockerembbed command and the proper way to put it into an ENTRYPOINT command, I'd love to see it.
Anyway, here is my code below. I'm pretty sure this is all the relevant stuff. My github repo is here: https://github.com/samparsons/pg6-backend
Thank you in advance - I really am not sure what else to try at this point!
My application.properties file looks like this:
# Application Properties
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/foodoxdb?createDatabaseIfNotExist=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.defer-datasource-initialization=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.database=mysql
spring.jpa.show-sql=true
My Jenkinsfile Looks like this:
pipeline {
agent any
triggers {
pollSCM('* * * * *')
}
stages {
stage('Docker compose build') {
steps {
echo '----------------- This is a docker-compose phase ----------'
sh 'docker-compose up -d --force-recreate --remove-orphans --build'
}
}
}
}
My docker-compose.yml file looks like this:
version: '3.8'
services:
db:
image: mysql:8
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_DATABASE=foodboxdb
- MYSQL_USER=root
- MYSQL_PASSWORD=pw
restart: always
ports:
- 3306:3306
volumes:
- foodboxdb:/var/lib/mysql
foodbox-service-rest:
container_name: foodbox-service-rest
restart: on-failure
image: foodbox-service-rest
build:
context: ./
dockerfile: Dockerfile
depends_on:
- db
ports:
- 8081:8081
environment:
- DATABASE_HOST=db
- DATABASE_USER=root
- DATABASE_PASSWORD=pw
- DATABASE_NAME=foodboxdb
- DATABASE_PORT=3306
volumes:
foodboxdb:
and lastly my Dockerfile looks like this:
# code below is from medium tutorial by Wynn Teo,
https://medium.com/geekculture/dockerizing-a-spring-boot-application-with-maven-122286e9f582
# it has been modified slightly for my use case.
# AS <NAME> to name this stage as maven
FROM maven:3.6.3 AS maven
RUN echo "jenkins ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
EXPOSE 8081
EXPOSE 3306
WORKDIR /usr/src/app
COPY . /usr/src/app
# Compile and package the application to an executable JAR
RUN mvn package -DskipTests
RUN echo "running in root"
RUN ls
RUN echo "running in /usr/src/app"
RUN ls /usr/src/app
RUN echo "running in /usr/src/app/target"
RUN ls /usr/src/app/target
# For Java 11,
FROM adoptopenjdk/openjdk11:alpine-jre
ARG JAR_FILE=foodbox-service-rest-0.0.1-SNAPSHOT.jar
WORKDIR /opt/app
# Copy the foodbox-service-rest-0.0.1-SNAPSHOT.jar from the maven stage to the /opt/app directory of the current stage.
COPY --from=maven /usr/src/app/target/${JAR_FILE} /opt/app/
ENTRYPOINT ["java","-jar","foodbox-service-rest-0.0.1-SNAPSHOT.jar"]
I got it to work. Here is what I did to figure this out.
Made sure docker-compose ran the service that I wanted to run. If you define the service that needs to run, it will create all the dependencies first. so, I changed my command from
docker-compose up -d --force-recreate --remove-orphans --build
to
docker-compose up -d --force-recreate --remove-orphans --build <PUT_SERVICE_NAME_HERE>
Here is the blurb I read on the docker-compose help docs that gave me this hint: https://docs.docker.com/compose/compose-file/compose-file-v2/#depends_on
Once I did this, I noticed while running docker ps -a to observe the status of my containers that I had a new issue - my database kept restarting. Obviously the service won’t work if the DB is down. So, to troubleshoot I found that you can grab container logs in Docker using the following command: docker logs -f <db_container_name>
using this command, I was able to figure out how to get the ‘just make it work’ setup going for my db. Please note - do not copy my environment setup, it’s terrible! I really just need it to work so I can get my project done. As this is for school, they’re not grading on security or even best practices honestly (this program is questionable at best!).
Finally, once the DB was working, I switched back to the format shown here
for my spring.datasource.url: jdbc:mysql://db:3306/foodboxdb?createDatabaseIfNotExist=true&useSSL=false using db as the database name, which is the name of the db service that I initiated in the docker-compose.yml file.
Feel free to reuse my repo with the caveats stated above.

Cannot connect Spring App docker container to MySQL DB docker container

I am having difficulty setting up two containers. One MySQL instance, and another a Spring Application (built with Maven) which wants to talk to the MySQL instance.
I am not sure if there is something wrong with how the ports are matching up, but I have created a network "shape-shop-network", on which both the application "shapeshop" and the database "shape-shop-db-container" should talk to each other through.
Both my "docker run" specify the correct network.
These are the steps I have taken :
create a network "shape-shop-network" for both containers.
docker network create shape-shop-network
Run MySQL container setting port to 3306, making sure to specify the "shape-shop-network"
docker run -d -p 3306:3306 --name=shape-shop-db-container --network shape-shop-network --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=shapeshop" mysql
...followed by adding some schema and initial data :
docker exec -i shape-shop-db-container mysql -uroot -proot shapeshop < SCHEMA_AND_INIT_DATA.sql
Specify my application properties in my Spring application
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/shapeshop?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.platform=mysql
spring.datasource.initialization-mode=always
Do a Maven Package (to create new jar files which are put in target)
Do a Docker Build. My Docker file looks like this :
FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE 8080
The command I run is
docker build -t shapeshop:1.0 .
Then I run my image like so (on port 8080):
docker run -p 8080:8080 shapeshop:1.0 --network shape-shop-network
The exception i get is :
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_212]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_212]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_212]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_212]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.NativeSession.connect(NativeSession.java:152) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
... 103 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.16.jar!/:8.0.16]
Strangely I can run my application through my IDE (IntelliJ) where it connects correctly to the container.
My IDE settings look like this :

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

Docker Container not running for spring boot application

I am new to docker. I am trying to run my application using docker. However, it throws an error for me when I try to run exec -it /bin/bash command on the container
error:
Error response from daemon: Container is not running
Following is my docker file (Dockerfile):
FROM openjdk:8-jdk-alpine
EXPOSE 8080
VOLUME /tmp
ADD /target/entertainment-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
The steps followed:
docker build -t entertainment-service .
docker images, got the image id
docker run command : docker run -d imageId
Use postman to hit localhost:8080/entertainment - Could not get response, error connecting
docker exec -it container-id /bin/bash
Error response from daemon: Container is not running
Any idea whats wrong with the Dockerfile?
1st UPDATE:
Update to the docker file
FROM java:alphine
EXPOSE 8198 - same port used in qa32 properties file
VOLUME /tmp
WORKDIR /srv
ADD /target/entertainment-0.0.1-SNAPSHOT.jar /srv/
CMD java -jar entertainment-0.0.1-SNAPSHOT.jar
I am getting the same issue. When I try to run the jar from target using manual java command it works. Not sure what is the issue here.
2nd Update:
I am getting hibernate error while running it
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:254) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
... 41 common frames omitted
And up the stack I got
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) ~[na:1.8.0_151]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_151]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_151]
Remove / after the add command, like this
ADD target/entertainment-0.0.1-SNAPSHOT.jar app.jar
Path is taken as relative to docker file so / might be causing issue.
Also docker run command needs changes to map the ports between host and container.
Also make sure you have port forwarding rule created if using virtual box.
For the second issue check this question
If you trying to learn docker checkout this repo.
Looks like the app is unable to contact to db from docker container. If you are using external db like mysql, you need to establish link between container and external mysql.
One way to do that is to put mysql container and app container in the same network.
Create a network - docker network create sample-network
Create mysql container - docker create --network sample-network
--name mysql-container -e MYSQL_ROOT_PASSWORD=root mysql --max-connections=300 --max-connect-errors=10000
Create app container - docker create --network sample-network --name your_app_name app_image_name_here
Start mysql and your app using - docker start mysql-container your_app_name