Docker构建python Flask+ nginx+uwsgi容器
安装Nginx
首先拉下centos镜像dockerpullcentos
我们安装最新的nginx1.19版本:下载地址
将centos镜像运行起来并进入:
dockerrun--namever-d-p8051:80-itnginx_start
将nginx-1.19.0.tar.gz这个包放入容器里面:
dockercpnginx-1.19.0.tar.gz10e87af84c05:/root(10e87af84c05为centos容器id)
安装nginx前先装一些依赖:
yum-yinstallgccgcc-c++autoconfautomakemake yum-yinstallzlibzlib-developensslopenssl-develpcrepcre-devel
解压:
tar-zxvfnginx-1.19.0.tar.gz
#进入到nginx-1.10.1,并配置nginx cdnginx-1.19.0 #配置nginx #--prefix指定安装的目录 #/usr/local/nginx是安装目录,不能和自己下载的文件目录重了 #./configure--prefix=/usr/local/nginx #带sslstub_status模块添加strem模块–with-stream,这样就能传输tcp协议了 #http_stub_status_module状态监控 #http_ssl_module配置https #stream配置tcp得转发 #http_gzip_static_module压缩 #http_sub_module替换请求 ./configure--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module--with-stream
注:
在这里我出现了pcre和zlib缺失的错,可以使用yum-yinstallzlibzlib-developensslopenssl--develpcrepcre-devel命令,安装所有依赖。
再make&makeinstall进行编译安装
安装成功后,在./configure--prefix=/usr/local/nginx指定目录会生成四个文件,我们也只需要输入/usr/local/nginx/sbin/nginx来启动nginx服务即可。
要验证是否成功,可以输入curllocalhost来查看是否启动成功。
生成镜像
10.将装有nginx的centos容器打包为镜像dockercommitba5ba0d81912nginx_centos(ba5ba0d81912为容器ID,重命名为nginx_centos)
11.重新运行新的镜像:dockerrun--namever-d-p8051:80-itnginx_centos
12.而此时的镜像,则是有我们安装好的nginx,我们就可以拿他开始为所欲为,做一些其他的骚操作了。
安装python2.7环境
yuminstallgccopenssl-develbzip2-devel
用wget下载python2.7并解压
yum-yinstallwget
进入目录/usr/src再用wget下载python2.7
cd/usr/src wgethttps://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz
再解压python2.7
tar-zxvfPython-2.7.15.tgz
安装python2.7
进入上面解压的Python-2.7.15解压文件中使用下面命令行安装
cdPython-2.7.15 ./configure--enable-optimizations makealtinstall
安装PIP
curl"https://bootstrap.pypa.io/get-pip.py"-o"get-pip.py" python2.7get-pip.py
因为版本为2.7,且requirements.txt里面有一个MYSQL-python的库,会报一个找不到libmysqlclient-dev的错,执行yuminstallmysql-devel即可解决。
安装UWSGI
pipinstalluwsgi的时候会报一个错:
plugins/python/uwsgi_python.h:2:20:fatalerror:Python.h:Nosuchfileordirectory
#include
[uwsgi] socket=/tmp/uwsgi.sock chown-socket=nginx:nginx chmod-socket=664 #GracefulshutdownonSIGTERM,seehttps://github.com/unbit/uwsgi/issues/849#issuecomment-118869386 hook-master-start=unix_signal:15gracefully_kill_them_all
在项目目录下/app/创建uwsgi.ini文件:
[uwsgi] uwsgi-socket=/tmp/uwsgi.sock chmod-socket=777 callable=app wsgi-file=main.py buffer-size=65535 processes=%(%k*2) threads=%(%k*20
其中每个参数的意思:
uwsgi-socket:将uwsgi-socket这个配置项指定了一个文件,这个文件是Unix套接字,即通过文件系统
(而非网络地址)进行寻址和访问的套接字。配置uwsgi-socket之后,还需要配置chmod-socket,
Unixsocket是个文件,所以会受到Unix系统的权限限制,可以配置成660或者777,
使得uwsgi客户端能够访问这个Unixsocket文件,这里配置为777。
callable:设置在收到请求时,uwsgi加载的模块中哪个变量将被调用,默认是名字为“application”的变量。
wsgi-file:加载指定的wsgi文件。
buffer-size:设置用于uwsgi包解析的内部缓存区大小。默认是4k。
processes和threads,分别是开启的进程数和线程数,而%k是魔数变量,代表CPU核数,如果我们是双核CPU,
那这里的processes和threads分别为4和40,即有4个进程,每个进程有40个线程。
安装Supervisor(可选)
直接yum安装会报一个Nopackagesupervisoravailable.的错误,那是因为CentOS是RedHat企业版编译过来的,去掉了所有关于版权问题的东西。只需要执行yuminstallepel-release即可解决。安装好后会生成如下目录:
现在我们将配置supervisor,使得supervisor监听nginx和uwsgi服务。
首先在/etc目录下创建supervisor文件,然后创建supervisord.conf文件和conf.d目录:
supervisord.conf目录配置如下:
;supervisorconfigfile [unix_http_server] file=/var/run/supervisor/supervisor.sock;(thepathtothesocketfile) chmod=0700;sockeffilemode(default0700) [supervisord] logfile=/var/log/supervisor/supervisord.log;(mainlogfile;default$CWD/supervisord.log) pidfile=/var/run/supervisord.pid;(supervisordpidfile;defaultsupervisord.pid) childlogdir=/var/log/supervisor;('AUTO'childlogdir,default$TEMP) ;thebelowsectionmustremainintheconfigfileforRPC ;(supervisorctl/webinterface)towork,additionalinterfacesmaybe ;addedbydefiningtheminseparaterpcinterface:sections [rpcinterface:supervisor] supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///var/run/supervisor.sock;useaunix://URLforaunixsocket ;The[include]sectioncanjustcontainthe"files"setting.This ;settingcanlistmultiplefiles(separatedbywhitespaceor ;newlines).Itcanalsocontainwildcards.Thefilenamesare ;interpretedasrelativetothisfile.Includedfiles*cannot* ;includefilesthemselves. [include] files=/etc/supervisor/conf.d/*.conf
再在conf.d目录下创建supervisord.conf文件并编辑:
[supervisord] nodaemon=true [program:uwsgi] command=/usr/bin/uwsgi--ini/etc/uwsgi/uwsgi.ini--die-on-term--need-app stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr stderr_logfile_maxbytes=0 [program:nginx] command=/usr/local/nginx/sbin/nginx stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr stderr_logfile_maxbytes=0 #Gracefulstop,seehttp://nginx.org/en/docs/control.html stopsignal=QUIT
以上路径均为实际目录配置,如果有不一样则需要更改。
然后将supervisor启动:
以上配置弄好后,我们将容器重新打包生成一个新的镜像,记为base_v3,我们写一个打包docker应用的Dockerfile:
FROMbase_v3 #创建工作路径 RUNmkdir/app #指定容器启动时执行的命令都在app目录下执行 WORKDIR/app #替换nginx的配置 COPYnginx.conf/etc/nginx/nginx.conf #将本地app目录下的内容拷贝到容器的app目录下 COPY./app//app/
这里,在Dockerfile和app同级目录下,再建立一个nginx.conf文件,并将nginx.conf内容修改如下:
usernginx; worker_processes1; error_log/usr/local/nginx/logs/error.logwarn; pid/usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile20480; events{ useepoll; worker_connections20480; multi_accepton; } http{ include/usr/local/nginx/conf/mime.types; default_typeapplication/octet-stream; log_formatmain'$remote_addr-$remote_user[$time_local]"$request"' '$status$body_bytes_sent"$http_referer"' '"$http_user_agent""$http_x_forwarded_for"'; #请求量级大建议关闭acccess_log #access_log/var/log/nginx/access.logmain; sendfileon; #tcp_nopushon; keepalive_timeout300s; client_header_timeout300s; client_body_timeout300s; gzipon; gzip_min_length1k; gzip_buffers416k; gzip_typestext/htmlapplication/javascriptapplication/json; include/usr/local/nginx/conf.d/*.conf; server{ listen6666; charsetutf-8; client_max_body_size75M; location/{ includeuwsgi_params; uwsgi_passunix:///tmp/uwsgi.sock; uwsgi_send_timeout300; uwsgi_connect_timeout300; uwsgi_read_timeout300; } } }
接下来只需要dockerbuild-tnew_project.并dockerrun--nametest-d-p8055:6666-v/root/web/mim_backend/data:/app/static-v/root/logs/mim_backend:/app/log-itnew_project即可。
当然,这个时候进去nginx和uwsgi没有自动启动,需要手动拉起来,如想自动拉起服务,可选用supervisor或者在dockerfile里面加一个ENTRYPOINTnginx-g"daemonon;"&&uwsgi--ini/app/uwsgi.ini
然后随便跑一个接口测试:
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。