I have created multiple Spring boot apps with a database in Docker and it’s never been a problem. I can run tests, and run the app and everything works fine. But, I created a new app in IntelliJ with the Spring Boot starter and I have added a bunch of things, probably which I shouldn’t have.
I have Docker running, I have a MySQL 8 Running in Docker with an existing database. That container is networked so the other containers can see it. The Port 3306 is exposed to outside the container, so that way my MySQL Workbench and DBeaver can connect and access this running database with data already in it. I can also run mvn clean install and all my tests which access the database run perfectly, no issues there. When I start the application is where the trouble begins. The app starts perfectly, and when I go to access the endpoint, that also works fine, but if I access the endpoint which gets a list of records from the database, that’s when it tells me that the table doesn’t exist.
Here is what I have:
Dockerfile:
FROM eclipse-temurin:17-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ./target/my-app.jar my-app.jar
ENTRYPOINT ["java","-jar","/my-app.jar"]
docker-compose.yml:
services:
mysql:
image: 'mysql:latest'
environment:
- 'MYSQL_DATABASE=my_app_db'
- 'MYSQL_PASSWORD=password'
- 'MYSQL_ROOT_PASSWORD=password'
- 'MYSQL_USER=my_app_user'
ports:
- '3306'
application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/my_app_db?serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=my_app_user
spring.datasource.password=password
spring.jpa.show-sql=true
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none <==== very important here!!!!!
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.highlight_sql=true
I can mvn clean install and the app builds 100% fine. I can run the app, and the app runs fine.
When I try to access the endpoint which I know is there, then it gets to the repository code, and then it says the database table does not exist: my_app_db.company table does not exist.
I am not initializing the database because it exists and the data is already there. Or, do I need to change the spring.jpa.hibernate.ddl-auto to something else and then bring in the Flyway schema.sql which will have the table creation and data insertion into it?
The idea is, is that this is a database and app in development, and not ready for GitHub Actions yet to deploy to ECS. I want to be able to run this app and test out the UI multiple times, like I have done many times in the past.
spring.jpa.hibernate.ddl-auto=none