Cannot resolve host from /etc/hosts file

Docker image: nextcloud:26.0
Nextcloud version: 26.0.0
Operating system and version: Debian 11 Bullseye
Nginx docker image: nginx:1.23.3
PHP version: 8.1.17

I use dockerized NextCloud and OpenProject behind the reverse proxy; SSL certificate is signed by Let’s Encrypt. The setup is similar to the one described in this SO question.

Docker compose templates are:

In order to make things more modular, I bind service ports to docker host machine ports.

I’m able to access and register NextCloud instance in OpenProject by modifying hosts file and adding Let’s Encrypt’s CA certificate to trusted certificates (manually for now).

When I do the same thing for NextCloud, I’m able to access my OpenProject instance from within the NextCloud container via curl https://openproject.example.com, but NextCloud fails to connect to OpenProject and throws “no route to host” error as if the NextCloud does not respect /etc/hosts file.

Error log
{
  "reqId": "S0WgZmGaLLbRxQYNGZHh",
  "level": 3,
  "time": "2023-04-07T10:23:44+00:00",
  "remoteAddr": "172.20.0.1",
  "user": "admin",
  "app": "integration_openproject",
  "method": "POST",
  "url": "/apps/integration_openproject/is-valid-op-instance",
  "message": "A network error occurred while trying to connect to the OpenProject URL 'https://openproject.example.com'",
  "userAgent": "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0",
  "version": "26.0.0.11",
  "exception": {
    "Exception": "GuzzleHttp\\Exception\\ConnectException",
    "Message": "cURL error 7: Failed to connect to openproject.example.com port 443: No route to host (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://openproject.example.com/api/v3/",
    "Code": 0,
    "Trace": [
      {
        "file": "/var/www/html/3rdparty/guzzlehttp/guzzle/src/Handler/CurlFactory.php",
        "line": 158,
        "function": "createRejection",
        "class": "GuzzleHttp\\Handler\\CurlFactory",
        "type": "::",
        "args": [
          "*** sensitive parameters replaced ***"
        ]
      },
      {
        "file": "/var/www/html/3rdparty/guzzlehttp/guzzle/src/Handler/CurlFactory.php",
        "line": 110,
        "function": "finishError",
        "class": "GuzzleHttp\\Handler\\CurlFactory",
        "type": "::",
        "args": [
          [
            "GuzzleHttp\\Handler\\CurlHandler"
          ],
          "*** sensitive parameters replaced ***",
          [
            "GuzzleHttp\\Handler\\CurlFactory"
          ]
        ]
      },
      {
        "file": "/var/www/html/3rdparty/guzzlehttp/guzzle/src/Handler/CurlHandler.php",
        "line": 47,
        "function": "finish",
        "class": "GuzzleHttp\\Handler\\CurlFactory",
        "type": "::",
        "args": [
          [
            "GuzzleHttp\\Handler\\CurlHandler"
          ],
          "*** sensitive parameters replaced ***",
          [
            "GuzzleHttp\\Handler\\CurlFactory"
          ]
        ]
      },
      {
        "file": "/var/www/html/lib/private/Http/Client/DnsPinMiddleware.php",
        "line": 150,
        "function": "__invoke",
        "class": "GuzzleHttp\\Handler\\CurlHandler",
        "type": "->",
        "args": [
          "*** sensitive parameters replaced ***",
          "*** sensitive parameters replaced ***"
        ]
      },
      {
        "file": "/var/www/html/3rdparty/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php",
        "line": 35,
        "function": "OC\\Http\\Client\\{closure}",
        "class": "OC\\Http\\Client\\DnsPinMiddleware",
        "type": "->",
        "args": [
          "*** sensitive parameters replaced ***"
        ]
      },
      {
        "file": "/var/www/html/3rdparty/guzzlehttp/guzzle/src/Middleware.php",
        "line": 31,
        "function": "__invoke",
        "class": "GuzzleHttp\\PrepareBodyMiddleware",
        "type": "->",
        "args": [
          "*** sensitive parameters replaced ***",
          "*** sensitive parameters replaced ***"
        ]
      },
      {
        "file": "/var/www/html/3rdparty/guzzlehttp/guzzle/src/RedirectMiddleware.php",
        "line": 55,
        "function": "GuzzleHttp\\{closure}",
        "class": "GuzzleHttp\\Middleware",
        "type": "::",
        "args": [
          "*** sensitive parameters replaced ***"
        ]
      },
      {
        "file": "/var/www/html/3rdparty/guzzlehttp/guzzle/src/Middleware.php",
        "line": 63,
        "function": "__invoke",
        "class": "GuzzleHttp\\RedirectMiddleware",
        "type": "->",
        "args": [
          "*** sensitive parameters replaced ***",
          "*** sensitive parameters replaced ***"
        ]
      },
      {
        "file": "/var/www/html/3rdparty/guzzlehttp/guzzle/src/HandlerStack.php",
        "line": 75,
        "function": "GuzzleHttp\\{closure}",
        "class": "GuzzleHttp\\Middleware",
        "type": "::",
        "args": [
          "*** sensitive parameters replaced ***"
        ]
      },
      {
        "file": "/var/www/html/3rdparty/guzzlehttp/guzzle/src/Client.php",
        "line": 331,
        "function": "__invoke",
        "class": "GuzzleHttp\\HandlerStack",
        "type": "->",
        "args": [
          "*** sensitive parameters replaced ***",
          "*** sensitive parameters replaced ***"
        ]
      },
      {
        "file": "/var/www/html/3rdparty/guzzlehttp/guzzle/src/Client.php",
        "line": 168,
        "function": "transfer",
        "class": "GuzzleHttp\\Client",
        "type": "->",
        "args": [
          "*** sensitive parameters replaced ***",
          "*** sensitive parameters replaced ***"
        ]
      },
      {
        "file": "/var/www/html/3rdparty/guzzlehttp/guzzle/src/Client.php",
        "line": 187,
        "function": "requestAsync",
        "class": "GuzzleHttp\\Client",
        "type": "->",
        "args": [
          "*** sensitive parameters replaced ***"
        ]
      },
      {
        "file": "/var/www/html/lib/private/Http/Client/Client.php",
        "line": 226,
        "function": "request",
        "class": "GuzzleHttp\\Client",
        "type": "->",
        "args": [
          "*** sensitive parameters replaced ***",
          "https://openproject.example.com/api/v3/",
          [
            "/var/www/html/data/files_external/rootcerts.crt",
            30,
            false,
            [
              "Bearer ",
              "Nextcloud OpenProject integration",
              "gzip"
            ],
            [
              false
            ],
            "And 1 more entries, set log level to debug to see all entries"
          ]
        ]
      },
      {
        "file": "/var/www/html/custom_apps/integration_openproject/lib/Service/OpenProjectAPIService.php",
        "line": 311,
        "function": "get",
        "class": "OC\\Http\\Client\\Client",
        "type": "->",
        "args": [
          "https://openproject.example.com/api/v3/",
          [
            false,
            [
              "Bearer ",
              "Nextcloud OpenProject integration"
            ]
          ]
        ]
      },
      {
        "file": "/var/www/html/custom_apps/integration_openproject/lib/Controller/OpenProjectAPIController.php",
        "line": 355,
        "function": "rawRequest",
        "class": "OCA\\OpenProject\\Service\\OpenProjectAPIService",
        "type": "->",
        "args": [
          "",
          "https://openproject.example.com",
          "",
          [],
          "GET",
          [
            false,
            [
              "Bearer ",
              "Nextcloud OpenProject integration"
            ]
          ]
        ]
      },
      {
        "file": "/var/www/html/lib/private/AppFramework/Http/Dispatcher.php",
        "line": 230,
        "function": "isValidOpenProjectInstance",
        "class": "OCA\\OpenProject\\Controller\\OpenProjectAPIController",
        "type": "->",
        "args": [
          "https://openproject.example.com"
        ]
      },
      {
        "file": "/var/www/html/lib/private/AppFramework/Http/Dispatcher.php",
        "line": 137,
        "function": "executeController",
        "class": "OC\\AppFramework\\Http\\Dispatcher",
        "type": "->",
        "args": [
          [
            "OCA\\OpenProject\\Controller\\OpenProjectAPIController"
          ],
          "isValidOpenProjectInstance"
        ]
      },
      {
        "file": "/var/www/html/lib/private/AppFramework/App.php",
        "line": 183,
        "function": "dispatch",
        "class": "OC\\AppFramework\\Http\\Dispatcher",
        "type": "->",
        "args": [
          [
            "OCA\\OpenProject\\Controller\\OpenProjectAPIController"
          ],
          "isValidOpenProjectInstance"
        ]
      },
      {
        "file": "/var/www/html/lib/private/Route/Router.php",
        "line": 315,
        "function": "main",
        "class": "OC\\AppFramework\\App",
        "type": "::",
        "args": [
          "OCA\\OpenProject\\Controller\\OpenProjectAPIController",
          "isValidOpenProjectInstance",
          [
            "OC\\AppFramework\\DependencyInjection\\DIContainer"
          ],
          [
            "integration_openproject.openProjectAPI.isValidOpenProjectInstance"
          ]
        ]
      },
      {
        "file": "/var/www/html/lib/base.php",
        "line": 1055,
        "function": "match",
        "class": "OC\\Route\\Router",
        "type": "->",
        "args": [
          "/apps/integration_openproject/is-valid-op-instance"
        ]
      },
      {
        "file": "/var/www/html/index.php",
        "line": 36,
        "function": "handleRequest",
        "class": "OC",
        "type": "::",
        "args": []
      }
    ],
    "File": "/var/www/html/3rdparty/guzzlehttp/guzzle/src/Handler/CurlFactory.php",
    "Line": 210,
    "message": "A network error occurred while trying to connect to the OpenProject URL 'https://openproject.example.com'",
    "exception": {},
    "CustomMessage": "A network error occurred while trying to connect to the OpenProject URL 'https://openproject.example.com'"
  }
}

Is there something special I have to do to add hostname to IP binding for NextCloud?

PS: due to 4 link limitation, I had to skip some extra details.