Search code examples
dockernode-red

when mounting volume, directory is empty in docker


when I run nodered with

docker run -v D:/mydir:/data

the content of /data is copied in my volume at first run, thats what I've expected.

If I make

docker run -v D:/mydir:/usr/src/node-red/node_modules nodered

Then the volume is empty

I was expecting to get the content of node_modules being copied in the volume at start time... what am I missing ?


I can illustrate that a little bit more :

docker run --rm -v d:/VM:/data nodered/node-red-docker ls /data

--> list files

docker run --rm  ls /usr/src/node-red/node_modules

--> list content of node_modules

docker run --rm -v d:/VM:/usr/src/node-red/node_modules nodered/node-red-docker ls /usr/src/node-red/node_modules

--> is empty !


Solution

  • You're mounting host directories as volumes, so there isn't any copying going on - the mount path inside the container is being mapped to the path on the host, so you're seeing the contents of the host directory.

    Volumes sit outside the Union File System when you mount them, so you don't get an overlay which merges the contents of the image and the contents of the host directory. Instead you're effectively bypassing the contents of the image for that volume, and repointing it to your host.

    Samples:

    touch /docker/nodered-modules/sample.txt                                           
    docker run --rm -v /docker/nodered-modules:/usr/src/node-red/node_modules nodered/node-red-docker ls /usr/src/node-red/node_modules                                         
    sample.txt
    
    touch /docker/nodered-data/sample.txt                                                                                                         
    docker run --rm -v /docker/nodered-data:/data nodered/node-red-docker ls /data       
    sample.txt 
    

    The reason you're seeing a difference is because the /data volume is defined in the Dockerfile and empty in the image, so you see the contents of your host directory as expected. The modules directory isn't empty in the image, but you're repointing it to an empty directory on your host.