Docker: 无法识别 uwsgi 服务

Docker: uwsgi service not recognised

FROM ubuntu:14.04.2
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN apt-get -y update && apt-get upgrade -y
RUN apt-get install python build-essential python-dev python-pip python-setuptools -y
RUN apt-get install libxml2-dev libxslt1-dev python-dev -y
RUN apt-get install libpq-dev postgresql-common postgresql-client -y
RUN apt-get install openssl openssl-blacklist openssl-blacklist-extra -y
RUN apt-get install nginx -y
RUN pip install "pip>=7.0"
RUN pip install virtualenv uwsgi

ADD canonicaliser_api /home/ubuntu/canonicaliser_api
ADD config_local.py /home/ubuntu/canonicaliser_api/config/config_local.py
RUN virtualenv /home/ubuntu/canonicaliser_api/venv
RUN source /home/ubuntu/canonicaliser_api/venv/bin/activate && pip install -r /home/ubuntu/canonicaliser_api/requirements.txt
RUN export CFLAGS=-I/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include/
RUN source /home/ubuntu/canonicaliser_api/venv/bin/activate && cd /home/ubuntu/canonicaliser_api/canonicaliser/cython_extensions/ && python setup.py build_ext --inplace
RUN cp /home/ubuntu/canonicaliser_api/canonicaliser/cython_extensions/canonicaliser/cython_extensions/*.so /home/ubuntu/canonicaliser_api/canonicaliser/cython_extensions
RUN rm -rf /home/ubuntu/canonicaliser_api/canonicaliser/cython_extensions/canonicaliser
RUN rm -r /home/ubuntu/canonicaliser_api/canonicaliser/cython_extensions/build

RUN mkdir /var/run/flask-uwsgi
RUN chown -R www-data:www-data /var/run/flask-uwsgi
RUN mkdir /var/log/flask-uwsgi
RUN touch /var/log/flask-uwsgi/dqs_canon.log
RUN chown -R www-data:www-data /var/log/flask-uwsgi
RUN mkdir /etc/flask-uwsgi

ADD configs/new-canon/flask-uwsgi/flask-uwsgi.conf /etc/init/
ADD configs/new-canon/flask-uwsgi/flask-uwsgi.ini /etc/flask-uwsgi/

EXPOSE 8888
CMD service flask-uwsgi restart

# RUN echo "daemon off;" >> /etc/nginx/nginx.conf
# CMD service nginx start

当我尝试 运行 这个 docker 我收到错误消息:

flask-uwsgi: unrecognized service

所以我最终取消了最后两行的注释,以便 nginx 启动并保持 docker 进程处于活动状态。然后我通过 ssh 进入它进行调试。

docker exec -it 20b2ff3a4cac bash

现在,当我尝试 运行 服务时,出现了同样的问题,我找不到任何遗漏的步骤。也许在 docker?

中不允许这样启动服务
root@30b2ff3a4cac:/# service flask-uwsgi start
flask-uwsgi: unrecognized service

/etc/flask-uwsgi/flask-uwsgi.ini:

[uwsgi]
socket = /var/run/flask-uwsgi/flask-uwsgi.sock
home = /home/ubuntu/canonicaliser_api/venv
wsgi-file = flask_uwsgi.py 
callable = app 
master = true
; www-data uid/gid
uid = 33
gid = 33
http-socket = :8888
die-on-term = true
processes = 4
threads = 2
logger = file:/var/log/flask-uwsgi/flask-uwsgi.log

/etc/init/flask-uwsgi.conf:

start on [2345]
stop on [06]

script
    cd /home/ubuntu/canonicaliser_api
    exec uwsgi --ini /etc/flask-uwsgi/flask-uwsgi.ini
end script

当 ssh 进入进程时,我可以 运行 像这样直接使用 uwsgi,它可以工作:

exec uwsgi --ini /etc/flask-uwsgi/flask-uwsgi.ini

因此 docker 中不支持服务,我必须 运行 直接在 docker 图像中这样做:

RUN exec uwsgi --ini /etc/flask-uwsgi/flask-uwsgi.ini

或者我遗漏了什么。

是的,不要使用服务。

你不能这样做:

RUN exec uwsgi --ini /etc/flask-uwsgi/flask-uwsgi.ini

该行将完成,并提交给图像。但是在后续的指令中或者容器启动时,进程将不再运行

相反,您可以在 ENTRYPOINT 或 CMD 命令中执行此操作,因为它们在容器启动时执行。这应该有效:

CMD uwsgi --ini /etc/flask-uwsgi/flask-uwsgi.ini

其他几点:

  • 如果您使用 official python images 之一,您可能会发现事情更容易。
  • 我只想摆脱 virtualenv;我没有看到 virtualenv 在隔离容器中的好处。
  • 运行 RUN rm -rf ... 不保存任何 space;这些文件已经提交到上一层。您需要在添加文件的同一指令中删除文件,以避免在图像中使用 space。
  • USER www-data 而不是 chowning 文件可能更有意义。