在docker上,nginx:[emerg] bind()到0.0.0.0:80失败(98:Address已被使用)

我正在尝试从nginx加载默认网页,但在容器运行后,我无法通过http连接到端口80.

我正在运行docker 1.9.9

我采取的步骤如下:

我创建了一个Docker文件:

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

该docker文件将下面的nginx配置文件复制到/ etc / nginx / sites-available / default中,并为/ 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;
  }
}

然后我用

docker build -t dnginx 

我启动了容器:

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

然后我找到了ip地址并尝试连接

curl http://172.17.0.2

哪个回来

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

我在容器中打开一个bash shell并运行nginx,它返回:

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

如果我运行netstat –listen我得到:

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

如果我运行netstat -ltnp | grep:80我得到:

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -

我绝对不知道发生了什么.

如果我连接到nginx图像,也会发生同样的情况.

最佳答案
我已经尝试了你的Dockerfile,它已经按预期工作了.我所做的唯一的改变就是删除任何涉及到上级和添加的内容

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

在Docker文件的末尾.

当容器启动时,其网络命名空间与主机和其他容器完全隔离,唯一的进程是由ENTRIPOINT或CMD指令及其子进程启动的,所以我认为您看到的nginx进程在容器就是由supervisord运行的容器.

您确定172.17.0.2是docker容器的当前IP吗?
容器IP不稳定,它取决于主机上运行的容器数量和启动顺序.

您使用运行选项’-p 80:80’在主机上公开http端口,因此您应该能够使用curl 127.0.0.1在docker主机上访问它.

转载注明原文:在docker上,nginx:[emerg] bind()到0.0.0.0:80失败(98:Address已被使用) - 代码日志