Ubuntu 18.04部署docker-compose+nginx+uwsgi+flask

4 篇文章 0 订阅
3 篇文章 0 订阅

flask–web框架
nginx–web服务器
uwsgi–WSGI协议
大致流程:http访问80端口,nginx监听,然后nginx socket通信8080端口到flask容器中的uwsgi,uwsgi启动flask主程序.
两个docker容器,一个flask一个nginx,docker-compose管理
在这里插入图片描述

1.文件目录
在这里插入图片描述
2.docker-compose.yml

version: "3.7"

services:

  flask:
    build: ./flask           # 指向相关镜像的Dockerfile所在目录
    container_name: flask
    restart: always
    environment:             # 配置容器的环境变量
      - APP_NAME=MyFlaskApp
    expose:                  # 将该容器的8080端口开放给同一网络下的其他容器和服务
      - 8080

  nginx:
    build: ./nginx
    container_name: nginx
    restart: always
    ports:                   # HOST:CONTAINER 将主机的80端口映射到容器的80端口,相当于将nginx容器的80端口开放给外部网络
      - "80:80"              # 这里修改host端口就可以改变外网访问的端口

3.flask-Dockfile

# Use the Python3.6 image
# 使用python 3.6作为基础镜像
FROM python:3.6

# Set the working directory to /app
# 设置工作目录,作用是启动容器后直接进入的目录名称
WORKDIR /app

# Copy the current directory contents into the container at /app
# . 表示和Dockerfile同级的目录
# 该句将当前目录下的文件复制到docker镜像的/app目录中
ADD . /app

# Install the dependencies
# 安装相关依赖
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

# run the command to start uWSGI
# 容器启动后要执行的命令 -> 启动uWSGI服务器
CMD ["uwsgi", "uwsgi.ini"]
# CMD ["uwsgi", "--ini", "/flask/uwsgi.ini"] #初始化的启动操作

4.flask-uwsgi.ini

[uwsgi]
wsgi-file = run.py
callable = app
socket = :8080
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
die-on-term = true

5.flask-pgsql_ini.json 是我的数据库配置文件
flask-requirements.txt 是pip freeze >requeirements.txt导出的依赖包
flask-run.py是我flask的程序

# 实例flask
app=Flask(__name__)
# 主页面
@app.route("/")
def zhuye():
  return '接收数据传入pgsql数据库'

6.nginx-Dockfile

# Use the Nginx image
# 使用Nginx镜像
FROM nginx

# Remove the default nginx.conf
# 移除官方的配置文件, 并换为自己的
RUN rm /etc/nginx/conf.d/default.conf

# Replace with our own nginx.conf
COPY nginx.conf /etc/nginx/conf.d/

7.nginx-nginx.conf

server {
    listen 80;                    # 监听80端口
    charset UTF-8;
    client_max_body_size 30M;
    location / {
        include uwsgi_params;
        uwsgi_pass flask:8080;   # flask指容器名字,该配置是指将信息转发至flask容器的8080端口
    }
}

8.先docker pull nginx:latest 然后在app目录下利用docker-compose启动

docker-compose up -d
docker logs -f #滚动查看日志

日志没有报错,本地进入http://0.0.0.0没问题,在访问服务器IP80端口也没问题,终于成功了
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值