django/apache 无法在 Docker 容器中提供网页。错误 - ModuleNotFoundError : No module named 'django'

标签 django python-3.x docker apache2 mod-wsgi

我的 Docker 文件

#Download base image from ubuntu
FROM python:3.8

#Install necessary softwares
RUN apt-get update 
RUN apt-get upgrade -y
RUN apt-get install -y python3-venv python3-pip apache2 libapache2-mod-wsgi-py3

#Set the python ENV
RUN python3 -m venv venv
#ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN /bin/bash -c ". /venv/bin/activate"

#Install django and dependency libs
RUN /venv/bin/pip3 install django psycopg2-binary

#Copy the artefacts to the python env 
COPY . /home/ubuntu/envio-project/envio

WORKDIR /home/ubuntu/envio-project/envio

#ENV APACHE_RUN_USER www-data
#ENV APACHE_RUN_GROUP www-data
#ENV APACHE_LOG_DIR /var/log/apache2

#Change the permission
RUN chown -R root:www-data /home/ubuntu/envio-project/envio
RUN chmod u+rwx,g+rx,o+rx /home/ubuntu/envio-project/envio

#COPY the apache conf 
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
COPY apache2.conf /etc/apache2/apache2.conf

EXPOSE 80

#RUN service apache2 start
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]

Docker 镜像创建成功,容器可以正常运行。但是 apache2 没有正确启动。

下面是从 Dockerfile 创建容器镜像的输出。
Enabling site 000-default.
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Setting up libapache2-mod-wsgi-py3 (4.6.5-1) ...
apache2_invoke: Enable module wsgi
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of restart.
Setting up python3-secretstorage (2.3.1-2) ...
Setting up python3-keyring (17.1.1-1) ...
Processing triggers for libc-bin (2.28-10) ...
Removing intermediate container 33d9de70fcf3
 ---> 634a1610c39f
Step 5/15 : RUN python3 -m venv venv
 ---> Running in c06f112ade79
Removing intermediate container c06f112ade79
 ---> 9f01a3f6bda8
Step 6/15 : RUN /bin/bash -c ". /venv/bin/activate"
 ---> Running in 29f984e84a4e
Removing intermediate container 29f984e84a4e
 ---> 75d5a582c091
Step 7/15 : RUN pip3 install django psycopg2-binary
 ---> Running in 179e057d4ab4
Collecting django
  Downloading https://files.pythonhosted.org/packages/55/d1/8ade70e65fa157e1903fe4078305ca53b6819ab212d9fbbe5755afc8ea2e/Django-3.0.2-py3-none-any.whl (7.4MB)
Collecting psycopg2-binary
  Downloading https://files.pythonhosted.org/packages/e0/97/583fae6781b1d686af3c25db1c4f2d064aa0e93248dcbf1b8b1b141f64e3/psycopg2_binary-2.8.4-cp38-cp38-manylinux1_x86_64.whl (3.0MB)
Collecting pytz
  Downloading https://files.pythonhosted.org/packages/e7/f9/f0b53f88060247251bf481fa6ea62cd0d25bf1b11a87888e53ce5b7c8ad2/pytz-2019.3-py2.py3-none-any.whl (509kB)
Collecting asgiref~=3.2
  Downloading https://files.pythonhosted.org/packages/a5/cb/5a235b605a9753ebcb2730c75e610fb51c8cab3f01230080a8229fa36adb/asgiref-3.2.3-py2.py3-none-any.whl
Collecting sqlparse>=0.2.2
  Downloading https://files.pythonhosted.org/packages/ef/53/900f7d2a54557c6a37886585a91336520e5539e3ae2423ff1102daf4f3a7/sqlparse-0.3.0-py2.py3-none-any.whl
Installing collected packages: pytz, asgiref, sqlparse, django, psycopg2-binary
Successfully installed asgiref-3.2.3 django-3.0.2 psycopg2-binary-2.8.4 pytz-2019.3 sqlparse-0.3.0
Removing intermediate container 179e057d4ab4
 ---> 3aa0a74c8d76
Step 8/15 : COPY . /home/ubuntu/envio-project/envio
 ---> a9b67ff40aa3
Step 9/15 : WORKDIR /home/ubuntu/envio-project/envio
 ---> Running in e8c2913faf03
Removing intermediate container e8c2913faf03
 ---> 33ddb79035c6
Step 10/15 : RUN chown -R root:www-data /home/ubuntu/envio-project/envio
 ---> Running in 4be8217cf224
Removing intermediate container 4be8217cf224
 ---> b8e7fa377da2
Step 11/15 : RUN chmod u+rwx,g+rx,o+rx /home/ubuntu/envio-project/envio
 ---> Running in c2489715e7f7
Removing intermediate container c2489715e7f7
 ---> 58b773577f23
Step 12/15 : COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
 ---> 8c7d4ea045be
Step 13/15 : COPY apache2.conf /etc/apache2/apache2.conf
 ---> 97a13a8ce492
Step 14/15 : EXPOSE 80
 ---> Running in 0b74ba729994
Removing intermediate container 0b74ba729994
 ---> 648a362a0a17
Step 15/15 : CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]
 ---> Running in 323d71a03a4a
Removing intermediate container 323d71a03a4a
 ---> b471321aa627
Successfully built b471321aa627
Successfully tagged django-apache2:latest
ubuntu@ip-172-31-29-211:~/envio-project/envio$ 

我检查了图像,可以确认 django 已正确安装在容器和 python 虚拟环境中。
ubuntu@ip-172-31-29-211:~$ docker run -it django-apache2 sh
# . /venv/bin/activate
(venv) # python3 -m django --version
3.0.2
(venv) # ls -lrt
total 36
-rw-rw-r-- 1 root www-data  102 Jan 15 16:34 requirements.txt
-rw-rw-r-- 1 root www-data  625 Jan 15 16:34 manage.py
drwxrwxr-x 1 root www-data 4096 Jan 15 16:34 enviowebapp
-rw-rw-r-- 1 root www-data 7244 Jan 15 16:34 apache2.conf
-rw-rw-r-- 1 root www-data  160 Jan 15 16:43 docker-compose.yml
-rw-rw-r-- 1 root www-data 1845 Jan 15 17:02 000-default.conf
drwxrwxr-x 1 root www-data 4096 Jan 15 17:16 envio
-rw-rw-r-- 1 root www-data 1075 Jan 15 17:52 Dockerfile
(venv) # 
(venv) # 
(venv) # service apache2 start
[ ok ] Starting Apache httpd web server: apache2.
(venv) # 
(venv) # 
(venv) # curl http://localhost/home
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator at 
 webmaster@localhost to inform them of the time this error occurred,
 and the actions you performed just before this error.</p>
<p>More information about this error may be available
in the server error log.</p>
<hr>
<address>Apache/2.4.38 (Debian) Server at localhost Port 80</address>
</body></html>
(venv) # 
(venv) # 
(venv) # cat /var/log/apache2/error.log
[Wed Jan 15 17:54:16.484944 2020] [mpm_event:notice] [pid 26:tid 140084587144320] AH00489: Apache/2.4.38 (Debian) mod_wsgi/4.6.5 Python/3.7 configured -- resuming normal operations
[Wed Jan 15 17:54:16.485074 2020] [core:notice] [pid 26:tid 140084587144320] AH00094: Command line: '/usr/sbin/apache2'
[Wed Jan 15 17:54:29.957753 2020] [wsgi:error] [pid 29:tid 140084554376960] [remote 127.0.0.1:38206] mod_wsgi (pid=29): Failed to exec Python script file '/home/ubuntu/envio-project/envio/envio/wsgi.py'.
[Wed Jan 15 17:54:29.957804 2020] [wsgi:error] [pid 29:tid 140084554376960] [remote 127.0.0.1:38206] mod_wsgi (pid=29): Exception occurred processing WSGI script '/home/ubuntu/envio-project/envio/envio/wsgi.py'.
[Wed Jan 15 17:54:29.957972 2020] [wsgi:error] [pid 29:tid 140084554376960] [remote 127.0.0.1:38206] Traceback (most recent call last):
[Wed Jan 15 17:54:29.957997 2020] [wsgi:error] [pid 29:tid 140084554376960] [remote 127.0.0.1:38206]   File "/home/ubuntu/envio-project/envio/envio/wsgi.py", line 12, in <module>
[Wed Jan 15 17:54:29.958004 2020] [wsgi:error] [pid 29:tid 140084554376960] [remote 127.0.0.1:38206]     from django.core.wsgi import get_wsgi_application
[Wed Jan 15 17:54:29.958020 2020] [wsgi:error] [pid 29:tid 140084554376960] [remote 127.0.0.1:38206] ModuleNotFoundError: No module named 'django'
(venv) # 


项目配置和步骤在外部/没有 docker 下工作正常。

您能否帮助确定我遗漏了什么以及什么不正确?

最佳答案

您的 virtualenv 实际上并未在 ENTRYPOINT 中激活。您需要像这样激活它:

ENV PATH=/venv/bin:$PATH

activateRUN仅在该特定 RUN 中激活它,而不是在以后的任何 RUN 中激活它。更详细的解释:https://pythonspeed.com/articles/activate-virtualenv-dockerfile/

关于django/apache 无法在 Docker 容器中提供网页。错误 - ModuleNotFoundError : No module named 'django' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59756949/

相关文章:

python-3.x - 如何在 python 项目之间共享代码?

python - 使用 asyncio 与其他对等方连接时如何处理 ConnectionRefusedError

Docker-compose - 设置不是文字的环境变量

docker 和 composer 安装

docker - 使用docker-composer扩展并在主机名后附加数字?

python - 如何解决这个问题: ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

python - 更新或编辑 json 文件时出现 Json 格式问题

python - Django 查询异常 __init__() 得到了一个意外的关键字参数 'hints'

创建集合的 Python 性能比较 - set() 与 {} 文字

python - 我可以使用 python 正则表达式来表示字母、破折号和下划线吗?