Dockerfile构建Python镜像失败:COPY无法找到requirements.txt
Got it, let's break down why you're hitting this error and get your Docker build working again:
1. First, fix the typo in your Dockerfile
Looking at your Dockerfile, there's a clear typo in the COPY command:
COPY requirements.txt $PROJECT_DIR/requirments.txt
You misspelled requirements.txt as requirments.txt (missing an e) in the target path. Even better, since you've already set WORKDIR $PROJECT_DIR, you can simplify this line to avoid path typos entirely:
COPY requirements.txt ./
This will copy the file directly into your working directory (/var/www/coroner) without needing to type out the full path.
2. Double-check your build context
Your build command uses . as the context, meaning Docker looks for files in your current working directory. Even though you said you checked, let's confirm two things:
- Run
ls -lin your terminal to make surerequirements.txtis definitely present in the directory where you're running thedocker buildcommand. - If you have a
.dockerignorefile, open it and make sure it doesn't listrequirements.txtor a wildcard like*.txtthat would exclude the file from the build context.
3. Remove the redundant directory creation
Your Dockerfile runs RUN mkdir -p $PROJECT_DIR after setting WORKDIR $PROJECT_DIR—this is unnecessary because WORKDIR automatically creates the directory if it doesn't exist. Removing this line avoids any potential permission or directory structure issues.
Here's your corrected Dockerfile
FROM python:3.6.7-slim # Version: 1.4 # Dockerfile to build the coroner container. # Install Python and Package Libraries RUN apt-get update && apt-get upgrade -y && apt-get autoremove && apt-get autoclean RUN apt-get install -y \ libffi-dev \ libssl-dev \ default-libmysqlclient-dev \ libxml2-dev \ libxslt-dev \ libjpeg-dev \ libfreetype6-dev \ zlib1g-dev \ net-tools \ nano ARG PROJECT=coroner ARG PROJECT_DIR=/var/www/${PROJECT} WORKDIR $PROJECT_DIR ENV PYTHONUNBUFFERED 1 COPY requirements.txt ./ RUN pip install --upgrade pip RUN pip install -r requirements.txt EXPOSE 8888 STOPSIGNAL SIGINT ENTRYPOINT ["python", "manage.py"] CMD ["runserver", "0.0.0.0:8888"]
If you're still stuck, try these extra steps
- Explicitly specify the source file path in the COPY command:
COPY ./requirements.txt ./to leave no doubt where Docker should look. - Rebuild without cache to rule out old cached layers causing issues:
sudo docker build --no-cache -t coroner:dev . - Check file permissions: Make sure
requirements.txthas at least read permissions for others (runchmod 644 requirements.txtif needed) so Docker can access it.
内容的提问来源于stack exchange,提问作者Richard Barnes

