Docker容器上的连接被拒绝

时间:2016-04-23 16:52:57

标签: 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拒绝连接。

我可能做错了什么?

6 个答案:

答案 0 :(得分:35)

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

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

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

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

只需运行以下命令:

docker run -it -p 8080:8080 demo

之后你的网址就可以了。

答案 1 :(得分:21)

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

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

docker run -it -p 8080:8080 demo

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

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

答案 2 :(得分:2)

在Docker Quickstart Terminal中运行以下命令: $ docker-machine ip 192.168.99.100

答案 3 :(得分:2)

在Windows中,通常还需要以管理员身份运行命令行。

作为标准用户:

docker build -t myimage -f Dockerfile .
Sending build context to Docker daemon  106.8MB
Step 1/1 : FROM mcr.microsoft.com/dotnet/core/runtime:3.0
Get https://mcr.microsoft.com/v2/: dial tcp: lookup mcr.microsoft.com on [::1]:53: read udp [::1]:45540->[::1]:53: read: 
>>>connection refused

但是作为管理员。

docker build -t myimage -f Dockerfile .
Sending build context to Docker daemon  106.8MB
Step 1/1 : FROM mcr.microsoft.com/dotnet/core/runtime:3.0
3.0: Pulling from dotnet/core/runtime
68ced04f60ab: Pull complete                                                                                             e936bd534ffb: Pull complete                                                                                             caf64655bcbb: Pull complete                                                                                             d1927dbcbcab: Pull complete                                                                                             Digest: sha256:e0c67764f530a9cad29a09816614c0129af8fe3bd550eeb4e44cdaddf8f5aa40
Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/runtime:3.0
 ---> f059cd71a22a
Successfully built f059cd71a22a
Successfully tagged myimage:latest

答案 4 :(得分:1)

我有同样的问题。我在Windows Home上使用Docker Toolbox。 我不得不使用localhost来代替http://192.168.99.100:8080/

您可以使用以下命令获取正确的IP地址:

docker-machine ip

以上命令为我返回了192.168.99.100

答案 5 :(得分:1)

Dockerfile中的

命令EXPOSE可让您将容器的端口绑定到主机上的某个端口,但它不会做其他任何事情。 运行容器时,要绑定端口,请指定-p选项。

因此,假设您公开了端口5000。在运行容器时构建映像后,运行docker run -p 5000:5000 name。这会将容器的端口5000绑定到笔记本电脑/计算机的端口5000,并且端口转发功能使容器可以接收外部请求。

这应该做到。