'RUN pip install -r requirements.txt'不起作用

时间:2019-04-24 09:37:37

标签: python docker dockerfile

我是Docker的新手,正在尝试使用Django部署django项目。但是在构建docker-compose之后,我得到了这个错误:

  

错误:无法打开需求文件:[Errno 2]没有此类文件或   目录:“ requirements.txt”错误:服务“网络”生成失败:   命令'/ bin / sh -c pip install -r requirements.txt'返回了   非零代码:1

我可能会设置正确的路径,所以这是我的项目结构

~/Desktop/Projects/ToDoApp
   ToDoApp
     settings.py
   docker-compose.yml
   Dockerfile
   manage.py
   requirements.txt

这是我的dockerfile

#pull official base image
FROM python:3

#set envionment variables
ENV PYTHONUNBUFFERED 1

# Adding requirements file
ADD requirements.txt ToDoApp/ToDoApp

#set work directory
WORKDIR /ToDoApp

#install dependencies
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

CMD ["python", "./ToDoApp/manage.py runserver 0.0.0.0:8000"]

这是我的docker-compose.yml

version: '3'

services:
  web:
    build: .
    command: python /ToDoApp/manage.py runserver 0.0.0.0:8000
    volumes:
        - .:/Desktop/Projects/ToDoApp
    ports:
        - "8000:8000"
    environment:
      - SECRET_KEY = please_change_me

2 个答案:

答案 0 :(得分:1)

我看到了几个问题:

  • 您使用的ADD命令会创建一个名为ToDoApp/ToDoApp的文件,甚至不会创建子目录。
  • 不需要ADD(您无需提取tar或从URL下载),因此可以切换到COPY。
  • 您需要复制代码。
  • 可以对RUN命令重新排序以提高缓存效率。
  • 正确使用相对路径和WORKDIR。
  • 使用json语法时,必须将Args分开

生成的Dockerfile如下:

FROM python:3

#set envionment variables
ENV PYTHONUNBUFFERED 1

# run this before copying requirements for cache efficiency
RUN pip install --upgrade pip

#set work directory early so remaining paths can be relative
WORKDIR /ToDoApp

# Adding requirements file to current directory
# just this file first to cache the pip install step when code changes
COPY requirements.txt .

#install dependencies
RUN pip install -r requirements.txt

# copy code itself from context to image
COPY . .

# run from working directory, and separate args in the json syntax
CMD ["python", "./manage.py", "runserver", "0.0.0.0:8000"]

答案 1 :(得分:0)

尝试一下:

FROM python:3

#set envionment variables
ENV PYTHONUNBUFFERED 1

#set work directory
WORKDIR /ToDoApp

# Adding requirements file
ADD requirements.txt /ToDoApp/

#install dependencies
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

CMD ["python", "./ToDoApp/manage.py runserver 0.0.0.0:8000"]