docker解决django+uwsgi问题.nginx的配置可以再拉一个镜像或用本机安装的nginx转发即可.
Docker构建:
docker build -t wp-api .
启动镜像
docker run -t -i -p 8001:9020 --sysctl net.core.somaxconn=4096 wp-api-test
- -t -i 伪终端模式,调试问题用.调试完成后可以替换为-d 后台运行
- -p 端口映射,uwsgi配置的端口是9020
- --sysctl net.core.somaxconn=4096 修改docker系统的参数 uwsgi的listen大于128时,会报错.
项目文件结构:
wp-api
├── uwsgi.ini
├── templates
├── static
├── start.sh
├── requirements.txt
├── pip.conf
├── mypy.ini
├── manage.py
├── WorkPlatFormApi
│ ├── wsgi.py
│ ├── urls.py
│ ├── settings_ldap.py
│ ├── settings.py
│ ├── celery.py
│ ├── __init__.py
├── UserApp
│ ├── views.py
│ ├── urls.py
│ ├── tests.py
│ ├── models.py
│ ├── apps.py
│ ├── admin.py
│ ├── __init__.py
├── README.md
└── Dockerfile
Dockfile:
#当你写下FROM centos:7的时候,你就要想着,在这以后的每一步操作都是在centos 7系统镜像中进行的操作,
#你以前是怎么部署应用的,那么请按照你以前的步骤一步一步来就好。
FROM ubuntu:16.04
FROM python:3.7-stretch
#声明镜像制作者
MAINTAINER Hero<wanghua.zhou@leyantech.com>
USER root
# 拷贝当前目录到镜像中
WORKDIR /root/
COPY . /root/wp-api/
# 拷贝 pip 配置文件
COPY ./pip.conf .pip/pip.conf
# 安装python3.7必要的包,以及一些常用工具
RUN apt-get update
RUN apt-get install -y vim wget htop software-properties-common
# 安装语言包
RUN apt-get install -y locales
# Ensure that we always use UTF-8 and with Canadian English locale
RUN locale-gen en_CA.UTF-8
## 安装python3开发环境
RUN apt-get update
RUN apt-get install -y python3-dev
RUN apt-get install libevent-dev
RUN apt-get install libsasl2-dev
RUN apt-get install libldap2-dev
RUN apt-get install -y libssl-dev
# 安装 django uwsgi 等项目依赖
WORKDIR /root/wp-api
RUN apt-get install -y uwsgi
RUN apt-get install -y gcc build-essential make
RUN pip3 install -r requirements.txt
# print()时在控制台显示中文
ENV PYTHONIOENCODING=utf-8
# support Chinese
ENV LC_ALL=en_CA.UTF-8
ENV LANG=en_CA.UTF-8
ENV LANGUAGE=en_CA.UTF-8
# 解决运行shell的权限问题
RUN chmod 777 ./start.sh
CMD ./start.sh
start.sh
#!/bin/sh
uwsgi --ini uwsgi.ini
uwsgi.ini
[uwsgi]
http = :9020
socket = uwsgi.sock
chdir =/root/wp-api
wsgi-file = WorkPlatFormApi/wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9195
vacuum = true
chmod-socket = 666
enable-threads = true
listen = 1024