Connecting to an H2 Database in a Docker Container

I have a basic Spring Boot Data JPA project. The h2 database that I’m connecting to is located at /tmp/customerdb.h2.db. When running the application using mvn spring-boot:run everything works fine. The application connects to the database, adds records, and prints the added records to the console.

I then build a docker container, and run it. The docker file looks like this:

  • Enabling webpack hot-reload in a docker application
  • How can I get an interactive shell inside a Google datalab container?
  • How about the docker run in virtul machine best practice?
  • Passing variable from container start to file
  • How do I expose a docker container port to another container
  • Cannot see the content of the volume from my Mac
  • FROM java:8
    VOLUME /tmp
    ADD jpa-docker-1.0.0.jar  app.jar
    RUN bash -c 'touch /app.jar'
    ENTRYPOINT ["java","","-jar",/app.jar"]

    When I run the container I get the following error:

    2015-06-12 19:25:57.200  WARN 1 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 42102, SQLState: 42S02
    2015-06-12 19:25:57.200 ERROR 1 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Table "CUSTOMER" not found; SQL statement:

    So it looks like the application can’t see the database. The connection URL looks like this:

    As I mentioned, this works fine when running outside the docker container. I’m assuming that the line in the Dockerfile VOLUME /tmp creates the /tmp directory inside the container, along with all the files it contains, such that the database is visible, but this seems like it’s not working. Thoughts?

    – Ole

  • 502 Bad Gateway: Registered endpoint failed to handle the request
  • Upgrading docker without affecting running containers
  • How to use COPY command in Docker build?
  • Error “Cannot connect to the Docker daemon. Is 'docker -d' running on this host?” after upgrade Linux kernel
  • Docker containers- running new java8 code on old java6 version
  • Is it safe to remove Docker containers listed with `docker ps -f status=created`?
  • 2 Solutions collect form web for “Connecting to an H2 Database in a Docker Container”

    You should use docker data volumes. When running your container you specify the parameter:

    -v <host folder>:<container folder> 

    This way, the on the host machine is mapped, inside the container

    for instance:

    docker run -v /tmp:/tmp -d yourcontainer

    Your application inside the container looks for the file /tmp/customerdb.h2.db, which actually is on the host at /tmp/customerdb.h2.db where the db file actually exists (in general you could use different paths on the guest and the host; in your example it just happens that the host and guest folders are both on the same location “/tmp”)

    This is the solution. First I added the database docker build directory containing the Dockerfile. Then I update the Dockerfile with the following line:

    ADD customerdb.h2.db  /tmp/customerdb.h2.db

    The application can now connect to the database inside the container. Note that the database contained in the volume /tmp/ is is confined to the container and different from the database I copied to the /tmp/ directory on my workstation.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.