Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) on docker

Tags:

docker

nginx

I am trying to get the default webpage from nginx loaded but I cannot connect to port 80 over http after the container is running.

I am running docker 1.9.9

The steps I took are as followed:

I created a Docker file that this:

FROM ubuntu:15.10

RUN echo "Europe/London" > /etc/timezone
RUN dpkg-reconfigure -f noninteractive tzdata

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update
RUN apt-get install -y nginx
RUN apt-get install -y supervisor
RUN apt-get update && apt-get -q -y install lsof
RUN apt-get install net-tools
RUN apt-get install psmisc
RUN apt-get -y install curl

ADD supervisor.nginx.conf /etc/supervisor.d/nginx.conf

CMD /usr/bin/supervisord -n

RUN rm -Rf /etc/nginx/conf.d/*
RUN rm /etc/nginx/sites-enabled/default

RUN mkdir /etc/nginx/logs/
RUN touch /etc/nginx/logs/error.log

RUN mkdir /usr/share/nginx/logs/
RUN touch /usr/share/nginx/logs/error.log

ADD ./conf/nginx.conf /etc/nginx/sites-available/default
RUN ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default

copy ./dist /usr/share/nginx/html

CMD /usr/bin/supervisord -n

THe docker file copies the nginx config file below into /etc/nginx/sites-available/default and creates a symlink to this file for /etc/nginx/sites-enabled/default.

server {
  root /usr/share/nginx/html;
  index index.html index.htm;

  # redirect server error pages to the static page /50x.html
  #
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

  location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
    access_log        off;
    log_not_found     off;
    expires           5d;
  }

  # deny access to . files, for security
  #
  location ~ /\. {
     access_log off;
     log_not_found off;
     deny all;
  }
}

I then built the image with:

docker build -t dnginx 

I started the container with:

docker run --name d3 -d -p 80:80 dnginx

I then found the ip address and tried to connect

curl http://172.17.0.2

Which returned

curl: (7) Failed to connect to 172.17.0.2 port 80: Operation timed out

I opened a bash shell in the container and ran nginx which returned:

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

If I run netstat --listen I get:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 *:80                    *:*                     LISTEN

If I run netstat -ltnp | grep :80 I get:

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -

I have absolutely no idea what is happening.

The same thing happens if I connect just to the nginx image.

like image 810
dagda1 Avatar asked Nov 11 '15 12:11

dagda1


2 Answers

I have the same issue these days, but i have to run multi-process, so removing supervisord is not a solution for me.

Simply add -g "daemon off;" flag to supervisor nginx program command solves the problem. That is

[program:nginx] command=/usr/sbin/nginx -g "daemon off;"

It seems that all process in a container must be run in the foreground, even managed by supervisord tools

like image 50
cbsw Avatar answered Oct 22 '22 08:10

cbsw


I've tried your Dockerfile and it has worked as expected. The only changes I made were removing anything referring to supervisord and adding

CMD ["nginx", "-g", "daemon off;"]

at the end of the Dockerfile.

When a container starts, its networking namespace is completely isolated from the host and from the other containers, and the only processes are the one started by the ENTRIPOINT or CMD directives and its children, so I think that the nginx process that you see running in the container is exactly the one which is run by supervisord.

Are you sure that 172.17.0.2 is the current IP of the docker container? The container IP is not stable, and it varies depending on how many containers are running on your host and the order on which they have been started.

You expose the http port on the host with the run option '-p 80:80', so you should be able to access it on docker host using curl 127.0.0.1.

like image 22
mnencia Avatar answered Oct 22 '22 09:10

mnencia