通过Docker部署Flask和Django应用实践

Aug 31, 2017 00:00 · 959 words · 2 minute read deploy docker flask django


这里假设,你已经有一个flask或者django项目,并且能够在主机上面成功部署。现在你想迁移到docker环境中。这篇文章可以给你一定的参考。我们在docker内部使用supervisor管理进程,当应用crash时,能够及时重启。supervisor简单教程及介绍(cn)

日志

由于使用了supervisor,所以uwsginginx日志都会在/var/log/supervisor/目录下

Flask + uwsgi + Nginx

nginx配置文件[nginx.conf]


# configuration of the server
server {
    # the port your site will be served on, default_server indicates that this server block
    # is the block to use if no blocks match the server_name
    listen      80;
    server_name yourhost;
    access_log  /var/log/nginx/xx.log;
    # the domain name it will serve for
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  127.0.0.1:5005;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }

    location ~ ^/nginx-status {
        stub_status on;
    }
}

uwsgi配置文件[start.ini]

[uwsgi]
wsgi-file = app.py
callable = app
socket = :5005
processes = 1
threads = 30
lazy-apps=true
; daemonize=uwsgi.log
buffer-size = 65535

docker配置文件[dockerfile]

FROM ubuntu:16.04

ENV TZ Asia/Shanghai

RUN apt-get update && apt-get install -yq python-dev \
    python-pip \
    libpq-dev \
    libmysqlclient-dev \
    nginx \
    tzdata \
    supervisor

ADD . /code
WORKDIR /code

RUN pip install --upgrade pip && pip install uwsgi
RUN pip install -r requirements.txt
RUN ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && dpkg-reconfigure -f noninteractive tzdata

COPY config/nginx.conf /etc/nginx/conf.d/
COPY config/supervisor.conf /etc/supervisor/conf.d/

EXPOSE 80

CMD ["supervisord", "-n"]

supervisor配置文件[supervisor.conf]

[program:app-uwsgi]
command = /usr/local/bin/uwsgi --ini /code/start.ini

[program:nginx-app]
command = /usr/sbin/nginx -g "daemon off;"

Django + uwsgi + Nginx

nginx配置[nginx.conf]

# nginx-app.conf

# the upstream component nginx needs to connect to
upstream django {
    server 127.0.0.1:9100; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on, default_server indicates that this server block
    # is the block to use if no blocks match the server_name
    listen      80;
    server_name yourhost;
    access_log  /var/log/nginx/xx.log;
    # the domain name it will serve for
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    location / {
        uwsgi_pass  127.0.0.1:9100;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }

    location ~ ^/nginx-status {
        stub_status on;
    }
}

uwsgi配置文件[start.ini]

[uwsgi]
socket = 0.0.0.0:9100
chdir =/code
wsgi-file =/client/wsgi.py
processes =2
threads =2
; daemonize=/home/docker/code/uwsgi.log
buffer-size = 65535

docker配置文件[dockerfile]

# Copyright 2013 Thatcher Peskens
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM ubuntu:16.04

MAINTAINER Dockerfiles

ENV TZ Asia/Shanghai

# Install required packages and remove the apt packages cache when done.

RUN apt-get update && \
    apt-get upgrade -y && \ 
    apt-get install -y \
    python \
    python-dev \
    python-setuptools \
    python-pip \
    libmysqlclient-dev \
    nginx \
    tzdata \
    libssl-dev \
    supervisor && \
    pip install -U pip setuptools

# install uwsgi now because it takes a little while
RUN pip install uwsgi

# setup all the configfiles
COPY config/nginx-app.conf /etc/nginx/conf.d/
COPY config/supervisor-app.conf /etc/supervisor/conf.d/

# COPY requirements.txt and RUN pip install BEFORE adding the rest of your code, this will cause Docker's caching mechanism
# to prevent re-installinig (all your) dependencies when you made a change a line or two in your app.

COPY requirements.txt /home/docker/

RUN pip install -r requirements.txt

# set timezone 

RUN ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && dpkg-reconfigure -f noninteractive tzdata


# add (the rest of) our code

ADD . /code
WORKDIR /code

# install django, normally you would remove this step because your project would already
# be installed in the code/app/ directory

EXPOSE 80
CMD ["supervisord", "-n"]

supervisor配置文件[supervisor.conf]

[program:app-uwsgi]
command = /usr/local/bin/uwsgi --ini /code/start.ini

[program:nginx-app]
command = /usr/sbin/nginx -g "daemon off;"

启动

docker build -t your_product . # 构建镜像

docker run -d -p 80:80 your_product # 启动一个 CONTAINER 运行镜像
tweet Share