Search code examples
pythondockerdockerfiledocker-machinerequirements.txt

how to correctly copy requirements.txt for docker file


This is how my current folder structure looks like:

enter image description here

I am present in the FinTechExplained_Python_Docker folder. My Dockerfile looks like this:

FROM python:3.8-slim-buster

WORKDIR /src

COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "main.py"]

However, when I run this command docker build --tag FinTechExplained_Python_Docker .

I get this error

ERROR [3/5] COPY requirements.txt requirements.txt                    0.0s
------
 > [3/5] COPY requirements.txt requirements.txt:
------
failed to compute cache key: "/requirements.txt" not found: not found

What am I doing wrong?

Edit:

I also tried changing it to:

COPY str/requirements.txt requirements.txt:

but then I would still get the error that:

failed to compute cache key: "/src/requirements.txt" not found: not found

maybe the second COPY statement is also to be changed but not sure how


Solution

  • You need to specify the source of your COPY statements relative to the build context, like this

    FROM python:3.8-slim-buster
    
    WORKDIR /src
    
    COPY src/requirements.txt requirements.txt
    RUN pip install --no-cache-dir -r requirements.txt
    
    COPY src/ .
    
    CMD [ "python", "main.py"]