0

I got a microsservice and a local mysql database. I´m trying to run my miccroservice from a container but it can´t connect to database

I´ve created this dockerfile

FROM openjdk:14-jdk-alpine
COPY target/docker-forum-0.0.1-SNAPSHOT.jar forum-0.0.1.jar
ENTRYPOINT ["java","-jar","/forum-0.0.1.jar"]

and my properties I tried two different approaches but none worked

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.url=jdbc:mysql://localhost/fj27_spring? createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&useTimezone=true&serverTiezone=UTC
spring.datasource.url=jdbc:mysql:172.17.0.1:3306/fj27_spring?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&useTimezone=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

From VsCode terminal, I did

docker build --tag=forum:latest .  

Then

docker run -p8080:8080 forum:latest

The result was

com.mysql.cj.jdbc.exceptions.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.
  • What OS is your host machine? – Hans Kilian Apr 22 '22 at 13:15
  • Where is the database running? Consider using a tool like Docker Compose to run your microservice and its local database together (and read [Networking in Compose](https://docs.docker.com/compose/networking/) in the Docker documentation to learn how to connect to it). If the database is outside a container on the host, [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) describes this setup. – David Maze Apr 22 '22 at 13:26

0 Answers0