【问题标题】:Can't map ports between host and a Docker container on OSX无法在 OSX 上映射主机和 Docker 容器之间的端口
【发布时间】:2015-01-25 07:14:43
【问题描述】:

我已尝试关注some instructions on GitHub 设置端口转发,但我没有运气。你能帮忙吗?我按照书中的示例构建了容器,这里是 Dockerfile:

FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y nginx
RUN echo 'Hi, I am in your container' > /usr/share/nginx/html/index.html
EXPOSE 8000   

采取的步骤:

$ boot2docker stop
$ VBoxManage modifyvm "boot2docker-vm" --natpf1 delete forwardHostPort8000ToDockerVM
$ VBoxManage modifyvm "boot2docker-vm" --natpf1 "forwardHostPort8000ToDockerVM,tcp,,8000,,8000"
$ boot2docker start
$ docker run -d -p 127.0.0.1:8000:8000 --name static_web static_web \nginx -g "daemon off;"
122ba8949685ce91b84890656c399b19028cb2e8a7e8be3d4a19122eba9ab592

这是结果:

$ curl 127.0.0.1:8000
curl: (52) Empty reply from server

$ telnet 127.0.0.1 8000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host

注意: 如果我不将其绑定到 127.0.0.1 接口(即仅-p 8000:8000),那么它将与我的 VM 的 IP 一起使用:

$ curl 192.168.59.103:8000
Hi, I am in your container

环境:

$ boot2docker -v
Boot2Docker-cli version: v1.3.1
Git commit: 57ccdb8

$ docker -v
Docker version 1.3.1, build 4e9bbfa

我使用的是在 OS X 10.10.1 下运行的 VirtualBox 4.3.20 版。

【问题讨论】:

  • 感谢@alexwlchan 对格式的帮助。

标签: port boot2docker


【解决方案1】:

这实际上是我这边的一个错误。我在 Dockerfile 中暴露了 8000 端口;但是,我没有更改 nginx 中的设置。因此,我将其改回端口 80 并按照以下方式启动容器,并且它起作用了。现在我可以在另一台机器上用浏览器访问它了。

    docker run -d -p 8000:80 --name static_web static_web \nginx -g "daemon off;"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 2014-09-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    相关资源
    最近更新 更多