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

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

最新文章

  1. 《Java程序设计》第五次实验实验报告
  2. java内省机制Introspector
  3. 免费的WebService
  4. [Bootstrap] 8. &#39;Collapse&#39;, data-target, data-toggle &amp; data-parent
  5. storm的设计思想
  6. 【HDOJ】3234 Exclusive-OR
  7. action 关联
  8. 一张图片快速明白Python概述
  9. GoLang-Rpc编程
  10. python django(forms组件)
  11. php7编译安装-php-fpm.service
  12. Shell脚本中变量和函数变量的作用域
  13. 远程桌面连接一台关联无线的电脑(A)时,A电脑无线总是断开导致远程桌面连接失败
  14. Linux基础学习(3)--初学注意
  15. Windows定时开机并开启工作软件
  16. 第三周(JAVA编写的 wordcount)
  17. python 生成图表
  18. 为你的网站加上SSL,可以使用HTTPS进行访问
  19. 源码分析五(HashSet的内部实现)
  20. VBA文本型数字变成数值

热门文章

  1. MySQL8.0性能优化(实践)
  2. StringBuilder的原理-append方法
  3. 用XAMPP搭建本地:Web服务器,访问服务器,下载服务器。
  4. spring cloud alibaba sentinel 运行及简单使用
  5. JS 计算两个时间戳相差年月日时分秒
  6. aspnetcore 原生 DI 实现基于 key 的服务获取
  7. [NOIP2018 提高组] 保卫王国
  8. JZOJ 2020.08.03【NOIP提高组】模拟 &amp;&amp;【NOIP2015模拟11.5】
  9. JavaSE 对象与类(二)
  10. 病程极短(≤16周)的495例未分化关节炎患者随访2年的结局[EULAR2015_SAT0055]