1

I am trying to use docker-compose up -d for deploying my django application. The problem is that my Dockerfile and docker-compose.yml are in one directory, but need access to a requirements.txt from the parent directory.

Minimal Example:

Filestructure:

requirements.txt (file)  
docker (directory)  
  docker/Dockerfile (file)  
  docker/docker-compose.yml (file)

Dockerfile:

FROM python:3.10-slim
COPY ./../requirements.txt /requirements.txt

docker-compose.yml:

version: '3'

services:

  django:
    container_name: django_123
    build:
      context: ./..
      dockerfile: ./docker/Dockerfile
    expose:
      - "8000"

The setup works on Docker Desktop 4 on Windows 10, but not on Ubuntu 22.
I get the error:

Step 1/2 : COPY ./../requirements.txt /requirements.txt
COPY failed: forbidden path outside the build context: ../requirements.txt ()
ERROR: Service 'django' failed to build : Build failed

I already read that I should build the image from the parent directory, but I get the same error message if I use docker build -f ../Dockerfile ..

What could be the problem? And why does it work on Windows and not on Ubuntu?

tverdo
  • 122
  • 10

1 Answers1

1

For security reasons, you can only copy from the directory set as the "build context" and below. So doing COPY ./../requirements.txt is not allowed, since that would copy from a directory above the build context.

Since you've set your build context to the parent directory, you should be able to get at the requirements file. You just need to specify your host file paths as originating from the build context, like this:

COPY ./requirements.txt /requirements.txt
Hans Kilian
  • 18,948
  • 1
  • 26
  • 35