1

So I have this Java Springboot MySql web project that works perfectly when run locally. I am trying to dockerize it creating a Springboot image of it, then use docker-compose to run it with another mysql image.

As Spring says, I used this command to generate the image: mvnw spring-boot:build-image It got built perfectly, but when I try to run a container with it, I get communication error with the database.

I'm guessing I'm not writting the database info correctly in my .properties file, but I've been trying different configs and nothing changes... this is what I got in my local project:

# ---
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.globally_quoted_identifiers=true

# ---
server.port=8081

## MySQL
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.datasource.url=jdbc:mysql://localhost:3306/bookshelter
spring.datasource.username=user
spring.datasource.password=1234

Am I forced to use a MySql Docker container when running a Docker Spring project? Can't I use my local database with a Dockerized project? What should I change in my .properties?

Nono-Man
  • 37
  • 1
  • 7
  • In Docker `localhost` typically means the container itself, not the containing host. If you're using Docker Desktop on MacOS or Windows you can use a special hostname `host.docker.internal` instead of `localhost` here; the linked question describes alternatives for other environments as well. – David Maze Apr 07 '22 at 12:59
  • I don't think the referred question was a match.. Rather have a look at inter-container communication instead of how to connect to localhost of the machine... create a docker network, add both containers into the same docker network, then they will reach each other by container name. E.g `docker network create myNetwork` `docker run --network=myNetwork --name=containerA myimage` `docker run --network=myNetwork --name=containerB myimage` Now containerA can communicate with containerB using its name. E:g. jdbc:mysql://containerB:3306/bookshelter – tbjorch Apr 07 '22 at 13:04
  • If you are using docker-compose you can overwrite the application property for your jdbc url with `environment: - SPRING_DATASOURCE_URL=` in the compose file for your application and then replace localhost:port with the name you have given your mysql container – Johannes Apr 07 '22 at 15:48

0 Answers0