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

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

  • Delete images from v2 private registry (leaves behined empty repository with tags of the images it contained)
  • AWS S3 Elastic Beanstalk Docker - /dev/fus: Operation not permitted
  • Docker: trouble connecting to mysql, network issue?
  • Exposed Service unreachable
  • Backup of Docker image
  • How to properly share a folder between few docker containers in read mode?
  • 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.

  • I would like to populate a entry in config file at run time via docker compose
  • How do I setup a read only private docker registry using just HTTP?
  • Starting a system service in a Docker container
  • How can I launch an IronWorker with Docker without needing to pay for private Docker repositories, nor make my code public?
  • Docker compose mysql reachable on port 3306
  • Find out which network interface belongs to docker container
  • 2 Solutions collect form web for “nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) on docker”

    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.

    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

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.