nginx: [emerg] bind() to 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

  • Can a dockerfile make use of a data only container defined in the docker-compose.yml file?
  • How to Create MongoDB cluster as Docker Containers
  • Invalid value “zookeeper” for flag -a: valid streams are STDIN, STDOUT and STDERR
  • How to configure NGINX routes?
  • How can I deploy a crate cluster on Giant Swarm?
  • Jenkins using docker: How to run tests?
  • 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


    Which returned

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

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

    nginx: [emerg] bind() to failed (98: Address already in use)
    nginx: [emerg] bind() to failed (98: Address already in use)
    nginx: [emerg] bind() to failed (98: Address already in use)
    nginx: [emerg] bind() to failed (98: Address already in use)
    nginx: [emerg] bind() to 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    *               LISTEN      -

    I have absolutely no idea what is happening.

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

  • Why patch linux kernel on Centos for using
  • Docker with cloudflare
  • Issue with docker Inter container communication on the isolated bridge
  • Elastic Beanstalk Docker App fails after “Restart App Servers”
  • How can I use nginx to forward incoming requests from a certain URL on port 80 to various docker applications
  • Run apache in both host machine and docker container on 80 port
  • 2 Solutions collect form web for “nginx: [emerg] bind() to 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 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

    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

    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.