Search code examples
dockerapachebindingdockerfiledocker-volume

How to create a bidirectional bind volume in Docker?


I would like to have a bidirectional volume in container. I will show a basic example:

$ docker container run -d -p 80:80 -v $HOME/htdocs:/var/www/html httpd

Imagine if I extend that image and copy a content like index.html in the /var/www/html folder at the build process, but when I bind a empty folder $HOME/htdocs, the content of /var/www/html gets overwrited.

Is there a way to avoid that default behaviour of overwriting of the destination of bind volume? I expect to be bidirectional, the content of /var/www/html should be copied to $HOME/htdocs.


Solution

  • The default is that if you mount a host volume the container volume gets overwritten.

    If you mount a new docker-volume the contents of the container version gets copied to the docker-volume.

    If you want to have a different behavior you have to build that mechanism yourself, which is very possible, but custom