简体   繁体   English

docker 容器上的连接被拒绝

[英]Connection refused on docker container

I'm new to Docker and trying to make a demo Rails app.我是 Docker 的新手,正在尝试制作一个演示 Rails 应用程序。 I made a dockerfile that looks like this:我做了一个看起来像这样的 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"]

I then built it like so:然后我像这样构建它:

docker build -t demo . 

And call a command to start the server which does start the server on port 8080:并调用一个命令来启动服务器,该服务器在端口 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

I then try to find the correct IP to navigate to:然后我尝试找到正确的 IP 导航到:

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

I navigate to http://192.168.99.100:8080 and get the error This site can't be reached 192.168.99.100 refused to connect.我导航到http://192.168.99.100:8080并收到错误此站点无法访问 192.168.99.100 拒绝连接。

What could I be doing wrong?我可能做错了什么?

You need to publish the exposed ports by using the following options:您需要使用以下选项发布暴露的端口:

-P (upper case) or --publish-all that will tell Docker to use random ports from your host and map them to the exposed container's ports. -P(大写)或 --publish-all将告诉 Docker 使用来自主机的随机端口并将它们映射到公开容器的端口。

-p (lower case) or --publish=[] that will tell Docker to use ports you manually set and map them to the exposed container's ports. -p(小写)或 --publish=[]将告诉 Docker 使用您手动设置的端口并将它们映射到公开容器的端口。

The second option is preferred because you already know which ports are mapped.首选第二个选项,因为您已经知道映射了哪些端口。 If you use the first option then you will need to call docker inspect demo and check which random ports are being used from your host at the Ports section.如果您使用第一个选项,那么您将需要调用docker inspect demo并在端口部分检查您的主机正在使用哪些随机端口。

Just run the following command:只需运行以下命令:

docker run -it -p 8080:8080 demo

After that your url will work.之后,您的网址将起作用。

If you are using Docker toolkit on window 10 home you will need to access the webpage through docker-machine ip command.如果您在window 10 home上使用Docker 工具包,则需要通过 docker-machine ip 命令访问网页。 It is generally 192.168.99.100:一般是192.168.99.100:

It is assumed that you are running with publish command like below.假设您正在使用如下所示的发布命令运行。

docker run -it -p 8080:8080 demo

With Window 10 pro version you can access with localhost or corresponding loopback 127.0.0.1:8080 etc (Tomcat or whatever you wish).使用 Window 10 专业版,您可以使用 localhost 或相应的环回 127.0.0.1:8080 等(Tomcat 或您希望的任何内容)进行访问。 This is because you don't have a virtual box there and docker is running directly on Window Hyper V and loopback is directly accessible.这是因为那里没有虚拟框,而且 docker 直接在 Window Hyper V 上运行,并且可以直接访问环回。

Verify the hosts file in window for any digression.验证窗口中的主机文件是否有任何离题。 It should have 127.0.0.1 mapped to localhost它应该有 127.0.0.1 映射到本地主机

I had the same problem.我有同样的问题。 I was using Docker Toolbox on Windows Home.我在 Windows Home 上使用 Docker Toolbox。 Instead of localhost I had to use http://192.168.99.100:8080/ .我不得不使用http://192.168.99.100:8080/而不是localhost

You can get the correct IP address using the command:您可以使用以下命令获取正确的 IP 地址:

docker-machine ip

The above command returned 192.168.99.100 for me.上面的命令为我返回了192.168.99.100

在 Docker 快速入门终端中运行以下命令:

$ docker-machine ip 192.168.99.100 

Command EXPOSE in your Dockerfile lets you bind container's port to some port on the host machine but it doesn't do anything else. EXPOSE中的命令EXPOSE允许您将容器的端口绑定到主机上的某个端口,但它不会执行任何其他操作。 When running container, to bind ports specify -p option.运行容器时,要绑定端口指定-p选项。

So let's say you expose port 5000. After building the image when you run the container, run docker run -p 5000:5000 name .因此,假设您公开了端口 5000。在运行容器时构建映像后,运行docker run -p 5000:5000 name This binds container's port 5000 to your laptop/computers port 5000 and that portforwarding lets container to receive outside requests.这将容器的端口 5000 绑定到您的笔记本电脑/计算机的端口 5000,并且该端口转发允许容器接收外部请求。

This should do it.这应该做。

In Windows, you also normally need to run command line as administrator.在 Windows 中,您通常还需要以管理员身份运行命令行。

As standard-user:作为标准用户:

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

But as an administrator.但是作为管理员。

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

Make sure that you use the -p flag before the image name like this:确保在图像名称之前使用-p标志,如下所示:

docker run -p 8080:8080 demo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM