在docker容器中使用uwsgi和nginx设置flask应用程序

时间:2018-02-12 11:01:39

标签: python docker nginx flask uwsgi

我试图在docker容器中运行带有flask,uwsgi和nginx的Docker容器。

我的Dockerfile看起来如此:

FROM ubuntu:16.04

MAINTAINER Dockerfiles

# Install required packages and remove the apt packages cache when done.
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y \
    ...

# install uwsgi 
RUN pip3 install uwsgi

# copy over requirements.txt file
COPY requirements.txt /home/docker/code/

# upgrade pip and install required python packages
RUN pip3 --no-cache-dir install -U pip
RUN pip3 --no-cache-dir install -r /home/docker/code/requirements.txt

# add (the rest of) our code
COPY ./app /home/docker/code/

# create a systemd unit file
COPY ./app.service /etc/systemd/system/  # Think the problem is here

# start the uwsgi service
RUN systemctl start app  # This is not working
RUN systemctl enable app

# setup all the configfiles
COPY nginx_app /etc/nginx/sites-available/

# enable nginx server block
RUN ln -s /etc/nginx/sites-available/nginx_app /etc/nginx/sites-enabled

# validate syntax
CMD ["nginx", "-t"]

# restart nginx
RUN systemctl restart nginx

# allow nginx
RUN ufw allow 'Nginx Full'

然后我的app.service所以:

[Unit]
Description=uWSGI instance to serve app
After=network.target

[Service]
User=docker
Group=www-data
WorkingDirectory=/home/docker/code
ExecStart=/usr/local/bin/uwsgi --ini uwsgi.ini

[Install]
WantedBy=multi-user.target

nginx配置

server {
    listen 80;
    server_name my_server_ip;

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

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/docker/code/myproject.sock;
    }

然后是我的uwsgi.ini

[uwsgi]
module = wsgi:app

master = true
processes = 5

socket = app.sock
chmod-socket = 664
vacuum = true

die-on-term = true

但是当我尝试构建它时,我收到此错误

  

步骤12:运行systemctl启动应用程序

     

--->在79a22c2629db中运行

     

无法连接到总线:没有这样的文件或目录

     

INFO [0022]命令[/ bin / sh -c systemctl start app]返回非零代码:1

我正在阅读,我认为问题出在systemd上。我应该使用supervisord。但我不知道如何做到这一点。

在"正常"服务器我可以运行这些命令,它会工作,但在docker容器中不起作用。

" only"问题我是因为我必须用uwsgi和nginx来运行我的应用程序,但我不能得到它。

我在互联网上阅读了很多帖子和教程,并尝试更改this project,但它仍然无效,我更喜欢使用我的dockerfile(这个),因为这是我所理解的。

2 个答案:

答案 0 :(得分:2)

我找到了答案。

我用supervisord解决了这个问题。

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

# setup all the configfiles
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
COPY nginx_app.conf /etc/nginx/sites-available/default
COPY supervisor_app.conf /etc/supervisor/conf.d/


# copy over our requirements.txt file
COPY requirements.txt /home/docker/code/

# upgrade pip and install required python packages
RUN pip3 --no-cache-dir install -U pip
RUN pip3 --no-cache-dir install -r /home/docker/code/requirements.txt

# add (the rest of) our code
COPY ./app /home/docker/code/

EXPOSE 80
CMD ["supervisord"]

然后supervisor_app.conf看起来如此

[supervisord]
nodaemon=true

[program:uwsgi]
command = /usr/local/bin/uwsgi --ini /home/docker/code/uwsgi.ini
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:nginx]
command = /usr/sbin/nginx
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

uwsg.ini

[uwsgi]
callable = app
chdir = /home/docker/code/
wsgi-file = /home/docker/code/wsgi.py
socket = /home/docker/code/app.sock
master = true
processes = 2
chmod-socket = 666
enable-threads = true

和我的nginx_app.conf

server {
    listen 80 default_server;
    server_name my_ip;

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

    location / {
        include uwsgi_params;
        uwsgi_pass unix:///home/docker/code/app.sock;
    }
}

就是这样

答案 1 :(得分:0)

您还可以使用专门制作的docker-systemctl-replacement来允许在docker容器中使用相同的systemctl命令,就像在真正的系统控制的机器上一样。当它作为CMD运行时,它将启动所有已启用的服务。