curl: (7) Failed to connect to www.example.com port 80: Connection timed out

  curl, docker

I cannot seem to connect to any web site from inside a docker container.

From the host machine, curl works:

$ curl -v -4 http://www.example.com/
*   Trying 93.184.216.34:80...
* TCP_NODELAY set
* Connected to www.example.com (93.184.216.34) port 80 (#0)
> GET / HTTP/1.1
> Host: www.example.com
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Age: 574872
< Cache-Control: max-age=604800
< Content-Type: text/html; charset=UTF-8
< Date: Wed, 03 Feb 2021 00:44:49 GMT
< Etag: "3147526947"
< Expires: Wed, 10 Feb 2021 00:44:49 GMT
< Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
... snipped ...

But when I do the same from inside the container:

$ docker-compose exec app sh

$ curl -v -4 http://www.example.com/
* Expire in 0 ms for 6 (transfer 0x557e9580ff90)
* Expire in 1 ms for 1 (transfer 0x557e9580ff90)
* Expire in 0 ms for 1 (transfer 0x557e9580ff90)
* Expire in 2 ms for 1 (transfer 0x557e9580ff90)
* Expire in 1 ms for 1 (transfer 0x557e9580ff90)
* Expire in 1 ms for 1 (transfer 0x557e9580ff90)
* Expire in 4 ms for 1 (transfer 0x557e9580ff90)
* Expire in 2 ms for 1 (transfer 0x557e9580ff90)
* Expire in 2 ms for 1 (transfer 0x557e9580ff90)
* Expire in 2 ms for 1 (transfer 0x557e9580ff90)
*   Trying 93.184.216.34...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x557e9580ff90)
* connect to 93.184.216.34 port 80 failed: Connection timed out
* Failed to connect to www.example.com port 80: Connection timed out
* Closing connection 0
curl: (7) Failed to connect to www.example.com port 80: Connection timed out

Same thing happens with ufw enabled or disabled.

I hope someone can point me in the right direction.

Source: Docker Questions

LEAVE A COMMENT