转载、翻译自 https://*.com/questions/44014698/docker-failed-to-connect-to-localhost-port-4000-connection-refused
按照Docker的安装指导进行操作时,在Get Started, Part 2: Containers这一章,遇到如下问题。
(where you can check up the link here => https://docs.docker.com/get-started/part2/#run-the-app)
我使用了和示例中一致的 Dockerfile, requirements.txt
$ ls
app.py Dockerfile requriements.txt
Dockerfile的内容:
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 command
$ docker build -t friendlyhello .
一切看起来很正常,生成了frindlyhello的镜像,可以通过以下指令查看镜像
$ 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
运行docker,将80端口映射到4000端口
$ 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是否运行
可惜我无法连接到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