Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have such folder structure

project
    - config
        -docker
           Dockerfile
           docker-compose.yml
    - src
       here_is_code
    requirements.txt

Dockerfile

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD ../../requirements.txt /code/
RUN pip install -r requirements.txt
ADD src /code/

docker-compose.yml

version: '3'

services:
  web:
    build:
      context: ../../
      dockerfile: config/docker/Dockerfile
    command:
        bash -c "ls"
    volumes:
      - .:/code
    expose:
      - "8000"
  nginx:
    image: nginx
    ports:
    - "8000:8000"
    volumes:
      - .:/code
      - ./config/nginx:/etc/nginx/conf.d
    depends_on:
    - web

When I run docker-compose build I get following error:

Service 'web' failed to build: ADD failed: Forbidden path outside the build context: ../../requirements.txt ()

Is it possible to add requirements.txt or I'll have to copy this file into docker directory? Or maybe I need to use any entrypoint (entrypoint.sh)?

UPDATE

After docker build -f config/docker/Dockerfile . and docker-compose up I can't see my code there. Here is the output of ls -R /code

web_1    | /code:
web_1    | Dockerfile
web_1    | config
web_1    | docker-compose.yml
web_1    | src
web_1    | static
web_1    | 
web_1    | /code/config:
web_1    | nginx
web_1    | 
web_1    | /code/config/nginx:
web_1    | 
web_1    | /code/src:
web_1    | static
web_1    | 
web_1    | /code/src/static:
web_1    | 
web_1    | /code/static:
like image 567
Headmaster Avatar asked Jan 21 '19 09:01

Headmaster


People also ask

How do I put files outside Docker's build context?

The best way to work around this is to specify the Dockerfile independently of the build context, using -f. For instance, this command will give the ADD command access to anything in your current directory. docker build -f docker-files/Dockerfile .

What is build context Docker?

The docker build command builds Docker images from a Dockerfile and a “context”. A build's context is the set of files located in the specified PATH or URL . The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

How do I make multiple Dockerfiles?

Let's say we have two Dockerfiles, one for building the backend and another for building the frontend. We can name them appropriately and invoke the build command two times, each time passing the name of one of the Dockerfiles: $ docker build -f Dockerfile.

Can Dockerfile copy from parent directory?

It turns out that you cannot include files outside Docker's build context. However, you can copy files from the Dockerfile's parent directory.


Video Answer


1 Answers

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

like image 199
Bicameral Mind Avatar answered Sep 18 '22 20:09

Bicameral Mind