
本文主要介绍Docker对pythonFlask+nginx+uwsgi容器的构造,通过示例代码进行了非常详细的介绍,对大家的学习或工作有一定的参考价值。有需要的朋友就跟着下面的边肖学习吧。
安装Nginx
首先,拉下centos镜像docker拉centos
我们安装了最新版本的nginx1.19:下载地址
运行centos映像并输入:
把这袋nginx-1.19.0.tar.gz放进容器里:
dockerCPnginx-1.19.0.tar.gz10e87af84c05:/root(10e87af84c05是centos容器id)
在安装nginx之前安装一些依赖项:
解压缩:
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-develOpenSSL-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.此时,映像中有我们安装的NGs。
安装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
安装PIPUWSGI时将报告错误:
plugins/python/uwsgi_python.h:2:20:致命错误:python.h:没有这样的文件或目录
#include<;Python.h>
运行yuminstallpython-devel.x86_64解决,重新pipinstall下载。
配置uWSGI服务器
相关的uwsgi.ini文件如下:
[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客户端能够访问这个Unix套接字文件,这里配置为777。
Callable:设置收到请求时将调用uwsgi加载的模块中的哪个变量。默认值是名为“应用程序”的变量。
加载指定的Wsgi文件。
Buffer-size:设置用于uwsgi包解析的内部缓冲区大小。默认是4k。
Processes和threads分别是打开的进程和线程的数量,%k是幻数变量,代表CPU核心的数量。如果我们是双核CPU,
那么这里的进程和线程分别是4和40,也就是有4个进程,每个进程有40个线程。
安装监控器(可选)
直接yum安装将报告没有可用的程序包管理程序的错误。那是因为CentOS是由RedHat企业版编译的,所有关于版权问题的内容都被去掉了。只需要执行yuminstallepel-release即可解决。安装后,将生成以下目录:
现在,我们将配置监控器,以便监控器监听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以上路径都是实际的目录配置,如果不一样,需要更改。
然后启动主管:
完成上述配置后,我们重新打包容器以生成一个新的映像,命名为base_v3。我们为打包Docker应用程序编写了一个Docker文件:
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或在上添加一个入口点nginx-g"守护进程&&uwsgi-ini/app/uwsgi.ini
然后运行一个接口测试:
关于Docker构建pythonFlask+nginx+uwsgi容器的这篇文章到此为止。更多关于DockerbuildingFlask+nginx+uwSGI的信息,请搜索我们之前的文章或者继续浏览下面的相关文章。希望大家以后能多多支持我们!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)