Setting flask app with uwsgi and nginx in docker container

人走茶凉 提交于 2021-02-07 04:18:10

问题


I'm trying to run a Docker container with flask, uwsgi and nginx in a docker container.

my Dockerfile looks so:

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'

and then my app.service so:

[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

the nginx configuration

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

then my uwsgi.ini

[uwsgi]
module = wsgi:app

master = true
processes = 5

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

die-on-term = true

But when I try to build it, I get this error

Step 12 : RUN systemctl start app

---> Running in 79a22c2629db

failed to connect to bus: No such file or directory

INFO[0022] The command [/bin/sh -c systemctl start app] returned a non-zero code: 1

I was reading and I think that the problem is with the systemd. I should use supervisord. But I don't have a clue how to do it.

On a "normal" server I could run those commands and it would work, but in a docker container is not working.

The "only" problem I've is that I have to run my app with uwsgi and nginx but I don't get it.

I read many posts and tutorials on Internet and also tried to change this project but it still not working and I'd prefer to use my dockerfile (this one) because it's what I understand.


回答1:


I found the answer.

I resolved it with 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"]

and then the supervisor_app.conf looks so

[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

the 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

and my 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;
    }
}

and that's it




回答2:


You could also use the docker-systemctl-replacement which has been specifically made to allow the same systemctl commands to be used in a docker container as on a real systemd controlled machine. When running it as CMD then it will start all services that have been enabled.



来源:https://stackoverflow.com/questions/48744884/setting-flask-app-with-uwsgi-and-nginx-in-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!