5

So this is not about seeking workarounds to -v.

I have a Dockerfile whose intent is to install a cross-compiler in /usr/local/<cross-compiler-path>, inside the container. Later during a build process, a file would be mounted to this cross-compiler, like this:

root@5bee5daf8165:/# mount <blah.img.gz> /usr/local/<cross-compiler-path>

I get mount: /usr/local/<cross-compiler-path>: mount failed: Operation not permitted.

Although if I skip this step, finish build, run a --privileged container and mount, it works fine.

I understand the reason for not giving privileged mode in the build since it breaks the 'portability' of containers as they depend on host volumes. But in my case, I am attempting to mount it inside the Container's own file system. Why is that not allowed?

For the record, I tried installing the cross-compiler on a different path, like this:

root@5bee5daf8165:/# mount <blah.img.gz> /home/<cross-compiler-path>

But that doesn't work either. I want to attempt the build inside the Dockerfile and discard the build cache which bloat up my container once I no longer need them. What options do I have?

Karan Shah
  • 417
  • 6
  • 21
  • You have to use the `COPY` or `ADD` command to achieve that. – ranieri Aug 21 '20 at 04:19
  • Can you say a little bit more about what you're trying to mount, and why you need to mount something during the build process? (Even outside of Docker that seems a little unusual to me.) – David Maze Aug 21 '20 at 10:46
  • I have a system image for an arm64 device whose driver has to be compiled. The cross-compiler requires that I mount this system image to its own install directory for gathering build related file context and info. – Karan Shah Aug 21 '20 at 10:54

1 Answers1

6

As mentioned in "Can You Mount a Volume While Building Your Docker Image to Cache Dependencies?" from Vladislav Supalov

Although there’s no functionality in Docker to have volumes at build-time, you can use multi-stage builds, benefit from Docker caching and save time by copying data from other images - be it multi-stage or tagged ones.

When building an image, you can’t mount a volume. However, you can copy (COPY) data from another image! By combining this, with a multi-stage build, you can pre-compute an expensive operation once, and re-use the resulting state as a starting point for future iterations.

Example:

FROM ubuntu as intermediate
RUN apt-get install -yqq python-dev python-virtualenv
RUN virtualenv /venv/
RUN mkdir -p /src
# those don't change often
ADD code/basic-requirements.txt /src/basic-requirements.txt
RUN /venv/bin/pip install -r /src/basic-requirements.txt

FROM ubuntu
RUN apt-get install -yqq python-dev python-virtualenv
# the data comes from the above container
COPY --from=intermediate /venv /venv
ADD code/requirements.txt /src/requirements.txt
# this command, starts from an almost-finished state every time
RUN /venv/bin/pip install -r /app/requirements.txt

The OP add in the comments:

I want to mount a volume internally to the container fs using the mount command while build, which currently doesn't work.

Just wanted to know if 'mount' operation, in general is tied to the kernel?

Kernel or not, using mount directly (outside of the sanctioned volumes) is not allowed for security reason, as described here by BMitch.

Docker removes the mount privilege from containers because using this you could mount the host filesystem and escape the container.


If you really need to mount something during the build process, you might consider buildah, which can build without running a container for each layer (like docker build does), and can do so without being root.
Use ONBUILD to read your existing Dockerfile.

Note that with "buildah mount, you can do the reverse: Mounts the specified container's root file system in a location which can be accessed from the host, and returns its location.
That is another alternative.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I guess you misunderstood. I want to mount a volume internally to the container fs using the ```mount``` command while build, which currently doesn't work. (Or is there a way to mount without explicitly calling mount?) – Karan Shah Aug 21 '20 at 06:53
  • @KaranShah From what I understand, there is no internal mount possible. – VonC Aug 21 '20 at 07:01
  • Since mount is being requested for container's internal FS itself, is there any way to 'emulate' a mount process? Just wanted to know if 'mount' operation, in general is tied to the kernel? – Karan Shah Aug 21 '20 at 07:43
  • @KaranShah It is tied to security: https://stackoverflow.com/a/52375147/6309. – VonC Aug 21 '20 at 08:04
  • @KaranShah I have edited the answer with a possible alternative. – VonC Aug 22 '20 at 17:01