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
Related
I cannot get my spring-boot server to run against my database using docker.
If I start up my mysql database (called shape-shop-db-container) and intialize the database like so :
docker run -d -p 3306:3306 --name=shape-shop-db-container --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=shapeshop" mysql
docker exec -i shape-shop-db-container mysql -uroot -proot shapeshop < SCHEMA.sql
docker exec -i shape-shop-db-container mysql -uroot -proot shapeshop < TEST_DATA.sql
and then run my application server within my IDE with the following application.properties :
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/shapeshop?useSSL=false&allowPublicKeyRetrieval=true&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
server.port=8080
everything works fine.
Now
.... instead of running my application server through my IDE, I instead have it running on a docker container, and I try the following :
(1) create network 'shape-shop-network'
docker network create shape-shop-network
(2) run db container as before, but this time specifying the network as well.
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
docker exec -i shape-shop-db-container mysql -uroot -proot shapeshop < SCHEMA.sql
docker exec -i shape-shop-db-container mysql -uroot -proot shapeshop < TEST_DATA.sql
(3) now i build my app server and run it on a container called 'shape-shop-server'.
docker build -t shapeshop:1.0 .
docker run --name shape-shop-server -p 8080:8080 shapeshop:1.0 --name shape-shop-server --network shape-shop-network
But when I run it in the container, I get "unable to acquire JDBC Connection" error.
Caused by: org.springframework.dao.DataAccessResourceFailureException: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
2022-11-03T11:34:10.043594500Z at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:275) ~[spring-orm-5.1.8.RELEASE.jar:5.1.8.RELEASE]
2022-11-03T11:34:10.043602700Z at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253) ~[spring-orm-5.1.8.RELEASE.jar:5.1.8.RELEASE]
2022-11-03T11:34:10.043610900Z
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]
Why is this happening? Is something 'off' with my ports? Why would it work in my IDE but not as a container? I would assume that the settings are the same.
UPDATE:
I tried to rename "localhost" in my application.properties to shape-shop-db-conatiner. Eg :
spring.datasource.url=jdbc:mysql://shape-shop-db-container:3306/shapeshop?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&useLegacyDatetimeCode=false
But I get "Caused by: java.net.UnknownHostException: shape-shop-db-container" , even though shape-shop-db-container is running.
When you want to establish a connection with another Docker container within a network, its hostname should be used instead of its name. By default, the container’s hostname is set to the container’s ID, which can be overridden using the --hostname flag with docker run command.
Another option would be to use the --network-alias flag, also with docker run command, to specify network-scoped alias for a container, which can be used as an alternative to a hostname. Likewise, the alias can be specified with the --alias flag when connecting to an existing network using docker network connect command.
Consider using docker-compose where you don't have to worry about network and hostnames.
For reference: Container Networking and Docker run reference
The hostname for the docker container is not the same as "name".
You should apply the --hostname parameter when starting the docker container to specificy the hostname it would be given.
You can then use that to connect to it from another container in the same network.
try change jdbc:mysql://shape-shop-db-container:3306 to jdbc:mysql://host_ip_address:3306
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.
I have MYSQL container Running i am able to execute it and able to connect from host machine using "mysql -h 172.17.0.2 -u root -p". I have executed "docker run --name=mysql-host -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.32" to run the docker image.
but when i trying to access through Spring boot app it`s giving connection refused below are the properties in the application.properties
spring.datasource.url=jdbc:mysql://172.17.0.2:3306/auditLog?createDatabaseIfNotExist=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
i am runnung my jar as given below
java -jar service-1.0.0.jar spring.config.location=file:///home/10662598/Downloads/entity-search-service-deploymentFolder/entity-search-service/configs/application.properties
Error getting Caused by: java.net.ConnectException: Connection refused (Connection refused)
Your issue is probably because the Spring boot app can't reach the database (mysql), the reason for this is probably because they're not in same network.
If your Spring boot app is running inside container
From your question, I see that you're running containers using docker run..., therefore when you start MySQL and Spring boot app using that command, they're not in same network.
Solution for this is, first create a network, then start MySQL and Spring app to run within that network:
docker network create my-net
Then start your containers as usual but remember to add your containers to the network you just created:
docker run ... --network my-net
Now your containers are in same network and should reach each other, you can verify this by exec to Spring boot app and try curl to Mysql container using its container name (and port):
curl <mysql_container_name>:3306
If Spring boot app is running on host machine
Then simply make sure you map Mysql port to host machine and connect using localhost:<mapped_port>
Good luck :)
Here, you must create a network layer between the containers.
First create network layer
docker network create spring-mysql-net
Build & Run MySql Docker image
docker run --name mysql -d -e MYSQL_ROOT_PASSWORD=root -v
mysql:/var/lib/mysql --network spring-mysql-net mysql:8
Build your spring boot or any API
docker build -t restapi .
Start container on that created the network
docker container run --network spring-mysql-net --name
mysql-restapi-container -p 8080:8080 -d restapi
a bit late but i think you need to forward port 3306 on local machine to the contain port 3306
try this
docker run --name=mysql-host -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.32
I deploy my war file in tomcat 7 successfully and start tomcat using following command
docker run -it --rm -p 7008:8080 -v //d/docker_tomcat/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml:ro -v //d/docker_tomcat/webapps:/usr/local/tomcat/webapps:rw tomcat:7.0
when tomcat start it shows following error logs:-
AbandonedObjectPool is used (org.apache.commons.dbcp.AbandonedObjectPool#9030ca2)
LogAbandoned: true
RemoveAbandoned: true
RemoveAbandonedTimeout: 90
[localhost-startStop-1] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot create PoolableConnectionFactory
i think that above error means that database studentdb is not accessible
here is my hibernate.properties file:-
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.connection.url=jdbc:mysql://10.0.75.x<ip of docker>:3306/studentdb?autoreconnect=true&zeroDateTimeBehavior=convertToNull&jdbcCompliantTruncation=false
show_sql=false
hibernate.jdbc.use_streams_for_binary=false
hibernate.dbcp.testOnBorrow=true
hibernate.dbcp.validationQuery=SELECT 1 FROM DUAL
hibernate.dbcp.testOnReturn=false
hibernate.dbcp.maxWait=2000
hibernate.dbcp.testWhileIdle=true
hibernate.dbcp.minEvictableIdleTimeMillis=1800000
hibernate.dbcp.timeBetweenEvictionRunsMillis=300000
hibernate.dbcp.numTestsPerEvictionRun=5
hibernate.dbcp.removeAbandoned=true
hibernate.dbcp.removeAbandonedTimeout=90
hibernate.dbcp.logAbandoned=true
i think there may be error in hibernate.connection.url property of
hibernate.properties file.
and also doubt is it becuase my windows 10 uses port 3306 for mysql as well as docker also uses port 3306 for mysql. if is it problem then how can i change port of mysql container running in docker with some different port
Use following command to forward your local port to docker container port
docker run -p <LOCAL-PORT>:3306 <mysql-image-name>
I'm trying to learn Docker and I created a container which is running MySQL server. It works fine and I can use MySQL from my Spring Boot application, when I run Spring Boot application locally (without Docker). But when I try to run Spring Boot application inside another Docker container, connection to MySQL fails and I get error: java.net.ConnectException: Connection refused
In my Spring Boot application.properties I have this configuration:
spring.datasource.url: jdbc:mysql://127.0.0.1/mydb
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.jdbc.Driver
Any ideas what might be wrong?
When you want to use the MySQL container from your Spring Boot container a good idea would be to link to it like:
docker run ... --name spring-boot --link mysql ...
Assuming that mysql is the name of your MySQL container you could then use the following JDBC URL in your configuration:
spring.datasource.url: jdbc:mysql://mysql/mydb
The best way to handle this situation (which I used) would be to run your MySQL container in detached mode. Obviously, you can name your container as you like:
docker run --detach --name = my-MySQL --env = "MYSQL_ROOT_PASSWORD=your_password_here" mysql
Your container will be running in detached mode, now you can check the IP and the port on which its running using the inspect command :
docker inspect docker_mysql
And you can check the logs using:-
docker logs my-MySQL
Furthur you can use the IP you get after inspecting. My MySQL was running on 172.17.0.2 and 3306 port the default one:
spring.datasource.url=jdbc:mysql://172.17.0.2:3306/mydb
You can also connect to the MySQL server using any client. I generally use the mysql-client:
sudo mysql -uroot -pyour_password_here -h 172.17.0.2 -P 3306