Docker、Nginx 和 Supervisor nginx 绑定失败

Docker、Nginx 和 Supervisor nginx 绑定失败

我正在尝试使用 Nginx、Docker 和 Supervisor 安装一个可用于生产的服务器。

我面临的问题是,即使它有效并且我可以在浏览器中看到 index.html,但此错误仍会一直显示: 2016/08/28 12:05:12 [emerg] 12#12: bind() to [::]:80 failed (98: Address in use) nginx: [emerg] bind() to [::]:80 failed (98: Address in use)

Dockerfile:

FROM nginx:stable-alpine

RUN rm -f /etc/nginx/conf.d/* && mkdir -p /var/www/app
COPY config/nginx.conf /etc/nginx/conf.d/
COPY config/supervisord.conf /supervisord.conf
COPY scripts /scripts
RUN chmod -R 700 /scripts

CMD [ "/scripts/start" ]

在/scripts/start 我有这个:

#!/bin/bash
supervisord -n -c /supervisord.conf

然后在supervisord.conf中:

[unix_http_server]
file=/dev/shm/supervisor.sock   ; (the path to the socket file)

[supervisord]
logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB        ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10           ; (num of main logfile rotation backups;default 10)
loglevel=info                ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false               ; (start in foreground if true;default false)
minfds=1024                  ; (min. avail startup file descriptors;default 1024)
minprocs=200                 ; (min. avail process descriptors;default 200)
user=root                    ;

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[program:nginx]
command=/usr/sbin/nginx
autostart=true
autorestart=true
priority=10
stdout_events_enabled=true
stderr_events_enabled=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

当我运行 docker(不带 daemon -d 选项)时,我得到了这个终端输出:

2016-08-28 12:05:10,474 CRIT Set uid to user 0
2016-08-28 12:05:10,481 INFO RPC interface 'supervisor' initialized
2016-08-28 12:05:10,481 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2016-08-28 12:05:10,481 INFO supervisord started with pid 6
2016-08-28 12:05:11,484 INFO spawned: 'nginx' with pid 9
2016-08-28 12:05:11,497 INFO exited: nginx (exit status 0; not expected)
2016-08-28 12:05:12,499 INFO spawned: 'nginx' with pid 12
2016/08/28 12:05:12 [emerg] 12#12: bind() to 0.0.0.0:80 failed (98: Address in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address in use)
2016/08/28 12:05:12 [emerg] 12#12: bind() to [::]:80 failed (98: Address in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address in use)
........

看起来它产生了 2 个 nginx 进程而不是一个,因为首先说它无缘无故死了,但实际上它并没有死。

答案1

这里有多个问题。一个是,通常不建议在容器中运行supervisord,除非你真的知道自己在做什么。另外,请确保将supervisord中的nodaemon设置为true,否则docker会杀死你的容器,因为不会再有pid 1(因为它会分叉)。

nginx 也一样。Supervisord 期望 nginx 不分叉,而是保持在前台。在 nginx 配置文件中将守护进程设置为 off。

相关内容