Docker 在 Django 项目中找不到 pipenv 安装的模块
Docker can't find installed modules by pipenv in Django project
所以我正在尝试构建 运行 一个 docker 图像,并将 pipfiles 作为包处理程序。但是即使在看似成功的构建之后,它也找不到 'django' 模块。顺便说一下,我在 Windows 10。
这是我的 pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
django-jquery = "*"
anyconfig = "*"
asgiref = "*"
crispy-forms-materialize = "*"
django-crispy-forms = "*"
django-ranged-response = "*"
pytz = "*"
six = "*"
sqlparse = "*"
Django = "*"
Pillow = "*"
[requires]
python_version = "3.8"
我的Dockerfile
FROM python:3.8
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
RUN pip install pipenv
RUN pipenv install
COPY . /code/
CMD pipenv run python manage.py runserver
和docker-compose.yml
文件
version: '3'
services:
web:
build: .
command: pipenv run python manage.py runserver 0.0.0.0:8000
ports:
- "8000:8000"
这是 docker 构建日志
$ docker-compose build --pull
Building web
Step 1/8 : FROM python:3.8
3.8: Pulling from library/python
Digest: sha256:e02bda1a92a0dd360a976ec3ce6ebd76f6de18b57b885c0556d5af4035e1767d
Status: Image is up to date for python:3.8
---> d47898c6f4b0
Step 2/8 : ENV PYTHONUNBUFFERED 1
---> Using cache
---> 5330d9208341
Step 3/8 : RUN mkdir /code
---> Using cache
---> 57420ad117b3
Step 4/8 : WORKDIR /code
---> Using cache
---> 4831c4a1edf9
Step 5/8 : RUN pip install pipenv
---> Using cache
---> be3d166f236e
Step 6/8 : RUN pipenv install
---> Using cache
---> 38a63cd1032a
Step 7/8 : COPY . /code/
---> c86632082d57
Step 8/8 : CMD pipenv run python manage.py runserver
---> Running in 45b2d9ccc973
Removing intermediate container 45b2d9ccc973
---> d0a474b9c984
Successfully built d0a474b9c984
Successfully tagged myapp_web:latest
...当我尝试 运行 docker-compose up
$ docker-compose up
Recreating myapp_web_1 ... done
Attaching to myapp_web_1
web_1 | Loading .env environment variables�
web_1 | Traceback (most recent call last):
web_1 | File "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 "manage.py", line 21, in <module>
web_1 | main()
web_1 | File "manage.py", line 12, in main
web_1 | raise ImportError(
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?
myapp_web_1 exited with code 1
希望有人能指出我做错了什么。谢谢!
在 运行 pipenv install
之前,您尚未将 Pipfile
复制到 Docker 图像中,因此安装不会安装任何东西。您还应该考虑 pipenv install --deploy --system
选项:由于 Docker 图像本身就是一个隔离层,因此您不需要创建虚拟环境,安装到 "system" Python 很好,这样您以后就不需要 pipenv run
。
FROM python:3.8
ENV PYTHONUNBUFFERED 1
WORKDIR /code # creates the directory too
RUN pip install pipenv
COPY Pipfile Pipfile.lock ./ # <-- add this
RUN pipenv install
COPY . ./
EXPOSE 8000 # typical metadata
# Remove "pipenv run", add the bind argument
# (No need to repeat `command:` in `docker-compose.yml`)
CMD python manage.py runserver 0.0.0.0:8000
所以我正在尝试构建 运行 一个 docker 图像,并将 pipfiles 作为包处理程序。但是即使在看似成功的构建之后,它也找不到 'django' 模块。顺便说一下,我在 Windows 10。
这是我的 pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
django-jquery = "*"
anyconfig = "*"
asgiref = "*"
crispy-forms-materialize = "*"
django-crispy-forms = "*"
django-ranged-response = "*"
pytz = "*"
six = "*"
sqlparse = "*"
Django = "*"
Pillow = "*"
[requires]
python_version = "3.8"
我的Dockerfile
FROM python:3.8
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
RUN pip install pipenv
RUN pipenv install
COPY . /code/
CMD pipenv run python manage.py runserver
和docker-compose.yml
文件
version: '3'
services:
web:
build: .
command: pipenv run python manage.py runserver 0.0.0.0:8000
ports:
- "8000:8000"
这是 docker 构建日志
$ docker-compose build --pull
Building web
Step 1/8 : FROM python:3.8
3.8: Pulling from library/python
Digest: sha256:e02bda1a92a0dd360a976ec3ce6ebd76f6de18b57b885c0556d5af4035e1767d
Status: Image is up to date for python:3.8
---> d47898c6f4b0
Step 2/8 : ENV PYTHONUNBUFFERED 1
---> Using cache
---> 5330d9208341
Step 3/8 : RUN mkdir /code
---> Using cache
---> 57420ad117b3
Step 4/8 : WORKDIR /code
---> Using cache
---> 4831c4a1edf9
Step 5/8 : RUN pip install pipenv
---> Using cache
---> be3d166f236e
Step 6/8 : RUN pipenv install
---> Using cache
---> 38a63cd1032a
Step 7/8 : COPY . /code/
---> c86632082d57
Step 8/8 : CMD pipenv run python manage.py runserver
---> Running in 45b2d9ccc973
Removing intermediate container 45b2d9ccc973
---> d0a474b9c984
Successfully built d0a474b9c984
Successfully tagged myapp_web:latest
...当我尝试 运行 docker-compose up
$ docker-compose up
Recreating myapp_web_1 ... done
Attaching to myapp_web_1
web_1 | Loading .env environment variables�
web_1 | Traceback (most recent call last):
web_1 | File "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 "manage.py", line 21, in <module>
web_1 | main()
web_1 | File "manage.py", line 12, in main
web_1 | raise ImportError(
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?
myapp_web_1 exited with code 1
希望有人能指出我做错了什么。谢谢!
在 运行 pipenv install
之前,您尚未将 Pipfile
复制到 Docker 图像中,因此安装不会安装任何东西。您还应该考虑 pipenv install --deploy --system
选项:由于 Docker 图像本身就是一个隔离层,因此您不需要创建虚拟环境,安装到 "system" Python 很好,这样您以后就不需要 pipenv run
。
FROM python:3.8
ENV PYTHONUNBUFFERED 1
WORKDIR /code # creates the directory too
RUN pip install pipenv
COPY Pipfile Pipfile.lock ./ # <-- add this
RUN pipenv install
COPY . ./
EXPOSE 8000 # typical metadata
# Remove "pipenv run", add the bind argument
# (No need to repeat `command:` in `docker-compose.yml`)
CMD python manage.py runserver 0.0.0.0:8000