Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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>
like image 299
Pakpoom Tiwakornkit Avatar asked May 17 '17 02:05

Pakpoom Tiwakornkit


People also ask

Does localhost work in Docker?

Alternatively you can run a docker container with network settings set to host . Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0.0.1 ) will refer to the docker host.

What port should I use for Docker?

By default, the httpd server listens on port 80. It's not mandatory to perform port mapping for all Docker containers.

Does Docker need IP forwarding?

Docker relies on the host being capable of performing certain functions to make Docker networking work. Namely, your Linux host must be configured to allow IP forwarding.


2 Answers

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

like image 188
kegn Avatar answered Sep 25 '22 02:09

kegn


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

like image 27
Anil Konduru Avatar answered Sep 25 '22 02:09

Anil Konduru