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