docker-compose - ADD failed: Forbidden path outside the build context

You cannot get outside of build context (which is normally the working directory) of Docker when building an image.

The reason is pretty simple - Docker consists of command line client and daemon, when you call docker build ... first thing happening is that your client packs entire folder (build context) into single archive and sends it to daemon together with your Dockerfile. Daemon gets an archive and instructions from Dockerfile and that means daemon does not access your local filesystem when building an image and cannot walk through ../.. references.

What you need to to set the build context to your root folder and specify Dockerfile explicitly.

You build command will look like

docker build -f config/docker/Dockerfile .

And inside Dockerfile you have to remember that all paths are relative to the project root.

So finally you come to following compose file:

docker-compose.yml

version: '3'
services:
  web:
    build:
      context: .  # here changed
      dockerfile: config/docker/Dockerfile
    command: ["bash", "-c", "ls"]
    expose:
      - "8000"
  nginx:
    image: nginx
    ports:
      - "8000:8000"
    depends_on:
      - web

You go to project root and run

docker-compose -f config/docker/docker-compose.yml up

Hope, my answer will help somebody (it is based on many GitHub repositories, I have looked though)

I have wrong project structure. If you need to separate docker files and application code, it's better to put the code in any folder (called app for example) in the root folder. Docker files should also be in the root folder. Such structure will avoid a lot of problems and it will be easy to use docker/docker-compose


context

It's all about context. Specify context and dockerfile in your build and you can plant your Dockerfile anywhere. Play a round with it (that's what she said).

I would at least keep the docker-compose.yaml in a root directory.


build:
  context: .
  dockerfile: dockerfiles/project-one/Dockerfile


_______________________________
deployments/docker-compose.yml:
[...]
service_name:
  build:
      context: ..                           # *That's what you need!*
      dockerfile: build/package/Dockerfile

_________________________
build/package/Dockerfile:
[...]
COPY . .

_____________________
from root of project:

docker-compose -f deployments/docker-compose.yml up

And your container will have everything from the root of a project.