如何在 docker 容器中正确地 运行 django 和 apache

How to run django with apache in a docker container correctly

所以我一直在尝试 docker 在容器上使用 apache 对我的 Django 应用程序进行处理,但我不断收到此错误 Invalid command 'WSGIProcessGroup', perhaps misspelled or defined by a module not included in the server configuration

我尝试用 apt 和 pip 安装 mod_wsgi 但结果还是一样

这是我的 Dockerfile

FROM httpd:2.4

ADD ./requirements.txt ./
RUN apt-get update \
&& apt-get install -y \
python3-pip python3-venv \
libapr1 libapr1-dev \
libaprutil1-dev \
libapache2-mod-wsgi \
&& python3 -m venv /.cp \
&& /.cp/bin/pip install --no-cache-dir -r requirements.txt \
&& rm -rf /var/lib/apt/lists/*

requirements.txt

django
mod-wsgi
psycopg2-binary
djangorestframework
markdown
django-filter

我的自定义虚拟主机文件

<VirtualHost *:80>
    serverAdmin svenikia@taskmaster.com
    DocumentRoot "/usr/local/apache2/htdocs/webapp"
    Servername www.webapp.io
    ServerAlias webapp.io
    ErrorLog "/var/log/apache2/error.log"
    CustomLog "/var/log/apache2/access.log" combined
    Alias /static /capstone/static
    <Directory "/usr/local/apache2/htdocs/webapp/static">
        Require all granted
        AllowOverride None
    </Directory>
    <Directory "/usr/local/apache2/htdocs/webapp/webapp">
        <Files wsgi.py>
            AllowOverride None
            Require all granted
        </Files>
    </Directory>
    WSGIProcessGroup webapp_project
    WSGIDaemonProcess webapp_project python-path=/webapp python-home=/.env
    WSGIScriptAlias / /usr/local/apache2/htdocs/webapp/webapp/wsgi.py
</VirtualHost>

这是我的 docker 撰写文件

version: '2'
services:
        postgres:
                image: postgres:13.1-alpine
                environment:
                        POSTGRES_USER: postgres
                        POSTGRES_PASSWORD: postgres
                        POSTGRES_DB: parking
                restart: always
                container_name: db
        apache:
                build : ./Django
                image: web:1.0
                container_name: web
                ports:
                        - "8080:80"
                volumes:
                        - ./my-httpd.conf:/usr/local/apache2/conf/httpd.conf
                        - ./Django/web.conf:/usr/local/apache2/conf/extra/httpd-vhosts.conf
                        - ./Django/web:/usr/local/apache2/htdocs/my-web
networks:
        default:
                external:
                        name: network 

起初我最初的想法是有3个容器,一个用于postgres,第二个用于django,第三个用于apache。但是在多次失败尝试之后,我只使用了 2 个容器,一个用于 postgres,另一个用于生成 virtualenv、django app 和 apache。任何帮助都会很棒,谢谢

你做的是对的,但它缺少一些自定义的东西:

  1. 首先你需要 link 你的 django 静态文件到 apache 网络服务器,为此你需要在你的 docker-compose.yml 上添加一个服务django 部分 .

之后 使用 volume 你可以 link django 输出作为 apache 的输入

更多详情感谢关注此example