I have a spring boot application that I have got working in a docker container and now I am trying to move some hard-coded values out of my application.properties. I know these questions get asked a lot but after reading many questions and answers I haven’t been able to resolve my errors.
My application.properties has this
spring.config.import=optional:file:.env[.properties]
spring.datasource.url=${SPRING_DATASOURCE_URL}
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
api.key=${API_KEY}
The springboot part of my docker-compose is like this
version: '3.8'
services:
spring-boot:
image: imagename
ports:
- "8090:8090"
- "5005:5005"
environment:
SPRING_DATASOURCE_URL: ${SPRING_DATASOURCE_URL}
SPRING_DATASOURCE_USERNAME: ${SPRING_DATASOURCE_USERNAME}
SPRING_DATASOURCE_PASSWORD: ${SPRING_DATASOURCE_PASSWORD}
API_KEY: ${API_KEY}
depends_on:
- mysql
And then my .env file in the same directory as my docker-compose
SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/db_name
SPRING_DATASOURCE_USERNAME=username
SPRING_DATASOURCE_PASSWORD=password
API_KEY=apikey
When I hardcode the values into the docker-compose the container runs fine. When I run the application locally it runs fine. I had the environment variables as different names and that made no difference.
I am getting a lot of errors but this is at the top
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
2024-07-02 16:01:07
2024-07-02 16:01:07 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
I have tried adding in a env_file option but that doesn’t change anything as the env_file option only passes those variables to the containers and not the Docker Compose file.
I would appreciate any help on this as I am quite stuck, thanks!