docker封装Django+uwsgi

拉取镜像

1
docker pull centos

准备容器环境

在容器中操作

更新yum

1
yum update

安装环境

1
2
3
yum install -y python36 python36-devel gcc libevent-devel openssl-devel  libffi-devel wget

pip3 install uwsgi gevent

安装uwsgi

1
pip3 install uwsgi gevent

复制文件到容器

1
docker cp 本地路径 容器id:容器路径

安装依赖

pip3 install -r requirements.txt –trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple

安装nginx

安装编译工具及库文件

1
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

安装 PCRE

PCRE 作用是让 Nginx 支持 Rewrite 功能

下载

1
2
cd /usr/local/src/
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

解压

1
tar zxvf pcre-8.35.tar.gz

编译安装

1
2
3
cd pcre-8.35
./configure
make && make install

查看PCRE版本

1
pcre-config --version

安装nginx

安装

1
2
3
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
1
2
./configure
make && make install

启动nginx

1
2
cd /usr/local/nginx/sbin  #nginx安装目录
./nginx

查看是否启动

1
ps -aux | grep nginx

修改配置文件

1
vi /usr/local/nginx/conf/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
server
{
listen 80;
listen 443 ssl http2;
server_name 你的域名;
charset urf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001; #8001对应uwsgi配置中的socket
}
}

刷新配置文件

1
./nginx -s reload

报错处理

1删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
make -f objs/Makefile
make[1]: Entering directory '/usr/local/src/nginx-1.6.2'
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I /usr/local/src/pcre-8.35 -I objs \
-o objs/src/core/ngx_murmurhash.o \
src/core/ngx_murmurhash.c
src/core/ngx_murmurhash.c: In function 'ngx_murmur_hash2':
src/core/ngx_murmurhash.c:37:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
h ^= data[2] << 16;
~~^~~~~~~~~~~~~~~~
src/core/ngx_murmurhash.c:38:5: note: here
case 2:
^~~~
src/core/ngx_murmurhash.c:39:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
h ^= data[1] << 8;
~~^~~~~~~~~~~~~~~
src/core/ngx_murmurhash.c:40:5: note: here
case 1:
^~~~
cc1: all warnings being treated as errors
make[1]: *** [objs/Makefile:446: objs/src/core/ngx_murmurhash.o] Error 1
make[1]: Leaving directory '/usr/local/src/nginx-1.6.2'
make: *** [Makefile:8: build] Error 2

解决:进入到nginx-1.6.2目录下(解压的目录)

找到当前目录下找到objs文件夹,并进入,打开文件Makefile,找到有一下内容的这行:

1
2
vi objs/Makefile
CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g 
gcc将所有的警告当成错误进行处理```把这行内容中的 “-Werror”去掉
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#### 2注释

```shell
sh-4.4# make && make install
make -f objs/Makefile
make[1]: Entering directory '/usr/local/src/nginx-1.6.2'
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I /usr/local/src/pcre-8.35 -I objs \
-o objs/src/os/unix/ngx_user.o \
src/os/unix/ngx_user.c
src/os/unix/ngx_user.c: In function 'ngx_libc_crypt':
src/os/unix/ngx_user.c:35:7: error: 'struct crypt_data' has no member named 'current_salt'
cd.current_salt[0] = ~salt[0];
^
make[1]: *** [objs/Makefile:726: objs/src/os/unix/ngx_user.o] Error 1
make[1]: Leaving directory '/usr/local/src/nginx-1.6.2'
make: *** [Makefile:8: build] Error 2

解决:

进入到对应文件把报错行注释掉

编写运行脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash

cd /usr/local/nginx/sbin
./nginx # 启动nginx进程

# 确认ngixn启动成功
ps -aux | grep nginx

#启动uwsgi确认
uwsgi -i /uwsgi.ini
netstat -tunlp

#tail 输出日志持续化
tail -f /tmp/uwsgi.log

提交容器

1
docker commit 容器id 镜像名

运行

docker run -p 80:80 容器id ./go.sh

访问ip

?问题

  1. 执行django并暴露端口可以使用,uwsgi无效

    解决:方案 使用tail命令对日志进行持续输出 保证在前台运行

  2. 保存docker镜像到本地

    1
    docker save 镜像id>本地路径\xxx.tar
  3. 导入docker镜像

    1
    docker load < xxx.tar

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!