docker容器上的连接被拒绝

docker容器上的连接被拒绝,docker,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

我是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
我导航到并得到错误,此站点无法访问192.168.99.100拒绝连接


我可能做错了什么

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

-p(大写)或--publish all,这将告诉Docker使用来自主机的随机端口,并将它们映射到公开容器的端口

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

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

只需运行以下命令:

docker run -it -p 8080:8080 demo
$ docker-machine ip 192.168.99.100 

之后,您的url将正常工作。

如果您在window 10 home上使用Docker toolkit,则需要通过Docker machine ip命令访问网页。一般为192.168.99.100:

假设您正在使用下面的publish命令运行

docker run -it -p 8080:8080 demo
使用Windows10Pro版本,您可以使用localhost或相应的环回127.0.0.1:8080等(Tomcat或任何您想要的)进行访问。这是因为您没有虚拟框,docker直接在Window Hyper V上运行,可以直接访问环回

验证窗口中的hosts文件是否有任何偏离。它应该有
127.0.0.1映射到Docker Quickstart终端中的本地主机运行以下命令:

docker run -it -p 8080:8080 demo
$ docker-machine ip 192.168.99.100 

我也有同样的问题。我在Windows Home上使用Docker工具箱。 我不得不使用
http://192.168.99.100:8080/

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

docker-machine ip

上面的命令为我返回了
192.168.99.100

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

假设您暴露了端口5000。运行容器时生成映像后,运行
docker run-p 5000:5000 name
。这将容器的端口5000绑定到笔记本电脑/计算机的端口5000,该端口转发允许容器接收外部请求


在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
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
作为标准用户:

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

尝试使用“发布”选项运行容器。docker run-it demo--publish 8080:8080谢谢,@jozef,但我得到了“docker:Error response from daemon:Container command'--publish'未找到或不存在…”这对我来说很有效!我通过minikube运行Docker,IP是不同的。通过在macOS中运行
minikube ip
获得了它当我运行此命令时,我得到了“未找到命令:docker machine”`你是个天才。这是为我工作的。我正在使用SpringBoot+OpenLDAP,它们都在不同的容器中。通过使用localhost,它不起作用。通过用我的实际ip替换本地主机,成功了。