项目目录
Dockerfile:
FROM python:3.6
# 工作目录
WORKDIR /home/
# apt的换源
RUN echo "deb http://mirrors.aliyun.com/debian/ buster main non-free contrib" > /etc/apt/sources.list
RUN echo "deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/debian-security buster/updates main" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.aliyun.com/debian-security buster/updates main" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib" >> /etc/apt/sources.list
# 更新apt
RUN apt-get update -y
# 安装nginx
RUN apt-get install -y nginx
# pip换源
# 创建源pip文件夹
RUN mkdir -p ~/.pip/
RUN echo "[global]" > ~/.pip/pip.conf
RUN echo "index-url = https://pypi.tuna.tsinghua.edu.cn/simple" >> ~/.pip/pip.conf
# 将项目放入镜像的home目录下
ADD xxx/ /home/xxx/
# 配置nginx, 将项目中的编写的mynginx文件移动到 /etc/nginx/nginx.conf
RUN mv /home/xxx/mynignx /etc/nginx/nginx.conf
# 安装项目依赖
RUN pip install -r /home/xxx/requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
# 暴露80端口,nginx需要转接
EXPOSE 80
# 启动nginx和uwsgi
CMD nginx && uwsgi -x /home/xxx/uwsgi.xml
mynginx
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80; # 转接80端口的请求
server_name 0.0.0.0:8888; # 本地服务端口
charset utf-//8;
location / {
include uwsgi_params;
uwsgi_pass 0.0.0.0:8888; # 端口要和uwsgi里配置的一样
uwsgi_param UWSGI_SCRIPT xxx.wsgi; #wsgi.py所在的目录名+.wsgi
uwsgi_param UWSGI_CHDIR /home/xxx/; # 项目路径
}
}
}
uwsgi.xml
<uwsgi>
<socket>0.0.0.0:8888</socket> <!-- 内部端口,自定义 -->
<chdir>/home/xxx/</chdir> <!-- 项目路径 -->
<module>xxx.wsgi</module> <!-- test_site为wsgi.py所在目录名-->
<processes>4</processes> <!-- 进程数 -->
</uwsgi>
执行命令:
docker build .
docker run -itd -p 8885:80 镜像ID