需要的文本文件(sources.list, pip.conf, dockerfile, supervisor.conf, nginx.conf, uwsgi.ini与项目中manage.py同级目录)
django框架的代码 -> 项目文件
sources.list -> 更改基础镜像软件源文件
pip.conf -> 更改pip源的文件
dockerfile -> 生成镜像引导文件
nginx.conf -> nginx配置文件
supervisor.conf -> supervisor 配置文件
source.list 文件内容(提交软件下载速度):
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
pip.conf (提高pip安装的软件下载速度):
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
dockerfile文件 (WORKDIR:等同 cd 命令,COPY ./ ./ :复制当前目录下所有文件到workdir目录):
FROM ubuntu:16.04
ADD sources.list /etc/apt/sources.list
RUN apt-get -y update && apt-get install -y nginx supervisor vim python-dev python-pip python-setuptools && apt-get clean all
COPY pip.conf /root/.pip/pip.conf
RUN python -m pip install --upgrade pip && pip install uwsgi
RUN mkdir -p /data/apps/mysite /data/tmp/sock /data/tmp/pid /data/logs/uwsgi /data/logs/nginx
WORKDIR /data/apps/mysite
COPY ./ ./
RUN pip install -r /data/apps/mysite/requirements.txt
RUN ln -s /data/apps/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
RUN ln -s /data/apps/mysite/supervisord.conf /etc/supervisor/conf.d/
WORKDIR /data
CMD ["/usr/bin/supervisord", "-n"]
* WORKDIR /data 不能去掉,supervisorctl在workdir下没有权限开启
nginx.conf (nginx配置文件):
server{
listen 8000;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
location / {
uwsgi_pass unix:///data/tmp/sock/mysite.sock;
include /etc/nginx/uwsgi_params;
}
}
supervisor.conf (supervisor 配置文件):
[program:nginx]
command = /usr/sbin/nginx -g 'daemon off';
autostart = true
autorestart = true
stdout_logfile = /data/logs/nginx/nginx_out.log
stderr_logfile = /data/logs/nginx/nginx_err.log
[program:uwsgi]
command = /usr/local/bin/uwsgi --ini /data/apps/mysite/mysite_uwsgi.ini
autostart = true
autorestart = true
stdout_logfile = /data/logs/uwsgi/uwsgi_out.log
stderr_logfile = /data/logs/uwsgi/uwsgi_err.log
uwsgi.ini (uwsgi 配置文件):
[uwsgi]
chdir = / data / apps / mysite
module = mysite.wsgi.application
master = true
processes = 2
socket = / data / tmp / sock / mysite.sock
pidfile = / data / tmp / pid / mysite.pid
chmod - socket = 666
vacuum = true
在manage.py文件目录下执行的docker命令:
docker pull ubuntu:16.04 拉直ubuntu基础镜像
docker build -t djangotest:01 . 生成tag为01 name为djangotest的镜像
docker run djangotest:01 运行项目
docker exec -it 容器id /bin/bash 进入容器查看项目运行情况
参考文档:https://blog.csdn.net/qq_36792209/article/details/82778611