skip to Main Content

I’m deploying a springboot application and I want to use a persistent DB. So, in application.properties file, I have

spring.datasource.url=jdbc:h2:file:/home/ubuntu/db;AUTO_SERVER=TRUE;

Now this works as long as I start this application without using a container. Now, I build a docker image and try to run the application. Dockerfile looks like

FROM maven:3-jdk-11 AS maven
ARG BUILD = target/build.jar
COPY ${BUILD} build.jar
EXPOSE 8080
USER spring:spring
ENTRYPOINT["java","-jar","/build.jar"]

Now this doesn’t work when I try to start it, because it searches for /home/ubuntu/db inside the container, which does not exist. Is there a way to make the app inside the docker container access the host folder /home/ubuntu/db? Thanks for the response.

2

Answers


  1. Chosen as BEST ANSWER

    Just in case it is helpful to anyone else, the full command to be used is:

    docker run -v /home/ubuntu/db:/home/ubuntu/db --privileged -p $HOST_PORT:$CONTAINER_PORT <image-name>
    

  2. The missing part is to tell docker when running the containter to mount /home/ubuntu/db from the host into the container.

    You do that like this:
    docker run -v <folder_on_host>:<folder_in_cointainer>

    with your example:
    docker run -v /home/ubuntu/db:/home/ubuntu/db

    more info on docker docs: https://docs.docker.com/get-started/06_bind_mounts/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search