1

I have a spring application using H2 persistent DB with a file located on my machine.

If I run the application outside of the docker container the DB is not empty. If I run the application in docker the database becomes empty. I read this post: Connecting to an H2 Database in a Docker Container and try to add volumes. From what I understand, the docker can persist a local DB file in a container with volumes. But seems like I haven't made it work because the DB is still empty. Can anyone help me?

application.properties

spring.datasource.url=jdbc:h2:./TESTDB/test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto= update

spring.h2.console.enabled=true
spring.h2.console.path=/h2-ui
spring.h2.console.settings.web-allow-others=true

Dockerfile

FROM azul/zulu-openjdk-alpine:11

ARG JAR_FILE=target/*.jar

COPY ${JAR_FILE} app.jar

EXPOSE 8080

ENTRYPOINT ["java","-jar","/app.jar"]

I run the container like this:

docker run -v ./TESTDB:/TESTDB -p 8080:8080 test-container
toni
  • 25
  • 6

1 Answers1

0

Isn't -v ./TESTDB:/TESTDB an illegal Docker mount specification? Normally, you get this:

docker: Error response from daemon: create ./TESTDB: "./TESTDB" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
See 'docker run --help'.

Try:

docker run -v $(pwd)/TESTDB:TESTDB -p 8080:8080 test-container

(Assuming you're on an OS like Linux or macOS)

Eric Schoen
  • 668
  • 9
  • 16