How to include files outside of Dockers build context?

How to include files outside of Dockers build context?

The best way to work around this is to specify the Dockerfile independently of the build context, using -f.

n

For instance, this command will give the ADD command access to anything in your current directory.

n

docker build -f docker-files/Dockerfile .n

n

Update: Docker now allows having the Dockerfile outside the build context (fixed in 18.03.0-ce). So you can also do something like

n

docker build -f ../Dockerfile .n

I often find myself utilizing the --build-arg option for this purpose. For example after putting the following in the Dockerfile:

n

ARG SSH_KEYnRUN echo $SSH_KEY > /root/.ssh/id_rsan

n

You can just do:

n

docker build -t some-app --build-arg SSH_KEY=$(cat ~/file/outside/build/context/id_rsa) .n

n

But note the following warning from the Docker documentation:

n

Warning: It is not recommended to use build-time variables for passing secrets like github keys, user credentials etc. Build-time variable values are visible to any user of the image with the docker history command.

How to include files outside of Dockers build context?

I spent a good time trying to figure out a good pattern and how to better explain whats going on with this feature support. I realized that the best way to explain it was as follows…

n

    n

  • Dockerfile: Will only see files under its own relative path
  • n

  • Context: a place in space where the files you want to share and your Dockerfile will be copied to
  • n

n

So, with that said, heres an example of the Dockerfile that needs to reuse a file called start.sh

n

Dockerfile

n

It will always load from its relative path, having the current directory of itself as the local reference to the paths you specify.

n

COPY start.sh /runtime/start.shn

n

Files

n

Considering this idea, we can think of having multiple copies for the Dockerfiles building specific things, but they all need access to the start.sh.

n

./all-services/n   /start.shn   /service-X/Dockerfilen   /service-Y/Dockerfilen   /service-Z/Dockerfilen./docker-compose.yamln

n

Considering this structure and the files above, heres a docker-compose.yml

n

docker-compose.yaml

n

    n

  • In this example, your shared context directory is the runtime directory.n
      n

    • Same mental model here, think that all the files under this directory are moved over to the so-called context.
    • n

    • Similarly, just specify the Dockerfile that you want to copy to that same directory. You can specify that using dockerfile.
    • n

    n

  • n

  • The directory where your main content is located is the actual context to be set.
  • n

n

The docker-compose.yml is as follows

n

version: 3.3nservices:n  n  service-An    build:n      context: ./all-servicen      dockerfile: ./service-A/Dockerfilenn  service-Bn    build:n      context: ./all-servicen      dockerfile: ./service-B/Dockerfilenn  service-Cn    build:n      context: ./all-servicen      dockerfile: ./service-C/Dockerfilen

n

    n

  • all-service is set as the context, the shared file start.sh is copied there as well the Dockerfile specified by each dockerfile.
  • n

  • Each gets to be built their own way, sharing the start file!
  • n

Leave a Reply

Your email address will not be published.