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.
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
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.
docker.network create spring-mysql.net
docker run --name mysql -d -e MYSQL_ROOT_PASSWORD=root -v mysql:/var/lib/mysql -.network spring-mysql.net mysql:8
docker build -t restapi.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.