1

我是码头工人的新手。我正在尝试对一个简单的 django 应用程序进行 docker 化,该应用程序是我在 ubuntu 的 anaconda3 中创建的。

我的 dockerfile 看起来像这样:

# Pull base image
FROM continuumio/anaconda3

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

COPY requirement.txt /code/
RUN conda install --file requirement.txt

# Copy project
COPY . /code/

requirement.txt是我的 conda 虚拟环境中的依赖包列表。我通过在我的 conda 激活环境中运行这一行来获得它:

conda list -e > requirement.txt 包裹django=3.0.3=py_0在这个requirement.txt

另外,此时,我已经正确安装了 docker 并拉取了 base image continuumio/anaconda3

这是我的docker-compose.yml文件:

version: '3.8'

services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000

现在,当我运行时docker-compose up,我收到了这条错误消息链(此时,所有依赖包requirement.txt都已安装):

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_project_web_1 exited with code 1

我的问题:我做错了什么?为什么 docker 要求激活虚拟环境?现在容器本身不就是“虚拟环境”了吗?

我在这里看到了一个使用“pip”的工作示例。但我习惯了 conda 并且很想弄清楚这一点。

谢谢,

4

0 回答 0