docker容器上的连接被拒绝

新手上路,请多包涵

我是 Docker 新手,正在尝试制作一个演示 Rails 应用程序。我制作了一个看起来像这样的 dockerfile:

 FROM ruby:2.2
MAINTAINER marko@codeship.com

# Install apt based dependencies required to run Rails as
# well as RubyGems. As the Ruby image itself is based on a
# Debian image, we use apt-get to install those.
RUN apt-get update && apt-get install -y \
build-essential \
nodejs

    # Configure the main working directory. This is the base
    # directory used in any further RUN, COPY, and ENTRYPOINT
    # commands.
RUN mkdir -p /app
WORKDIR /app

    # Copy the Gemfile as well as the Gemfile.lock and install
    # the RubyGems. This is a separate step so the dependencies
    # will be cached unless changes to one of those two files
    # are made.
COPY Gemfile Gemfile.lock ./
RUN gem install bundler && bundle install --jobs 20 --retry 5

# Copy the main application.
COPY . ./

# Expose port 8080 to the Docker host, so we can access it
# from the outside.
EXPOSE 8080

# The main command to run when the container starts. Also
# tell the Rails dev server to bind to all interfaces by
# default.
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-p", "8080"]

然后我像这样构建它:

 docker build -t demo .

并调用一个命令来启动服务器,该服务器在端口 8080 上启动服务器:

 Johns-MacBook-Pro:demo johnkealy$ docker run -it demo
=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://0.0.0.0:8080
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-04-23 16:50:34] INFO  WEBrick 1.3.1
[2016-04-23 16:50:34] INFO  ruby 2.2.4 (2015-12-16) [x86_64-linux]
[2016-04-23 16:50:34] INFO  WEBrick::HTTPServer#start: pid=1 port=8080

然后我尝试找到正确的 IP 导航到:

 Johns-MacBook-Pro:demo johnkealy$ docker-machine ip default
192.168.99.100

我导航到 http://192.168.99.100:8080 并收到错误此站点无法访问 192.168.99.100 拒绝连接。

我可能做错了什么?

原文由 jdkealy 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

您需要使用以下选项发布公开的端口:

-P(大写)或 –publish-all 将告诉 Docker 使用主机中的随机端口并将它们映射到暴露容器的端口。

-p(小写)或 –publish=[] 将告诉 Docker 使用您手动设置的端口并将它们映射到公开容器的端口。

第二个选项是首选,因为您已经知道映射了哪些端口。如果您使用第一个选项,那么您将需要调用 docker inspect demo 并在 端口 部分检查主机正在使用哪些随机端口。

只需运行以下命令:

 docker run -it -p 8080:8080 demo

之后,您的网址将起作用。

原文由 CodeNotFound 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果您在 windows 10 主页 上使用 Docker 工具包,则需要通过 docker-machine ip 命令访问该网页。一般是192.168.99.100:

假设您正在使用如下发布命令运行。

 docker run -it -p 8080:8080 demo

使用 Window 10 专业版,您可以使用 localhost 或相应的环回 127.0.0.1:8080 等(Tomcat 或任何您想要的)访问。这是因为那里没有虚拟框,并且 docker 直接在 Window Hyper V 上运行,并且可以直接访问环回。

验证窗口中的主机文件是否有任何题外话。它应该有 127.0.0.1 映射到本地主机

原文由 vimal krishna 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进