0

I'm practicing with docker, I can create a working image and container, but so far I've only tried with apps without database, now I'm trying with one with a mysql database, when I start the container, however, I get connection refused as an error.. .how do I pass db credentials to docker?

these are the commands I entered in the terminal to create the container:

-mvn clean package;
-docker build -t app:0.0.1-SNAPSHOT .;
-docker run -d --name=app-p 8761:8761 app:0.0.1-SNAPSHOT

This is my dockerfile:

FROM openjdk:19-slim
EXPOSE 8761
ADD target/app-0.0.1-SNAPSHOT.jar app-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/app-0.0.1-SNAPSHOT.jar"]

I'm trying to connect to my localhost db

  • https://stackoverflow.com/questions/62072977/whats-default-password-in-docker-container-mysql-server-when-you-dont-set-one Hope it helps! – DreamBold Nov 30 '22 at 18:31

1 Answers1

0

It depends how your Spring Boot application gets credentials.

If using environment variables, set them when running the container. For example:

docker run \
  -d \
  --name=app \
  -p 8761:8761 \
  -e DATABASE_USERNAME=user \
  -e DATABASE_PASSWORD=password \
  app:0.0.1-SNAPSHOT

If using a properties file, mount a volume when running the container. For example:

docker run \
  -d \
  --name=app \
  -p 8761:8761 \
  -v $HOME/path/to/properties:/path \
  app:0.0.1-SNAPSHOT

For Spring Boot apps, you may even need to override the location of the properties file as an option when running the app: --spring.config.location=file:/path/. Read externalized configuration docs for more info.

jcragun
  • 2,060
  • 10
  • 8