Access to outside of context in Dockerfile

Dir Structure

Suppose you have the following dir structure

./docker-compose.yaml
./all-runners/
        /start.sh
        /runner-A/Dockerfile
        /runner-B/Dockerfile
        /runner-C/Dockerfile
  • I had a requirement where all Dockerfiles share the same file
  • The top-level docker-compose is the driver of all the builds

Dockerfile

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

COPY start.sh /runtime/start.sh

Docker-compose

  • The real trick is here. The context you want to set is the directory where your main content is located.
  • In this example, your shared context dir is the runtime dir.
    • Imagine all the files under this dir being copied over to a dir called context.
    • Now imaging you can just specify the Dockerfile that you want to copy to that same dir. You can specify that using dockerfile.

The docker-compose.yml is as follows

version: "3.3"
services:

  runner-A
    build:
      context: ./all-runners
      dockerfile: ./runner-A/Dockerfile

  runner-B
    build:
      context: ./all-runners
      dockerfile: ./runner-B/Dockerfile

  runner-C
    build:
      context: ./all-runners
      dockerfile: ./runner-C/Dockerfile
  • Since the context is set to all-runners, the file start.sh will be reuse by each individual Dockerfile specified by the path in dockerfile.
  • You get the same effect as in the parent dir, in a more organized way

Now your build works with files outside the dir of your Dockerfile. The result is just the same when you do the proper mapping!

Happy Dockering 🐳!


As far as I know it's not possible to access things outside out your build context.

You might have some luck by mixing the dockerfile directive with the context directive in your compose file in the root dir of your project as follows:

build:
  context: .
  dockerfile: A/Dockerfile

You may wish to include a .dockerignore in the project root dir to prevent the entire project being send to the docker daemon resulting in potentially much slower builds.

Tags:

Docker

Java

Maven