Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connecting to docker with curl

Tags:

docker

curl

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.

like image 920
pongoS Avatar asked Jan 27 '17 05:01

pongoS


People also ask

What is curl command in docker?

curl is a command line tool and library for transferring data with URLs. curl is used in command lines or scripts to transfer data.

How do I run a docker container from an image?

A container is a normal operating system process except that this process is isolated and has its own file system, its own networking, and its own isolated process tree separate from the host. To run an image inside of a container, we use the docker run command. It requires one parameter and that is the image name.


1 Answers

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.

like image 127
Webert Lima Avatar answered Oct 05 '22 13:10

Webert Lima