簡體   English   中英

在Docker容器中使用uwsgi和nginx設置Flask應用

[英]Setting flask app with uwsgi and nginx in docker container

我正在嘗試在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。 我應該使用主管。 但是我不知道如何去做。

在“普通”服務器上,我可以運行這些命令,並且可以運行,但在Docker容器中則無法運行。

我的“唯一”問題是我必須使用uwsgi和nginx運行我的應用程序,但我不明白。

我在Internet上閱讀了許多帖子和教程,也嘗試更改此項目,但它仍然無法正常工作,我更喜歡使用我的dockerfile(此文件),因為這是我的理解。

我找到了答案。

我和主管一起解決了。

# 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;
    }
}

就是這樣

您還可以使用docker-systemctl-replacement ,它專門用於允許在docker容器中使用與實際systemd受控機器上相同的systemctl命令。 作為CMD運行時,它將啟動所有已啟用的服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM