nginx:[emerg] bind()到docker上的0.0.0.0:80失败(98:地址已在使用中) [英] nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) on docker

查看:584
本文介绍了nginx:[emerg] bind()到docker上的0.0.0.0:80失败(98:地址已在使用中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我正在运行docker 1.9.9

I am running docker 1.9.9

我采取的步骤如下:

我创建了一个Docker文件,该文件是:

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

泊坞窗文件将下面的nginx配置文件复制到/etc/nginx/sites-available/default中,并为/etc/nginx/sites-enabled/default创建到该文件的符号链接.

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

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

I then found the ip address and tried to connect

curl http://172.17.0.2

哪个回来了

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

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

我在容器中打开了一个bash外壳,然后运行nginx,该操作返回了:

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

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,我会得到:

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

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

If I run netstat -ltnp | grep :80 I get:

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -

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

I have absolutely no idea what is happening.

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

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

推荐答案

我已经尝试过您的Dockerfile,并且按预期工作.我所做的唯一更改是删除了所有引用supervisord的内容并添加了

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;"]

Dockerfile的末尾.

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

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.

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

Are you sure that 172.17.0.2 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.

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

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 127.0.0.1.

这篇关于nginx:[emerg] bind()到docker上的0.0.0.0:80失败(98:地址已在使用中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆