Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Union mounts into Docker containers

Tags:

docker

unionfs

If I have a directory d1/, I know I can mount it at /mountPoint inside a Docker container by doing this:

docker run -v /path/to/d1:/mountPoint ...

However, I have two directories d1/ and d2/ (let's say they contain files a.txt and b.txt respectively).

I want to mount the union of these two directories at /mountPoint in my container, i.e. I want /mountPoint/a.txt and /mountPoint/b.txt to exist.

Given that Docker uses UnionFS a lot internally, I am rather hoping there are options to do a union mount at a specific path inside a container, but I can't find them if so.

like image 468
David North Avatar asked Mar 09 '16 12:03

David North


People also ask

What is union mount in Docker?

AUFS is a union filesystem, which means that it layers multiple directories on a single Linux host and presents them as a single directory. These directories are called branches in AUFS terminology, and layers in Docker terminology. The unification process is referred to as a union mount.

Is Docker a union file system?

Docker uses file systems inspired by Unionfs, such as Aufs, to layer Docker images. As actions are done to a base image, layers are created and documented, such that each layer fully describes how to recreate an action.

Can two Docker containers mount same volume?

For some development applications, the container needs to write into the bind mount so that changes are propagated back to the Docker host. At other times, the container only needs read access to the data. Multiple containers can mount the same volume.


1 Answers

One (probably obvious) workaround would be to install unionfs within the container and then unify all required partitions together with it:

docker run -v /path/to/d1:/mnt/d1 -v /path/to/d2:/mnt/d2
# and within docker container:
mkdir -p /mnt/joined
unionfs /mnt/d1=RO:/mnt/d2=RW /mnt/joined

But it is ugly and I hope there is any better option.

like image 105
MarSoft Avatar answered Oct 07 '22 15:10

MarSoft