简体   繁体   中英

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

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>

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

如果您在Mac上运行docker并尝试使用localhost连接到您的dependancies(postgres等), 请将localhost替换为docker.for.mac.localhost

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

在此输入图像描述

docker实例没有在你的本地而不是docker docker-machine ssh上运行。你可以使用docker-machine ssh命令登录,然后在docker-machine上curl "localhost:6666"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM