Docker-compose 无法导入 Django (ModuleNotFoundError)

sam*_*idi 3 python django docker dockerfile docker-compose

我对 Docker 很陌生,我一直在尝试让一个非常简单的“Hello World”程序在 docker 上运行。无论我做什么,我总是得到:

ModuleNotFoundError:没有名为“Django”的模块

我究竟做错了什么?

这是终端输出。

C:\path\to\app\root>docker-compose up
Creating network "hello-world_default" with the default driver
Creating hello-world_web_1 ... done                                                                                     Attaching to hello-world_web_1
web_1  | Traceback (most recent call last):
web_1  |   File "/code/manage.py", line 10, in main
web_1  |     from django.core.management import execute_from_command_line
web_1  | ModuleNotFoundError: No module named 'django'
web_1  |
web_1  | The above exception was the direct cause of the following exception:
web_1  |
web_1  | Traceback (most recent call last):
web_1  |   File "/code/manage.py", line 21, in <module>
web_1  |     main()
web_1  |   File "/code/manage.py", line 16, in main
web_1  |     ) from exc
web_1  | ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
hello-world_web_1 exited with code 1
Run Code Online (Sandbox Code Playgroud)

这是泊坞窗文件

# Pull base image
FROM python:3.7

# Set environmental variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Install dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system

# Copy project 
COPY . /code/
Run Code Online (Sandbox Code Playgroud)

这是 docker-compose.yml 文件:

version: '3.7'
services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000
Run Code Online (Sandbox Code Playgroud)

编辑

这是我的内容pipfile

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
pytz = "==2019.3"
sqlparse = "==0.3.1"
Django = "==2.2.7"

[requires]
python_version = "3.7"
Run Code Online (Sandbox Code Playgroud)

小智 6

嗨,我刚刚解决了问题。跑完之后

docker build .

运行docker-compose build代替docker-compose up.

然后最后运行docker-compose up