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

it2025-02-26  27

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端口也没问题,终于成功了

最新回复(0)