运行效果
1.拉取django 项目依赖的中间件(python3+Uwsgi+Nginx+MySQL+Redis)
2.使用Dockerfile 打包项目
1.在项目添加以下文件夹目录
1./compose/mysql/conf/my.cnf
# compose/mysql/conf/my.cnf
[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
port = 3306 # 端口与docker-compose里映射端口保持一致
#bind-address= localhost #一定要注释掉,mysql所在容器和django所在容器不同IP
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
skip-name-resolve # 这个参数是禁止域名解析的,远程访问推荐开启skip_name_resolve。
[client]
port = 3306
default-character-set=utf8
[mysql]
no-auto-rehash
default-character-set=utf8
2./compose/mysql/init/init.sql
# compose/mysql/init/init.sql
GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "p@ssw0rdwcx";
FLUSH PRIVILEGES;
3./compose/nginx/Dockerfile
# nginx镜像compose/nginx/Dockerfile
FROM nginx:latest
# 删除原有配置文件,创建静态资源文件夹和ssl证书保存文件夹
RUN rm /etc/nginx/conf.d/default.conf \
&& mkdir -p /usr/share/nginx/html/static \
&& mkdir -p /usr/share/nginx/html/media \
&& mkdir -p /usr/share/nginx/ssl
# 设置Media文件夹用户和用户组为Linux默认www-data, 并给予可读和可执行权限,
# 否则用户上传的图片无法正确显示。
RUN chown -R www-data:www-data /usr/share/nginx/html/media \
&& chmod -R 775 /usr/share/nginx/html/media
# 添加配置文件
ADD ./nginx.conf /etc/nginx/conf.d/
# 关闭守护模式
CMD ["nginx", "-g", "daemon off;"]
4./compose/nginx/nginx.conf
# nginx配置文件
# compose/nginx/nginx.conf
upstream sobase {
server web:8010; # Docker-compose web服务端口
keepalive 2000;
}
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
server_tokens off;
server {
listen 80; # 监听80端口
server_name localhost; # 可以是nginx容器所在ip地址或127.0.0.1,不能写宿主机外网ip地址
charset utf-8;
client_max_body_size 10M; # 限制用户上传文件大小
location /static {
alias /usr/share/nginx/html/static; # 静态资源路径
}
location /media {
alias /usr/share/nginx/html/media; # 媒体资源,用户上传文件路径
}
location /sobase {
client_max_body_size 50m;
include /etc/nginx/uwsgi_params;
#proxy_pass http://sobase;
uwsgi_pass sobase;
uwsgi_read_timeout 600;
uwsgi_connect_timeout 600;
uwsgi_send_timeout 600;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $http_x_forwarded_for; # $http_x_forwarded_for变量 = X-Forwarded-For变量
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
5./compose/redis/redis.conf
# compose/redis/redis.conf
# Redis 5配置文件下载地址
# https://raw.githubusercontent.com/antirez/redis/5.0/redis.conf
# 请注释掉下面一行,变成#bind 127.0.0.1,这样其它机器或容器也可访问
bind 127.0.0.1
# 取消下行注释,给redis设置登录密码。这个密码django settings.py会用到。
requirepass yourpassword
6./SOBase/Dockerfile
# SOBase/Dockerfile
# 建立 python3.7 环境
FROM python:3.7
# 镜像作者cx
MAINTAINER cx
# 设置 python 环境变量
ENV PYTHONUNBUFFERED 1
COPY pip.conf /root/.pip/pip.conf
# 创建 SOBase 文件夹
RUN mkdir -p /smartOffice/SOBase
# 将 SOBase 文件夹为工作目录
WORKDIR /smartOffice/SOBase
# 将当前目录加入到工作目录中(. 表示当前目录)
ADD . /smartOffice/SOBase
# 更新pip版本
RUN /usr/local/bin/python -m pip install --upgrade pip
# 利用 pip 安装依赖
RUN pip install -r requirements.txt
# 去除windows系统编辑文件中多余的\r回车空格
RUN sed -i 's/\r//' ./start.sh
# 给start.sh可执行权限
RUN chmod +x ./start.sh
7./SOBase/pip.conf
[global]
index-url = http://pypi.douban.com/simple #豆瓣源,可以换成其他的源
extra-index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host =
pypi.douban.com #添加豆瓣源为可信主机,要不然可能报错
pypi.tuna.tsinghua #清华
timeout = 120
8./SOBase/start.sh
#!/bin/bash
# 从第一行到最后一行分别表示:
# 1. 收集静态文件到根目录,
# 2. 生成数据库可执行文件,
# 3. 根据数据库可执行文件来修改数据库
# 4. 用 uwsgi启动 django 服务
python manage.py collectstatic --noinput&&
python manage.py makemigrations --settings=SOBase.test_settings&&
python manage.py migrate --settings=SOBase.test_settings&&
uwsgi --ini /smartOffice/SOBase/uwsgi.ini
9./SOBase/uwsgi.ini
[uwsgi]
project=SOBase
uid=www-data
gid=www-data
base=/var/www/html
chdir=%(base)/%(project)
#wsgi-file=shopping/pro_wsgi.py
module=%(project).test_wsgi:application
master=True
processes=2
socket=0.0.0.0:8010
chown-socket=%(uid):www-data
chmod-socket=664
vacuum=True
max-requests=5000
pidfile=/tmp/%(project)-master.pid
daemonize=/tmp/%(project)-uwsgi.log
harakiri = 60
buffer-size= 65535
harakiri-verbose = true
memory-report = true
reload-mercy = 10
reload-on-as= 1024
python-autoreload=1
# 设置日志文件最大字节数
log-maxsize=1000
# 设置每个请求进程最大请求数
max-requests=1000
10./SOBase/docker-compose.yaml
version: "3"
volumes: # 自定义数据卷,位于宿主机/var/lib/docker/volumes内
sobase_db_vol: # 定义数据卷同步容器内mysql数据
sobase_redis_vol: # 定义数据卷同步redis容器内数据
sobase_media_vol: # 定义数据卷同步media文件夹数据
services:
portainer:
image: portainer/portainer
container_name: portainer
restart: always # always表容器运行发生错误时一直重启
ports:
- "9000:9000"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
redis:
image: redis:6.2.5
container_name: redis
command: redis-server /etc/redis/redis.conf # 容器启动后启动redis服务器
volumes:
- sobase_redis_vol:/data # 通过挂载给redis数据备份
- ./compose/redis/redis.conf:/etc/redis/redis.conf # 挂载redis配置文件
ports:
- "6379:6379"
restart: always # always表容器运行发生错误时一直重启
db:
image: mysql:5.7
container_name: mysql
environment:
- MYSQL_ROOT_PASSWORD=p@ssw0rdwcx # 数据库密码
- MYSQL_DATABASE=sobase # 数据库名称
- MYSQL_ROOT_USER=root # 数据库用户名
- MYSQL_PASSWORD=p@ssw0rdwcx # 用户密码
volumes:
- sobase_db_vol:/var/lib/mysql:rw # 挂载数据库数据, 可读可写
- ./compose/mysql/conf/my.cnf:/etc/mysql/my.cnf # 挂载配置文件
- ./compose/mysql/init:/docker-entrypoint-initdb.d/ # 挂载数据初始化sql脚本
ports:
- "3306:3306" # 与配置文件保持一致
restart: always
web:
build: ./ # 使用myproject目录下的Dockerfile
container_name: web
expose:
- "8000"
volumes:
- ./:/var/www/html/SOBase # 挂载项目代码
- sobase_media_vol:/var/www/html/SOBase/media # 以数据卷挂载容器内用户上传媒体文件
- ./compose/uwsgi:/tmp # 挂载uwsgi日志
links:
- db
- redis
depends_on: # 依赖关系
- db
- redis
environment:
- DEBUG=False
restart: always
tty: true
stdin_open: true
#entrypoint: [ "/bin/sh","./start.sh" ]
nginx:
build: ./compose/nginx
container_name: nginx
ports:
- "80:80"
- "443:443"
expose:
- "80"
volumes:
- ./static:/usr/share/nginx/html/static # 挂载静态文件
- ./compose/nginx/ssl:/usr/share/nginx/ssl # 挂载ssl证书目录
- ./compose/nginx/log:/var/log/nginx # 挂载日志
- sobase_media_vol:/usr/share/nginx/html/media # 挂载用户上传媒体文件
links:
- web
depends_on:
- web
restart: always
# 进入docker-compose.yml所在文件夹,输入以下命令构建镜像
sudo docker-compose build
# 查看已生成的镜像
sudo docker images
# 启动容器组服务
sudo docker-compose up
# 查看运行中的容器
sudo docker ps