docker部署flask+uwsgi+nginx+postgresql,解耦拆分到多个容器,实现多容器互访

 

本人承诺,本博客内容,亲测有效。

dockerfile文件,

FROM centos:7

RUN mkdir /opt/flask_app

COPY ./show_data_from_jira /opt/flask_app/show_data_from_jira

RUN mkdir /opt/flask_app/show_data_from_jira/uwsgi_log
COPY ./dist /opt/flask_app/dist
WORKDIR /opt/flask_app

RUN yum makecache

RUN yum -y install python3

RUN yum -y install gcc && yum -y install python36-devel

RUN pip3 install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/simple

RUN yum -y install epel-release 
RUN yum -y install nginx
RUN yum -y install vim

RUN pip3 install -r ./show_data_from_jira/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

RUN yum clean all

RUN mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf-bak
COPY .nginx.conf /etc/nginx/nginx.conf

EXPOSE 8080

#ENTRYPOINT uwsgi --ini /opt/flask_app/show_data_from_jira/config.ini && nginx -g "daemon off;"  写绝对路径也行,
#workdir相对路径也行
ENTRYPOINT uwsgi --ini ./show_data_from_jira/config.ini && nginx -g "daemon off;"

 dockerfile文件最后一行是容器的启动命令。容器就相当于一个process,如果这个process运行结束,那么容器也会自动existed。当dockerfile生成镜像之后,用这个dockerfile生成的镜像来run容器,得到的容器如果不停地existed,每每start之后,就立即existed,请注意,多半是这里dockerfile启动容器命令有问题。这是我整个过程中,最大的坑,因为本身对于docker理解不够,这是盲区。

 

uwsgi配置文件,

[uwsgi]
# port
#http = 0.0.0.0:8001
http = 0.0.0.0:8090
# project dir path
chdir = /opt/flask_app/show_data_from_jira
#chdir = /opt/flask_app
# wsgi file
module = manage:app
# processes count
processes = 4
# main thread
master = true
# each process got 2 threads
threads = 2
# save log files
# logto = %(chdir)/uwsgi_log/uwsgi.log # 这是另一种日志路径写法,单纯设置日志保存路径而已。
# 下面daemonize参数,有两层意思,设置日志保存路径,同时让uwsgi后台运行。如果要把后台代码拆分到单独的容器中,那么,该容器的uwsgi就是唯一的进程,
# 就不能后台运行,否则,容器会自动existed,因为没有前台进程在运行。 daemonize = %(chdir)/uwsgi_log/uwsgi.log
# save main process number pidfile = %(chdir)/uwsgi_log/uwsgi.pid # log file max size of each log file log-maxsize = 100000 # start request log disable-logging = true # set broken connect timeout harakiri = 60 # fix lazy load lazy = true lazy-apps = true

 

nginx配置文件,

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       8080;
        listen       [::]:80;
        server_name  localhost;
        root         /usr/share/nginx/html;
        charset utf-8;
        gzip on;
        location / {
            root /opt/flask_app/dist;
            autoindex on;
            try_files $uri $uri/ /index.html;
            index index.php index.html index.htm;
        }
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        location /api/{
            proxy_pass http://127.0.0.1:8090;  # 可以替换成容器IP+容器开放端口
            proxy_send_timeout 600;
            proxy_read_timeout 120;
            proxy_connect_timeout 90;
        }

        location /static/{
            root /opt/flask_app/dist;
            expires 30d;
            autoindex on;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

 

数据库配置,

POSTGRES_USER = "postgres"
POSTGRES_PWD = ""
#POSTGRES_HOST = "localhost"
POSTGRES_HOST = "172.17.0.5"  # 这里的IP就是要访问的另一个容器的虚拟IP地址,我的多个容器都是在本地运行,容器自动会分配一个虚拟IP,
#容器之间互相访问,就用虚拟IP+容器开放端口即可(开放端口就是“-p 宿主机端口:容器开放端口”,也是dockerfile “expose 参数值)”
POSTGRES_PORT = 5432 POSTGRES_DB = "jira_test"

 

后端代码目录结构,

 

 

requirements文件内容,根据我这里贴出来的目录结构,requirements.txt文件是在后端代码里面的,不需要在dockerfile里面拷贝。

Flask==2.0.3
Flask_Cors==3.0.10
Flask_WTF==1.0.1
pandas==1.4.3
psycopg2==2.9.3
psycopg2_binary==2.7.4
SQLAlchemy==1.4.39

 

这里是postgresql安装教程,我自己就是按照这个教程一步一步安装的,我选择的是源码安装,用wget下载tar包,手动一步步操作的。

 数据库安装教程

 

下面这些都是整个docker部署过程中,遇到的一些bug的总结:

centos8版本,从2021年开始红帽公司不再维护其镜像源,国内应该有替代的镜像源,需要找教程配置。我直接用的centos:7,这一版还能正常用。

把文件从宿主机拷贝到容器里面:docker cp /root/lmj-work/test.txt containerID:/root/test;


把文件从容器里面拷贝到宿主机:docker cp containerID:/root/test/test.txt /root/lmj-work/


用容器“2c6e8675a377”反向生成镜像,docker commit deploy1 deploy:1; docker run -d -p 8002:8080 --name flask_uwsgi_nginx_v1 flask-uwsgi-nginx:v72 docker logs flask_uwsgi_nginx_v1 docker inspect flask_uwsgi_nginx_v1 netstat -AaLlnW|grep 8002 awk 'NR>35 && NR<55 {print $0}' /etc/nginx/nginx.conf sed -n '35,55p' /etc/nginx/nginx.conf # docker nginx 日志存放地址:/var/log/nginx nginx配置文件:/etc/nginx/nginx.conf
#容器安装psycopg2失败,要安装下面这个包
pip3 install psycopg2-binary==2.7.4
# 'ascii' codec can't decode byte 0xe8 in position 0: ordinal not in range(128) 数据库报错报错,编码错误,修改数据库编码命令如下 sudo -u postgres psql -c "SHOW SERVER_ENCODING" jira_test update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = 'jira_test';

su postgres  password:123; su root  password:centos

client-postgresql:sql -h localhost -p port -u username

postgresql数据库,数据导入导出:
a、copy tb_pdc_rel to '/Users/dream-mac/Desktop/南天-work/开发跟踪矩阵/导出数据-项目搭建测试数据-lmj/tb_pdc_rel_export.csv' delimiter ',' csv header;
b、copy tb_pdc_rel from '/home/postgres/tb_pdc_rel.csv' delimiter ',' csv header;

 

centos容器安装uwsgi报错:

#include <Python.h>
                        ^
    compilation terminated.
    ----------------------------------------
ERROR: Command errored out with exit status 1
solution[#] yum install gcc && yum install python36-devel && pip3 install uwsgi

 

posted @ 2022-07-22 17:59  dream-子皿  阅读(440)  评论(0)    收藏  举报