windowsdockerdocker-composehostdocker-container

Docker: Accessing a host folder from a container folder


I want to access some files kept on the host from the docker container.

Edit-
My use case:

  1. I have a Spring Boot app (rest-service) that reads a file stored on the local(host) system. It's jar will run in the docker container.
  2. I have an other docker containerized application (QE app) that uses the jar of the spring boot app. So, I have 2 containerized applications - 1. spring boot app and 2. the QE app
  3. I am required to hit a rest service from the QE which I am able to do. But since, the service's jar (running in the container) doesn't have access to the host file, my service is not working properly. That's why I want to access a folder kept on the host from the service running in the docker container.

What I've done:

  1. executed the following command to copy the contents of the host folder to the container's folder docker run -d --volume C:\host_folder:/conf/container/tmp d9746fe0b603 sleep infinity

  2. Upon executing the followng command - docker exec -it cd8ca46e46c6 sh
    I am able to check that contents of the C:\host_folder are copied to the /conf/container/tmp folder in the container. Attached Screenshots.
    But when I am running the application in the container, getting the same error - the service can't find the file stored on the host system.

Attaching a few screenshots- host_folder_contents enter image description here
I tried restarting and rebuilding the container using - docker-compose up --build but that's not working. Can anybody tell me what's the issue and how I can fix that. TIA


Solution

  • Problem with the approach that I have discussed under What I've done in the description:

    1. Able to successfully copy the files from host_folder to the container_folder.
    2. But as soon as, I was building the containers again - docker-compose up --build, the updates were getting lost. (Because the commands were run on a console, may be!)

    Solution that finally worked for the discussed case -
    In the docker-compose file, inside the image, under the volumes tag mention the folders that you want to mount, like given below:

    application-mage:
      .
      .
      volumes:
        - C:/host_folder/:/conf/container_folder/tmp/