Reputation: 336
I'm following this tutorial on docker which sets up a small hello world server.
This is the expected behavior:
docker run -d -p 5000:5000 training/webapp:latest python app.py
curl http://dockerhost:5000
Hello world!
However, I get an error when I run
curl http://dockerhost:5000
curl: (6) Could not resolve host: dockerhost
I looked at some other answers and tried
ping http;//dockerhost:5000
unknown host http://dockerhost:5000
nslookup http://dockerhost:5000
;; connection timed out; no servers could be reached
netstat -utapen | grep 5000
tcp6 0 0 :::5000 :::* LISTEN 0 251303 -
Some stackoverflow answers (curl: (6) Could not resolve host: google.com; Name or service not known) were talking about DNS issues (wrong DNS server), but I don't know what to do to see if this is relevant to my problem.
Upvotes: 17
Views: 85894
Reputation: 14527
Motivation and Solution: You run some service and you want to expose it to the outside world (in default - the container is open and talk to other services if they run together - for example like web application service and some db service like redis/postgress/mongo that are running together on same machine and "talk" to each other) But accesing it in blackbox world is different and try to get in is forbidden. you don't have permission. therefore you should configure the specific port explicitly on your docker command or on your docker-compose.yml
file the port attribute, and connect through this link:
For example lets say on my docker-compose.yml
file , configure port
attribute:
ports:
- "0.0.0.0:8080:8080"
Verify it using docker ps
to watch all running containers:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123asd456 someService/someService:lts "/sbin/tini -- /usr/…" 10 weeks ago Up 10 days 0.0.0.0:8080->8080/tcp, 50000/tcp serviceName
and access it directly from localhost: 0.0.0.0:8080
curl -X GET 0.0.0.0:8080/api/some-api-function
Upvotes: 13
Reputation: 111
if you try
curl name_of_container:5000/path/to/method
(without http://)
should work
Upvotes: -4
Reputation: 14035
This is more a DNS
question.
When you use commands such as curl
, ping
or nslookup
or other commands that open connections to other ends, they expect you to inform the IP Address of the other end, or a name that can be resolved to an IP.
On your case, dockerhost
is a name, not an IP, and your system can't resolve this name to an IP Address.
There are many solutions, like the one put above.
You can inform your system the IP Address of the name dockerhost
but inserting a line in the /etc/hosts
file (if you're on a Linux box):
echo "127.0.0.1 dockerhost" >> /etc/hosts
or just edit the file C:\Windows\System32\drivers\etc
if you're on a Windows machine.
That tells your system that the IP Address of the name dockerhost
is 127.0.0.1
, which is the same as your localhost
, or, your own machine.
Now you should be able to ping
it:
ping dockerhost
node that ping
and nslookup
expect just a name, without the protocol (http://
) and port (5000
). curl
does expect the protocol and port.
Either that or you can use just the IP 127.0.0.1
instead of dockerhost
.
Upvotes: 7
Reputation: 1770
You have to add
dockerhost 127.0.0.1
in your hosts file or use 127.0.0.1 to access the container. Also, I recommend using a name in your docker run command as in:
docker run -d -p 5000:5000 --name dockerhost training/webapp:latest python app.py
When you run multiple containers, if you put them in the same network, you can connect to containers from the other containers by the name you give them.
Upvotes: 2