Docker-无法连接到本地主机端口4000:连接被拒绝 [英] Docker - Failed to connect to localhost port 4000: Connection refused

查看:558
本文介绍了Docker-无法连接到本地主机端口4000:连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新手,我想通过遵循官方网站上的教程来熟悉Docker。现在,我陷入了教程的第2部分(您可以在此处查看链接=> https://docs.docker.com/get-started/part2/#run-the-app

Hi I'm very new to Docker, I'm trying to get familiar with Docker by following the tutorial on official site. Now I get stuck at part 2 of the tutorial (where you can check up the link here => https://docs.docker.com/get-started/part2/#run-the-app)

我有示例应用程序代码,Dockerfile和requirements.txt与官方教程完全相同

I have sample application code, Dockerfile, and requirements.txt exactly same as the offical tutorial

$ ls
app.py  Dockerfile  requriements.txt

我的Dockerfile看起来像这样

My Dockerfile looks like this

FROM python:2.7-slim
WORKDIR /app
ADD . /app
RUN pip install -r requriements.txt
EXPOSE 80
ENV NAME World
CMD ["python", "app.py"]

所有3个文件的文件内容/代码都与本教程完全相同。我能够使用此命令构建映像注册表

All 3 files have file content/code exactly same as the tutorial also. I was able to build image registry with this command

$ docker build -t friendlyhello .

一切看起来都很不错。现在我有示例项目图像注册表。

Everything looks great. Now I had sample project image registry.

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED
friendlyhello       latest              82b8a0b52e91        39 minutes ago
python              2.7-slim            1c7128a655f6        5 days ago
hello-world         latest              48b5124b2768        4 months ago

然后我使用此命令根据官方教程运行应用程序

I then ran the app according to the official tutorial with this command

$ docker run -d -p 4000:80 friendlyhello
c1893f7eea9f1b708f639653b8eba20733d8a45d3812b442bc295b43c6c7dd5c

编辑:这是在执行上述命令后我的容器

Edit: This is my container after ran above command

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS
c1893f7eea9f        friendlyhello       "python app.py"     15 minutes ago      Up 16 minutes

官方教程指导读者看一下 http:// localhost:4000 ,因为他们已经映射了机器端口 4000 到容器端口 80

And the official tutorial guides readers to have a look at http://localhost:4000 as they have already mapped machine port 4000 to container port 80

不幸的是,我无法从该URL得到任何响应。

Unfortunately, I couldn't get any response from that URL.

$ curl http://localhost:4000
curl: (7) Failed to connect to localhost port 4000: Connection refused

我是新手,我也不知道该怎么办...。如何我上班了吗?
预先感谢您的任何答复。

I'm totally newbie and I have no idea what to do....How can I get it to work ? Thanks in advance for any response.

编辑:我按照@ johnharris85的建议进行操作。下面是输出

Edit: I did as @johnharris85 suggested. Below is the output

$ curl http://$(echo docker-machine ip default):4000
curl: (6) Couldn't resolve host 'docker-machine'
curl: (6) Couldn't resolve host 'ip'
curl: (6) Couldn't resolve host 'default'

似乎也不起作用。

编辑:@ johnharris85更正了他的建议,@ user8023051阐明了此命令的来源以及幕后的情况。它现在正在工作:)谢谢

Edit: @johnharris85 corrected his suggestion and @user8023051 clarify how this command come from and what is going on under the hood. It is working now :) Thanks

$ curl http://$(docker-machine ip default):4000
<h3>Hello World!</h3><b>Hostname:</b> c1893f7eea9f<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>


推荐答案

我对docker不太熟悉,但是它听起来您的设置是这样的:您的docker实例正在虚拟机中运行,并且您正尝试从Windows计算机访问绑定到localhost(即vm)的应用程序。您会在curl上被拒绝的原因是,实际上在主机(Windows)的端口4000上没有监听。

I'm not very familiar with docker, but it sounds like your setup is such that your docker instance is running in a virtual machine, and you're trying to access an application bound to localhost (the vm) from your Windows machine. The reason you would get a refusal here from curl is because nothing is actually listening on port 4000 on the host (Windows).

尝试通过以下方式查找您的Docker实例正在使用的IP:

Try to find the IP that your docker instance is using by:

$ docker-machine ip default

现在您知道IP地址,请再次尝试curl。您甚至可以像下面这样在命令中对其进行求值:

Now that you know the IP address, try curl again. You can even have it evaluated within the command like so:

$ curl http:// $(docker-machine ip默认值):4000

这篇关于Docker-无法连接到本地主机端口4000:连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆