0

I have a set of directories that I would like to overlay mount into a container:

# On host
/opt/a
  - bin
  - lib
/opt/b
  - bin
  - lib
/opt/c
  - bin
  - lib

# In container
/usr/local
  - bin
  - lib
  - cuda

I am able to create an overlayfs mount on the host machine for /opt/{a,b,c} and mount that as a volume to /usr/local in the container, but then /usr/local/cuda will be inaccessible in the container.

I'm able to achieve this directly with systemd-nspawn with the following:

systemd-nspawn --overlay /opt/a:/opt/b:/opt/c:/usr/local <other flags>

This makes all the files available as a merged mount in /usr/local in the container, with changes written to /opt/c on the host.

Is it possible to easily achieve what I want with docker?

1 Answers1

0

No. If you're familiar enough with the host tools to attempt this, consider running it in a chroot environment instead.

Docker has no way to merge image content, volume content, and host-system directories. Mounts work the same way as the normal Unix mount(8) command: the thing you're mounting completely hides whatever was there before and you only see the mounted contents.

It's also unusual to run a container that heavily depends on host-system content. Typical practice is for an image to include all of the application and library code that's needed to run the software; that way you can run it on a different system even if that software isn't present on the host. That is, it's more typical to install the software you need directly in your Dockerfile, with something like apt-get install for Debian/Ubuntu-based images. If the various /opt things are things you'd build from source, you can use a multi-stage build to install them from source, and then in the final phase COPY --from=opt-a-build into the /usr/local tree.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Thanks for your reply. What I want to achieve can be done with the unix **mount** command on recent linux kernels with `mount -t overlay overlay -o lowerdir=/opt/a:/opt/b:/opt/c,upperdir=/usr/local,workdir=/opt/workdir /usr/local` on the host. I want to do this but use the `/usr/local` from the docker container instead. – Sebastien Collier Oct 06 '20 at 12:01
  • The container is building software from source for me in a clean environment. `/opt/a` etc represent dependencies of the software that I'm trying to build (packages and versions that are not available in APT repos, that I'm building myself to have fine control over versions and configurations. – Sebastien Collier Oct 06 '20 at 12:03
  • You should install it in your Dockerfile so it's contained in the image; don't try to inject it from the host. – David Maze Oct 06 '20 at 12:07
  • (If you can spell out the `mount` command in that much detail then you might be able to `docker volume create` a volume with those exact options, but it's not how I'd recommend setting it up.) – David Maze Oct 06 '20 at 12:08