Search code examples
dockercontainersdocker-volumepersistent-storage

Is there a way to use bind mount, in docker, for a single file only


I was trying to run a simple docker container with bind mount, so the application can read and modify data.json file (from host machine). I placed data.json in /home/usr/project and ran the container with

docker container run -it -v /home/usr/project:/app container_name main.exe

project contains 3 files, rest of the 2 files were included in container build. When I try to run the container, it gives the error about the other 2 files being not found. Placing those files in /home/usr/project on local host solves the issue. Since, I want the container to only look for data.json, is there any way I can do it without keeping other 2 files unnecessarily in the bind mount directory


Solution

  • You can map individual files in docker

    docker run -it -v /home/usr/project/data.json:/app/data.json alpine cat /app/data.json

    And you can even make them readonly inside the container to avoid unwanted modifications

    docker run -it -v /home/usr/project/data.json:/app/data.json:ro alpine cat /app/data.json