Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: Cannot COPY from parent directory while building the image [duplicate]

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?

like image 974
tverdo Avatar asked Jan 24 '26 15:01

tverdo


1 Answers

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
like image 87
Hans Kilian Avatar answered Jan 27 '26 05:01

Hans Kilian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!