Volume overwrites the directory inside the docker container

Hi,

I am learning Docker,

I have successfully created the build file,

Here is the scenario, I have copied few files to media folder which is in /app/ while building the image, and I need to access this media folder outside the docker also. So, I used the volumes.

So, while building the image, the structure of the files inside the docker looks like this

---/app
    ---media
        ---a.png
        ---c.png

I have the media folder in the host system also, there I have some other files which requires for the app when starting the containers, I will change those specific files, every time I run the container.

The media folder in the host system contains this files

---media
    ---b.png
    ---d.png

When I start the container, I use this command to create the volume -

sudo docker run -it --name test1 /home/qwerty/media:/app/media test1:v1

What is happened after running this command is, the files which are in the media folder inside the docker gets deleted [a.png and c.png] and, the files which are saved in the host system gets saved inside the docker.

I am looking for syncing of all the files[a.png, b.png, c.png, d.png] inside the docker instead of deletion of [a.png and c.png]. How to achieve this ?

I do not have the option to put those files in separate folders, to avoid this conflict

Thanks

Docker does not delete files when you bind a folder on top of another folder, it simply eclipses the original folder. As a result no content of the original container folder will visible, just the content of the folder you bind on top of it.

You could use named volumes, which have a copy-on-first use mechanism, which copies the files from the container back into an emtpy(!) named volume, before it binds it to its target location. Though, it will do it only if the named volume was empty - so it will happen exactly once. If you create a new image with new files, those files will never be reflected back to the named volume, because it is not empty.

So neither way, docker doesn’t seem the right solution for your problem.