Docker - Failed to connect to localhost port 4000:

2019-04-04 17:29发布

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)

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

$ ls
app.py  Dockerfile  requriements.txt

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"]

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

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

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.

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'

It seems like it doesn't work either.

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
4条回答
对你真心纯属浪费
2楼-- · 2019-04-04 18:17

The docker instance is not running on your local rather than docker machine.You can use docker-machine ssh command login, and then curl "localhost:6666" on the docker-machine.

查看更多
做自己的国王
3楼-- · 2019-04-04 18:28

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).

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

$ docker-machine ip default

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 default):4000

查看更多
Bombasti
4楼-- · 2019-04-04 18:28

if you are running your docker on Mac and trying to connect to your dependancies (postgres etc) using localhost, replace localhost with docker.for.mac.localhost

查看更多
Juvenile、少年°
5楼-- · 2019-04-04 18:28

You are using port 9089 in your docker config but your program or server running on different port. To check the port for xampp you can use below method or Try to search google to check the port number :

Go to xampp control panel and see the port number below image. I have marked that red color. In my case, port number is 80

enter image description here

查看更多
登录 后发表回答