Docker-compose在runserver命令中找不到文件manage.py

时间:2019-10-15 17:04:27

标签: python django docker docker-compose

我该如何对我的Django应用进行docker化, 我创建了我的Dockerfile:

FROM python:3.6-alpine
RUN apk add --no-cache linux-headers libffi-dev jpeg-dev zlib-dev
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
RUN mkdir /DEV
WORKDIR /DEV
COPY ./requirements.txt .

RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ENV PYTHONUNBUFFERED 1

COPY . .

这时我创建了docker-compose.yml:

version: '3'

networks:
  mynetwork:
    driver: bridge

services:

  db:
    image: postgres
    restart: always
    ports:
      - "5432:5432"
    networks:
      - mynetwork
    environment:
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypass
      POSTGRES_DB: mydb
    volumes:
      - ./data:/var/lib/postgresql/data

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    networks:
      - mynetwork

    volumes:
      - .:/DEV

    ports:
      - "8000:8000"

    depends_on:
      - db

然后我创建一个.dockerignore文件:

# Ignore
.DS_Store
.idea
.venv2
__pycache__
!manage.py
*.py[cod]
*$py.class
*.so
.Python
*.log
docker-compose.yml
Dockerfile
geckodriver.log
golog.py
golog.pyc
log.html
media
out
output.xml
report.html
startup.sh
templates
testlibs
.dockerignore

好吧,这时我跑步:

docker-compose build --no-cache

最后,图像正确构建,但是当我运行时:

docker-compose up

系统返回此错误:

  

web_1 | python:无法打开文件'manage.py':[Errno 2]没有这样的文件或目录   core_web_1退出,代码2

有人可以帮我解决这个问题吗?

非常感谢

1 个答案:

答案 0 :(得分:1)

尝试使Dockerfile的位置更加明确,然后也更改docker-compose:

FROM python:3.6-alpine
RUN apk add --no-cache linux-headers libffi-dev jpeg-dev zlib-dev
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
RUN mkdir /DEV
WORKDIR /DEV
COPY ./requirements.txt /DEV/

RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ENV PYTHONUNBUFFERED 1

COPY . /DEV/
web:
   build: .
   command: python /DEV/manage.py runserver 0.0.0.0:8000
   networks:
       - mynetwork