Docker - Failed to connect to localhost port 4000: Connection refused

转载、翻译自 https://stackoverflow.com/questions/44014698/docker-failed-to-connect-to-localhost-port-4000-connection-refusedpython

按照Docker的安装指导进行操做时,在Get Started, Part 2: Containers这一章,遇到以下问题。
(where you can check up the link here => https://docs.docker.com/get-started/part2/#run-the-app)docker

我使用了和示例中一致的 Dockerfile, requirements.txt浏览器

$ ls
app.py  Dockerfile  requriements.txt

Dockerfile的内容:app

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

三个文件的内容和安装指导中的彻底一致。使用以下指令创建镜像也成功了。
All 3 files have file content/code exactly same as the tutorial also. I was able to build image registry with this commandcurl

$ docker build -t friendlyhello .

一切看起来很正常,生成了frindlyhello的镜像,能够经过如下指令查看镜像ui

$ 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 agothis

运行docker,将80端口映射到4000端口url

$ docker run -d -p 4000:80 friendlyhello

使用docker ps指令检查运行状态翻译

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

官方的指导手册说,能够在浏览器中输入地址 http://localhost:4000 来检测docker是否运行code

惋惜我没法链接到localhost

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

//------------------------------------------------------------------------------------------------------------------------------------
解决方案

查询docker-machine使用的默认IP

$  docker-machine ip default

使用查询到的IP在浏览器中
http://.......:4000

或使用curl指令

$  curl http://$(docker-machine ip default):4000