在 docker 容器中使用 uwsgi 和 nginx 设置烧瓶应用程序
Setting flask app with uwsgi and nginx in docker container
我正在尝试 运行 一个 Docker 容器,在 docker 容器中装有烧瓶、uwsgi 和 nginx。
我的 Docker 文件看起来是这样的:
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
但是当我尝试构建它时,出现此错误
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
我正在阅读,我认为问题出在 systemd 上。我应该使用 supervisord。但是我不知道该怎么做。
在“普通”服务器上我可以 运行 这些命令并且它会工作,但在 docker 容器中不工作。
我遇到的“唯一”问题是我必须 运行 我的应用程序与 uwsgi 和 nginx 但我不明白。
我在 Internet 上阅读了很多帖子和教程,也尝试更改 this project 但它仍然无法正常工作,我更愿意使用我的 docker 文件(这个),因为它是我的明白了。
我找到了答案。
我用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;
}
}
就是这样
您还可以使用 docker-systemctl-replacement,它专门用于允许在 docker 容器中使用与在真正的 systemd 控制的机器上相同的 systemctl 命令。当 运行 它作为 CMD 时,它将启动所有已启用的服务。
我正在尝试 运行 一个 Docker 容器,在 docker 容器中装有烧瓶、uwsgi 和 nginx。
我的 Docker 文件看起来是这样的:
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
但是当我尝试构建它时,出现此错误
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
我正在阅读,我认为问题出在 systemd 上。我应该使用 supervisord。但是我不知道该怎么做。
在“普通”服务器上我可以 运行 这些命令并且它会工作,但在 docker 容器中不工作。
我遇到的“唯一”问题是我必须 运行 我的应用程序与 uwsgi 和 nginx 但我不明白。
我在 Internet 上阅读了很多帖子和教程,也尝试更改 this project 但它仍然无法正常工作,我更愿意使用我的 docker 文件(这个),因为它是我的明白了。
我找到了答案。
我用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;
}
}
就是这样
您还可以使用 docker-systemctl-replacement,它专门用于允许在 docker 容器中使用与在真正的 systemd 控制的机器上相同的 systemctl 命令。当 运行 它作为 CMD 时,它将启动所有已启用的服务。