Docker 封装Django+apache+mod_wsgi应
第一步:首先安装docker ubuntu系统
参考链接:https://docs.docker.com/install/linux/docker-ce/ubuntu/
命令如下:
$ sudo apt-get remove docker docker-engine docker.io containerd runc
$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
第二步:拉取python:3.7镜像
docker pull python:3.7
docker images 查看下载的镜像
第三步:进入Django 项目主目录
下载 mod_wsgi
wget https://files.pythonhosted.org/packages/26/03/a3ed5abc2e66c82c40b0735c2f819c898d136879b00be4f5537126b6a4a4/mod_wsgi-4.6.7.tar.gz
apache 配置文件也放入主目录 000-default.conf 我的文件内容如下
<VirtualHost *:8000>
WSGIScriptAlias / /usr/src/app/devops/wsgi.py
WSGIPAssAuthorization On
<Directory /usr/src/app/devops/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
python3.7 库文件widgets.py 152行修改为 '%s=%s' % (k, v) for k, v in params.items()
将修改的文件也放入主目录
上面的这些文件你也可以自己定义放置目录
创建文件 wsgi.load
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
第四步:编辑Dockerfile
FROM python:3.7
#创建工作目录
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
#安装apache2
RUN apt-get update -y
RUN apt-get install -y apache2 apache2-dev
#拷贝项目文件
COPY . .
#项目包的安装
RUN pip install -r requirements.txt
COPY ./widgets.py /usr/local/lib/python3.7/site-packages/django/contrib/admin/
#替换apache2配置
COPY 000-default.conf /etc/apache2/sites-available/
mv wsgi.load /etc/apache2/mods-available
#安装mod_wsgi
RUN tar zxvf mod_wsgi-4.6.7.tar.gz
RUN cd mod_wsgi-4.6.7 && ./configure --with-python=/usr/local/bin/python && make && make install
RUN cd /etc/apache2/mods-enabled && ln -sf ../mods-avaliable/wsgi.load wsgi.load
EXPOSE 80
#apache启动脚本
RUN echo -e '#!/bin/bash\napachectl -k start\n/bin/bash' >/etc/apache2/auto_service.sh &&\
chmod 777 /etc/apache2/auto_service.sh
CMD ['/etc/apache2/auto_service.sh']
第五步:建立镜像
docker build --no-cache -t apache:v1 .
第六步:创建容器
docker run --privileged -d --name apache -p 6003:80 -t apache:v1
第七步:访问 http://localhost:6003 没有问题就可以正常访问。
如有异常 docker exec -i -t apache /bin/bash 命令可以进入容器查看异常