django/apache can't serve webpage within Docker Container. Error - ModuleNotFoundError: No module named 'django'
django/apache can't serve webpage within Docker Container. Error - ModuleNotFoundError: No module named 'django'
我的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 没有正确启动。
下面是从 Docker 文件创建容器镜像的输出。
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) #
项目 conf 和步骤工作正常 outside/without docker。
能否请您帮助确定我遗漏了什么,哪里不对?
您的 virtualenv 并未在 ENTRYPOINT 中实际激活。您需要像这样激活它:
ENV PATH=/venv/bin:$PATH
在 RUN
中执行 activate
只会在那个特定的 运行 中激活它,不会在任何未来的中激活它。更详细的解释:https://pythonspeed.com/articles/activate-virtualenv-dockerfile/
我的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 没有正确启动。
下面是从 Docker 文件创建容器镜像的输出。
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) #
项目 conf 和步骤工作正常 outside/without docker。
能否请您帮助确定我遗漏了什么,哪里不对?
您的 virtualenv 并未在 ENTRYPOINT 中实际激活。您需要像这样激活它:
ENV PATH=/venv/bin:$PATH
在 RUN
中执行 activate
只会在那个特定的 运行 中激活它,不会在任何未来的中激活它。更详细的解释:https://pythonspeed.com/articles/activate-virtualenv-dockerfile/