docker-compose搭建flask环境:python3-flask-uwsgi-nginx-supervisor

话说想把之前的flask项目搬家,之前是手动部署。现在直接用docker-compose简单粗暴,一键部署。


先看下目录结构

├── docker-compose.yml
├── flask
│   └── Dockerfile	#我的项目是python3,就安装python3。以及uwsgi和supervisor
├── log 	#存放日志文件
├── nginx
│   ├── default.conf   # 为了跟uwsgi沟通
│   └── nginx.conf		#改了nginx启动权限root(为了简单测试,等有时间再弄个文章)和daemon off
├── README.md
├── tmp	#存访uwsgi.sock
└── wwwroot #flask项目目录
    ├── app						#项目工程
    │   ├── __init__.py
    ├── manage.py				#项目启动文件
    ├── requirements.txt   		#项目需要的的包
    ├── supervisord.conf		#supervisor配置文件
    └── uwsgi.ini			    #uwsgi配置文件

相关内容:

docker-compose.yml(这里为了方便测试环境,我随便写了测试。没有加数据库,数据库也是很简单的)
version: '3'
services:

  nginx:
    hostname: nginx
    image: nginx:1.12.1
    ports:
      - 80:80
    networks:
      - lnpm
    depends_on:
      - flask
    volumes:
      - ./log:/log
      - ./tmp:/tmp
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    command: |
      bash -c '
      rm /etc/nginx/conf.d/default.conf
      /usr/sbin/nginx'
  flask:
    hostname: flask
    build:
      context: ./flask
      dockerfile: Dockerfile
    networks:
      - lnpm
    volumes:
      - ./tmp:/tmp
      - ./wwwroot:/app
      - ./log:/log
    # environment:
    #   - LANG=zh_CN.UTF-8
    command: |
      sh -c '
      pip3 install -r /app/requirements.txt
      cp /app/uwsgi.ini /etc/uwsgi/uwsgi.ini
      cp /app/supervisord.conf /etc/supervisord.conf
      /usr/bin/supervisord'
     
networks:
  lnpm:
flask/Dockerfile
FROM alpine:3.8

RUN apk add --no-cache \
    python3 \
    bash \
    uwsgi \
    uwsgi-python3 \
    supervisor && \
    python3 -m ensurepip && \
    rm -r /usr/lib/python*/ensurepip && \
    pip3 install --upgrade pip setuptools && \
    rm -r /root/.cache
nginx/default.conf
server {
    listen      80;
    server_name www.test2018.com;
    charset     utf-8;
    client_max_body_size 75M;

   
    location / {
        access_log /log/cms.access.log;
	    error_log /log/cms.error.log;
	    include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
    }
}
nginx/nginx.conf
user  root;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
daemon off;
wwwroot/uwsgi.ini
[uwsgi]
chdir = /app
module = manage
callable = app
plugins = /usr/lib/uwsgi/python3
master = true
uid = root
gid = root
socket = /tmp/uwsgi.sock
chown-socket = root:root
chmod-socket = 664
processes = 2
logto=/log/%n.log
wwwroot/supervisord.conf
[supervisord]
nodaemon=true

[program:uwsgi]
directory=/app
command=/usr/sbin/uwsgi --ini /etc/uwsgi/uwsgi.ini --die-on-term
stdout_logfile=/log/supervisord_access.log
stderr_logfile=/log/supervisord_error.log
wwwroot/requirements.txt
flask
wwwroot/manage.py
# -*- coding:utf-8 -*-

from app import app

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)
wwwroot/app/init.py
# -*- coding:utf-8 -*-

from flask import Flask

app = Flask(__name__)


@app.route("/")
def index():
    return "uwsgi nginx flask!"

使用方法:

docker-compose build
docker-compose up -d

完整项目地址:https://github.com/magic-joker/Docker 随手点个start嘿嘿!

猜你喜欢

转载自blog.csdn.net/Maggie_up/article/details/82926594