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)
碼頭工人文件:
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 程序而不是一個,因為首先說它無緣無故地死了,但實際上它並沒有死。
這裡有多個問題。一是通常不建議在容器中執行 supervisord,除非你真的知道你在做什麼。另外,請確保您將 supervisord 中的 nodaemon 設置為 true,否則 docker 將殺死您的容器,因為將不再有 pid 1(因為它會分叉)。
同樣適用於 nginx。Supervisord 期望 nginx 不會分叉,而是保持在前台。在 nginx 配置文件中將 daemon 設置為 off。