我从以下 Docker 文件创建了一个 docker 镜像:
FROM ubuntu:14.04
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 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
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
ADD supervisor.nginx.conf /etc/supervisor.d/nginx.conf
copy ./dist /usr/share/nginx/html/
EXPOSE 80
CMD /usr/bin/supervisord -n
然后我用以下方法构建了图像:
docker build -t dnginx .
然后我创建了一个容器:
docker run --name d3 -d -p 80:80 dnginx
我可以看到容器正在运行,并且通过运行 docker ps -a 暴露了端口 80:
42ba6aa343bd dnginx "/bin/sh -c '/usr/bin" 2 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp d3
我可以通过运行
docker inspect --format '{{ .NetworkSettings.IPAddress }}' d3
找到 IP 地址但如果尝试从 IP 地址点击 url,我将无法连接到容器:
curl http://172.17.0.2
我得到以下回复Failed to connect to 127.17.0.2 port 80: Operation timed out
我也无法ping通IP地址
我错过了什么?
最佳答案
命令docker run --name d3 -d -p 80:80 dnginx
意味着您将主机上的端口 80 映射到 docker 容器中的端口 80。
您可以在 docker ps
中查看映射。输出为 0.0.0.0:80->80/tcp
这样如果你 curl 127.0.0.1
或 curl your-ip-here
在您的主机上,您的请求会自动路由到将为流量提供服务的 nginx 容器。
无需查找容器的私有(private) IP。您可以使用任何主机的 IP 地址。
关于docker - 通过 http 连接到正在运行的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33687388/