'Creating an image with Docker and resources outside the main directory

I try to build an image with Docker (10.20.13 on RH 7.9). But some of my resources are outside the Dockerfile directory. Below is my tree :

/dir1
├── dir2
│   ├── dir3
│   │   ├── dir4
│   │   │   ├── boost
│   │   │   │   └── lib
│   │   │   │       ├── linuxV2_6_18A32
│   │   │   │       │   ├── libboost_atomic-mt.a
│   │   │   │       │   ├── ....

/home/myproject/myDockerfile

I want to add in my image the resources that are in /dir1/dir2/dir3/dir4/boost which are not necessary my resources (but I do have at least read access). My first try was to build an image from /home/myproject/myDockerfile with the following command :

/home/myproject/myDockerfile/docker build -t myimage:1.0 .

But it failed with, saying this:

ADD failed: file not found in build context or excluded by .dockerignore: stat dir1: file does not exist

Okay, the dir1 is not in the context. So I tried to make a link to dir1 in the Dockerfile directory, and again the same command, but different issue :

ADD failed: forbidden path outside the build context: netdata ()

Third try, I launch the command from the root directory (to get all the context as I understand), with the following command:

docker build -t myimage:1.0 -f /home/myproject/myDockerfile

This time I get this response:

error checking context: 'no permission to read from '/boot/System.map-3.10.0-1160.31.1.el7.x86_64''

So I image to add the last directory to my .dockerignore, but it should be in the context (root directory) which is impossible.

So is there a solution to my problem apart copying in project directory all the resources I need?



Solution 1:[1]

You have to copy all of the resources you need into the project directory. You can't really build a Docker image containing files from completely unrelated parts of the filesystem (you can't include a library from /usr/lib from a Dockerfile in your home directory).

Since what you're trying to include is a static library, you have a couple of options to get it. The easiest is to just install it via your base image's package manager:

FROM debian:stable
RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive \
    apt-get install --no-install-recommends --assume-yes \
      libboost-atomic1.74-dev

A second harder option is to use a multi-stage build or a similar technique to build the library from source.

Since the specific file you're referencing is a static library, another option could be to build the binary on the host system and COPY it into the image unmodified. This requires you to be on a native-Linux host with a similar base Linux distribution.

FROM debian:stable
COPY ./myapp /usr/local/bin
CMD ["myapp"]
gcc -o myapp ... -lboost_atomic-mt
docker build -t myapp .

If all else fails then you can make a copy locally. You might write a script to do this; this is also a place where Make works well since it's largely dealing with concrete files.

#!/bin/sh
mkdir ./docker
cp -a Dockerfile .dockerignore src ./docker
cp /dir1/dir2/.../libboost_atomic-mt.a ./docker
docker build -t myapp ./docker

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Maze