So I’m working on a Spring Boot application with MySQL. Normally I will load some variables in application.yml
file like this
spring:
datasource:
url: ${DATABASE_URL}
username: ${DATABASE_USERNAME}
password: ${DATABASE_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
show-sql: true
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
The variables will be read from a file named local.env
like this
DATABASE_URL=jdbc:mysql://mysqldb:3306/todolist
DATABASE_USERNAME=root
DATABASE_PASSWORD=root
Basically these are for MySQL Workbench (I changed from localhost
to mysqldb
like the name of MySQL image, since I want to have a MySQL image). And then I have a docker-compose.yml
file (both Dockerfile
and docker-compose.yml
are in the same location of local.env
) like this
services:
todo-list:
image: todo-list:0.0.1
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
depends_on:
mysqldb:
condition: service_healthy
env_file:
- ./local.env
networks:
- todo-list-network
deploy:
restart_policy:
condition: on-failure
max_attempts: 10
mysqldb:
image: mysql:8.0.33
container_name: mysqldb
ports:
- "3307:3306"
env_file:
- ./local.env
environment:
- MYSQL_DATABASE=todolist
- MYSQL_USER=root
- MYSQL_PASSWORD=somerandompassword
- MYSQL_ROOT_PASSWORD=root
volumes:
- ./mysql-data:/var/lib/mysql
networks:
- todo-list-network
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
retries: 10
interval: 3s
timeout: 30s
volumes:
mysql-data:
networks:
todo-list-network:
name: todo-list-network
I tried to build containers using docker-compose up -d
. The mysqldb
was created and ran, meanwhile the other container todo-list-todo-list-1
got this kind of error
java.sql.SQLException: Access denied for user 'root'@'172.18.0.3' (using password: YES)'
I’m not sure if it’s because of my configuration or something else, like local.env
file. I made changes and tried many ways and the container todo-list-todo-list-1
just got errors and turned off. I would appreciate any help, thanks!
trlq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.