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

前端之家收集整理的这篇文章主要介绍了在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主机上访问它.

原文链接:https://www.f2er.com/nginx/434651.html

猜你在找的Nginx相关文章