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

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

docker部署flask+uwsgi+nginx+postgresql,解耦拆分到多个容器,实现多容器互访的更多相关文章

  1. 阿里云部署 Flask + uWSGI + Nginx

    一.引言 今天入手了一台阿里云服务器,是centeros 7.5版本.本文解决的是 Flask 的部署问题.假设你的Flask的应用已经完成,现在只是部署的问题,本文以部署我的二次开发微信订阅号的项目 ...

  2. 基于docker部署flask+gunicorn+nginx

    nginx安装在/etc/下,项目映射在docker中的/var/www/下 1.创建docker容器将端口映射出来,将docker外的项目映射到docker中 #docker run -it -p ...

  3. python部署-Flask+uwsgi+Nginx

    一.Flask部分(app.py) flask即Python代码:部分参考代码如下,相信很多人如果看到这篇文章一定有flask的代码能力. from app import create_app fro ...

  4. 用docker部署flask+gunicorn+nginx

    说来惭愧,写了好几个flask django项目都是在原型阶段直接python app.py 运行的,涉及到部署用nginx和gunicorn 都是让别人帮我部署的,据说好像说很麻烦的样子,我就没自己 ...

  5. 写给新手看的Flask+uwsgi+Nginx+Ubuntu部署教程

    学习 Flask,写完一个 Flask 应用需要部署的时候,就想着折腾自己的服务器.根据搜索的教程照做,对于原理一知半解,磕磕碰碰,只要运行起来了,谢天谢地然后不再折腾了,到下一次还需要部署时,这样的 ...

  6. Flask+uwsgi+Nginx+Ubuntu部署

    学了一段时间flask,可是一直没有做过部署, 于是想着怎么部署呢, 想想,先吧服务给搞通吧,于是呢 就先想着去吧服务给搞起来,这里选择的是Flask+uwsgi+Nginx+Ubuntu, Pyth ...

  7. Flask+uwsgi+Nginx+Ubuntu部署教程

    学习 Flask,写完一个 Flask 应用需要部署的时候,就想着折腾自己的服务器.根据搜索的教程照做,对于原理一知半解,磕磕碰碰,只要运行起来了,谢天谢地然后不再折腾了,到下一次还需要部署时,这样的 ...

  8. 阿里云部署 Flask + WSGI + Nginx 详解

    抵不住朋友的诱惑,今天终于入手了一台阿里云服务器,是Ubuntu 1.4 32位版本,最初考虑是用来尝尝鲜只是买了个最低配的,价格算起来与在国外买个空间的价格相当吧(可能一年才贵100多),但用起来感 ...

  9. flask+uwsgi+nginx+docker-compose部署

    简单介绍 Flask这里就不多阐述了,已经是很流行的一个轻量级python框架了,对于小.中型项目特别适合.这里用docker的compose编排部署.uwsgi 简单的说明下,uWSGI是一个Web ...

  10. 使用Flask+uwsgi+Nginx部署Flask正式环境

    环境准备 在开始正式讲解之前,我们将首先进行环境准备. Step1:安装Python,pip以及nginx: sudo apt-get update sudo apt-get install pyth ...

随机推荐

  1. JavaScript 文件上传

    一.普通文件上传 JavaScript 可以使用表单提交来实现文件上传.首先,在 HTML 中创建一个文件输入框: <input type="file" id="f ...

  2. java入门与进阶-P1.5+P1.6

    输入 Scanner输入语句介绍 java.util.Scanner 是 Java5 的新特征,我们可以通过 Scanner 类来获取用户的输入. 下面是创建 Scanner 对象的基本语法: Sca ...

  3. 创建型模式 - 原型模式Prototype

    孩子生来没娘的NT审核机制,又开始说我涉及到广告了,我涉及到什么广告了?我接着发. 学习而来,代码是自己敲的.也有些自己的理解在里边,有问题希望大家指出. 所属:创建型模式  原型模式 一般会和 工场 ...

  4. CAN2-CH32V307CAN2使用说明与CAN波特率计算方法

    一.修改引脚 CH32V307CAN2的TX为PB13,RX为PB12 注意用CAN2时需要初始化CAN1的时钟. 二.配置CAN2过滤器开始的组(组号与图24-4相对应) 三.将FIFO0改为FIF ...

  5. JSP第五次作业

    1.教材P78-79  例4-9 1 <%@ page language="java" import="java.util.*" pageEncoding ...

  6. MySQL-字段约束条件

    1.无符号.零填充 1.unsigned:用在生成表的过程中,表示不取负数,只取正数和0,负数会直接报错,eg:id int unsigned. 2.zerofill:用在生成表的过程中,跟在整形2后 ...

  7. 记一次失败的StackOverflow回答

    有一位同学在StackOverflow上提问,他想创建一个 Future 类,异步的实现 Future 的构造,当构造完成之后自动调用 .then 方法,执行后面的逻辑 class Features ...

  8. Creo9.0 安装破解图文教程 【2022年12月29日亲测有效】

    前言 creo9.0正式版是一款非常优秀的3D建模设计软件.该软件界面美观,提供了CAD 技术.模制造绘图.多实体建模.多体设计.实时仿真.框架和焊缝设计等一系列强大的辅助设计功能,通过这些功能,让用 ...

  9. C++并发-同步并发

    1.等待事件 std::mutex m; void wait() { std::unique_lock<std::mutex> lk(m); lk.unlock(); std::this_ ...

  10. 【Vue】vue项目目录介绍 es6的导入导出语法 vue项目开发规范 Vue项目编写步骤

    目录 昨日回顾 今日内容 0 vue-cli创建项目 node.js环境 创建vue-cli项目 1 vue项目目录介绍 node_modules index.html app.vue package ...