Nginx – Docker, Nginx and Supervisor nginx bind fail

dockernginxsupervisord

I am trying to mount a server ready for production using Nginx, Docker and Supervisor.

The problem I'm facing is, even if it works and I can see index.html in my browser, this error is showing all time:
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)

The 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" ]

In /scripts/start I have this:

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

Then in 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

And when I run docker (without daemon -d option), I got this terminal output:

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)
........

It seems like it spawned 2 nginx processes instead of one because first said it died by no reason, but in fact it wasn't dead.

Best Answer

There are multiple problems here. One would be that usually it ain't recommended to run supervisord in containers unless you really know what you are doing. Also, make sure you set nodaemon in supervisord to true, otherwise docker will kill your container because there will be no more pid 1 (since it will fork).

Same goes for nginx. Supervisord expects nginx to not fork but remain in foreground. Set daemon to off in the nginx configuration file.