【问题标题】:Application served by uWSGI with Supervisord from Docker由 uWSGI 和 Docker 的 Supervisord 服务的应用程序
【发布时间】:2015-02-23 01:25:03
【问题描述】:

我正在尝试使用来自 Docker 的 uWSGI 为 Django 应用程序提供服务。我正在使用 supervisord 在 Dockerfile 结束时为我启动该过程。当我运行图像时,它说 uWSGI 进程启动并成功,但我无法在我认为会显示它的 URL 上查看应用程序。也许我没有正确设置/配置东西?

我现在没有让 supervisord 启动 nginx,因为我目前正在通过 Amazon S3 提供静态文件,并且希望首先专注于启动和运行 wsgi。

我通过uwsgi --init uwsgi.ini:local 成功地在本地使用 uwsgi 运行了应用程序,但是我无法将其移动到 docker 中。

这是我的 Dockerfile

FROM ubuntu:14.04

# Get most recent apt-get
RUN apt-get -y update

# Install python and other tools
RUN apt-get install -y tar git curl nano wget dialog net-tools build-essential
RUN apt-get install -y python3 python3-dev python-distribute
RUN apt-get install -y nginx supervisor
# Get Python3 version of pip
RUN apt-get -y install python3-setuptools
RUN easy_install3 pip

RUN pip install uwsgi

RUN apt-get install -y python-software-properties

# Install GEOS
RUN apt-get -y install binutils libproj-dev gdal-bin

# Install node.js
RUN apt-get install -y nodejs npm

# Install postgresql dependencies 
RUN apt-get update && \
    apt-get install -y postgresql libpq-dev && \
    rm -rf /var/lib/apt/lists

ADD . /home/docker/code

# Setup config files
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
RUN rm /etc/nginx/sites-enabled/default
RUN ln -s /home/docker/code/nginx-app.conf /etc/nginx/sites-enabled/
RUN ln -s /home/docker/code/supervisor-app.conf /etc/supervisor/conf.d/

RUN pip install -r /home/docker/code/app/requirements.txt

EXPOSE 8080

CMD ["supervisord", "-c", "/home/docker/code/supervisor-app.conf", "-n"]

这是我的 uwsgi.ini

[uwsgi]
# this config will be loaded if nothing specific is specified
# load base config from below
ini = :base

# %d is the dir this configuration file is in
socket = %dmy_app.sock
master = true
processes = 4

[dev]
ini = :base
# socket (uwsgi) is not the same as http, nor http-socket
socket = :8001


[local]
ini = :base
http = :8000
# set the virtual env to use
home=/Users/my_user/.virtualenvs/my_env


[base]
# chdir to the folder of this config file, plus app/website
chdir = %dmy_app/
# load the module from wsgi.py, it is a python path from 
# the directory above.
module=my_app.wsgi:application
# allow anyone to connect to the socket. This is very permissive
chmod-socket=666
http = :8080

这是我的 supervisor-app.conf 文件

[program:app-uwsgi]
command = /usr/local/bin/uwsgi --ini /home/docker/code/uwsgi.ini

从一个使用 boot2docker 的 MAC,我试图访问 $(boot2docker ip):8080 的应用程序

最终我想将此容器上传到 AWS Elastic Beanstalk,不仅运行 uWSGI 进程,还运行 celery worker。

当我运行我的容器时,我可以从日志中看到 supervisor 和 uwsgi 都成功启动了。我能够单独使用 uwsgi 和通过主管使用 uwsgi 在我的本地机器上运行东西,但是由于某种原因,当我将东西容器化时,我在任何地方都找不到它。

这是我启动 docker 容器时记录的内容

2014-12-25 15:08:03,950 CRIT Supervisor running as root (no user in config file)
2014-12-25 15:08:03,953 INFO supervisord started with pid 1
2014-12-25 15:08:04,957 INFO spawned: 'uwsgi' with pid 9
2014-12-25 15:08:05,970 INFO success: uwsgi entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

【问题讨论】:

    标签: django docker uwsgi supervisord


    【解决方案1】:

    你是如何启动 docker 容器的?

    我没有看到任何 CMD 或 ENTRYPOINT 脚本,所以我不清楚是如何开始的。

    一般来说,除非绝对必要,否则我建议避免使用 supervisord 之类的东西,只需从 CMD 行在前台启动 uWSGI。尝试在 Dockerfile 的最后一行添加以下内容:

    CMD ["/usr/local/bin/uwsgi", "--ini", "/home/docker/code/uwsgi.ini"]
    

    然后使用docker run -p 8000:8000 image_name 运行。你应该得到一些来自 uWSGI 的回复。如果可行,我建议您将其他服务(postgres、node、到单独的容器)移动。有 Node、Python 和 Postgres 的官方图片,应该可以为您节省一些时间。

    请记住,Docker 容器仅在其主进程(必须在前台)中运行。

    【讨论】:

    • 好吧,我认为在容器中运行 celery 进程也需要 supervisord。我最终计划将此容器上传到 AWS Elastic Beanstalk,因此我需要在一个容器中运行所有进程,因为 EB 无法链接容器(据我所知)。另外,我没有在这个容器中运行 postgres 服务器或 Node 应用程序,它们只是我的 django 应用程序的依赖项。
    • 好的。在docs.docker.com/articles/using_supervisord 有关于运行 supervisord 的非常好的文档。请记住,您需要使用 CMD 或在 docker run 中显式启动主管。
    • 我的 Dockerfile 底部有一个 CMD,我可以看到该进程已启动。我将日志添加到原始帖子的底部。但是,我仍然无法在我的网络浏览器中访问该应用程序。
    • docker 日志上是否有任何错误消息?究竟是什么问题?现在听起来像是一个端口转发问题。
    • 原谅我,我没有意识到我需要滚动 Dockerfile 才能看到 CMD...
    猜你喜欢
    • 2015-11-02
    • 1970-01-01
    • 2017-03-08
    • 2021-11-01
    • 2015-03-04
    • 2014-03-08
    • 2021-10-26
    • 2015-12-05
    • 2012-04-06
    相关资源
    最近更新 更多