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

dag*_*da1 16 nginx docker

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

我正在运行码头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
Run Code Online (Sandbox Code Playgroud)

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我用以下内容构建了图像:

docker build -t dnginx 
Run Code Online (Sandbox Code Playgroud)

我启动了容器:

docker run --name d3 -d -p 80:80 dnginx
Run Code Online (Sandbox Code Playgroud)

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

curl http://172.17.0.2
Run Code Online (Sandbox Code Playgroud)

哪个回来了

curl:(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()
Run Code Online (Sandbox Code Playgroud)

如果我跑,netstat --listen我得到:

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

如果我跑,netstat -ltnp | grep :80我得到:

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -
Run Code Online (Sandbox Code Playgroud)

我完全不知道发生了什么.

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

小智 16

这些天我有同样的问题,但我必须运行多进程,所以删除supervisord不是我的解决方案.

只需将-g "daemon off;"标志添加到supervisor nginx程序命令即可解决问题.那是

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

似乎容器中的所有进程必须在前台运行,甚至由supervisord工具管理

  • 该解决方案适用于使用supervisord.conf的人 (2认同)

mne*_*cia 12

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

CMD ["nginx", "-g", "daemon off;"]
Run Code Online (Sandbox Code Playgroud)

在结束时Dockerfile.

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

你确定这172.17.0.2是docker容器的当前IP吗?容器IP不稳定,具体取决于主机上运行的容器数量以及启动顺序.

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