nginx:docker上的[emerg] bind()到0.0.0.0:80失败(98:地址已经在使用中)

时间:2015-11-11 12:44:17

标签: nginx docker

我正在尝试从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

返回

  

卷曲:(7)无法连接到172.17.0.2端口80:操作超时

我在容器中打开了一个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图像,也会发生同样的事情。

1 个答案:

答案 0 :(得分:11)

我已经尝试了你的Dockerfile,它已经按预期工作了。我做的唯一更改是删除任何引用supervisord和添加

的内容
CMD ["nginx", "-g", "daemon off;"]

Dockerfile

的末尾

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

您确定supervisord是Docker容器的当前IP吗? 容器IP不稳定,它取决于主机上运行的容器数量以及它们的启动顺序。

您使用运行选项'-p 80:80'公开主机上的http端口,因此您应该可以使用172.17.0.2在docker主机上访问它。