Overlay a folder in docker by one from host

560 Views Asked by At

My situation is the following:

I am having a docker image/container in which I am compiling. I had to install some components to $HOME via the Dockerfile (so while creating the image). Let's say one of those components is in ~/.config, but also other folders.

I would like to have the possibility to override the files in .config by mounting a home folder from the host on top of the one inside docker. Whenever you place a file in the mounted folder, it overrides the one which is already inside the container.

So in theory, this is exactly what an OverlayFS does, right? While the lower directory would be the one inside the Docker container, the upper directory would be the one on my Host.

Is there a way to accomplish that?

Until now I found the following related topics:

  1. https://serverfault.com/questions/841238/how-to-use-overlayfs-with-docker-volumes

    Drawback: The answer does only show how to use overlayfs on the host, but getting acccess to the lower container/image directory is not that self-explaining and also feels dirty.

  2. Can I mount docker host directory as copy on write/overlay?

    Drawback: Using mount -t overlay inside docker does not work on newer kernels because of the disabled overlay-on/over-overlay option

I also thought about manipulating the docker files on host directly, i.e. the directories where docker stores the files, but that feels a bit dirty.

To do so, I would declare VOLUME /home/user at the end of the Dockerfile. Then I would find my files of that directory in /var/lib/docker/volumes/user/_data. I could then create a overlayfs on my host, using that directory as lower, my other folder as upper. I could then remount that new directory using docker run --volume. Unfortunately this would involve su rights to access the /var/lib directory.

The other way around would be to bind-mount single files, but that's maybe a bit hackish too.

0

There are 0 best solutions below