Docker容器连接被拒绝

90

我刚开始接触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 页面时出现错误提示:This site can’t be reached 192.168.99.100 refused to connect。

可能是哪里出了问题?


1
尝试使用发布选项运行容器。docker run -it demo --publish 8080:8080 - Jozef Vaclavik
1
谢谢 @jozef,但我收到了“docker: Error response from daemon: Container command '--publish' not found or does not exist.”的错误信息。 - jdkealy
11个回答

84

您需要使用以下选项来发布暴露的端口:

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

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

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

只需运行以下命令:

docker run -it -p 8080:8080 demo

之后你的url将会正常工作。


14
是的,请记得在图像名称之前放置“-p”,而不是之后。 - WesternGun

42
如果你正在使用Docker toolkitwindow 10 home上,你需要通过docker-machine ip命令访问网页。一般来说它是192.168.99.100:
假设你正在运行像下面这样的发布命令。
docker run -it -p 8080:8080 demo

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

检查Windows中的hosts文件是否存在任何偏差。它应将127.0.0.1映射到localhost。


这对我有用!我通过minikube运行Docker,IP地址不同。通过运行minikube ip得到了它。 - jiggy

16

我遇到了同样的问题。我在Windows Home上使用Docker Toolbox。 我需要使用http://192.168.99.100:8080/代替localhost

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

docker-machine ip

对我来说,上述命令返回了192.168.99.100


10
在 macOS 上运行时,我收到了 command not found: docker-machine 的错误提示。 - Surya
你真是个天才。这对我很有用。我正在使用Spring Boot + OpenLDAP,它们都在单独的容器中。使用localhost无法工作。将localhost替换为我的实际IP地址就解决了问题。 - Linksx

7

EXPOSE 命令可以在 Dockerfile 中将容器的端口绑定到主机上的某个端口,但并不做任何其他操作。运行容器时,要绑定端口,请指定-p选项。

假设您暴露了5000端口。构建镜像后运行容器,运行 docker run -p 5000:5000 name。这将把容器的5000端口绑定到您的笔记本电脑/计算机的5000端口,并进行端口转发,使容器能够接收外部请求。

这样就可以了。


7
在 Docker 快速入门终端中运行以下命令:
$ docker-machine ip 192.168.99.100 

3
在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

2
在某些情况下,Docker容器中的内部进程试图绑定到localhost(或显式地绑定到127.0.0.1),但这可能是一个错误。考虑尝试绑定到0.0.0.0。这在过去对我来说是一个常见的问题。

问题中的Dockerfile已经有一个rails server -b 0.0.0.0选项。 - David Maze

1
请注意,在图像名称前使用-p标志,像这样:
docker run -p 8080:8080 demo

0
如果有人使用Docker Compose,请确保您的容器位于相同的网络桥上,例如:
  container-1:
    networks:
     - app-network

  container-2:
    networks:
     - app-network

0

在Ubuntu中使用Python和Docker的最常用方法

在项目的根目录下运行以下命令来创建包含虚拟环境所需包的requirements.txt文件:

pip freeze > requirements.txt

在项目根目录下创建一个新的 'Dockerfile':
FROM python:3.10.12
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt --quiet
COPY . .
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

执行bash命令来构建docker镜像:
docker build -t my-app-img:0.1.0 .

执行bash命令以运行图像:
docker run --rm -d -p 8000:8000/tcp doospy-api:0.1.0

使用浏览器访问它:
http://localhost:8000/

Ubuntu 22.04,Python 3.10.12,Docker 24.0.7,Venv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接