✅ Getting Error 400: Invalid Hostname when trying to access application in Docker container
Hello, I have a .net 7 application running inside a Docker container. The .NET application has an endpoint to generate PDFs of a Razor site. It is using pupeteer to generate the pdf, so it needs to access its own page:
I'm trying to access the page using localhost:
http://localhost:80/api/...
http://localhost:80/api/...
That works without any flaws when running the application outside of the docker container during development, but once its deployed it nolonger works.
I'm receiving
400: Invalid Hostname
400: Invalid Hostname
errors when the headless chromium tries to access localhost.
I also tried curling from within the container to localhost and get the same response:
root@89aa2fb2bde5:/app# curl -v http://127.0.0.1:80/api/v1/mitarbeiter* Trying 127.0.0.1:80...* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)> GET /api/v1/mitarbeiter HTTP/1.1> Host: 127.0.0.1> User-Agent: curl/7.74.0> Accept: */*>* Mark bundle as not supporting multiuse< HTTP/1.1 400 Bad Request< Content-Length: 334< Content-Type: text/html< Date: Thu, 09 Nov 2023 17:26:26 GMT< Server: Kestrel<<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><HTML><HEAD><TITLE>Bad Request</TITLE><META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></ HEAD ><BODY><h2>Bad Request - Invalid Hostname</h2><hr><p>HTTP Error 400. The request hostname is invalid.</p>* Connection #0 to host 127.0.0.1 left intact</BODY></HTML>root@89aa2fb2bde5:/app#
root@89aa2fb2bde5:/app# curl -v http://127.0.0.1:80/api/v1/mitarbeiter* Trying 127.0.0.1:80...* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)> GET /api/v1/mitarbeiter HTTP/1.1> Host: 127.0.0.1> User-Agent: curl/7.74.0> Accept: */*>* Mark bundle as not supporting multiuse< HTTP/1.1 400 Bad Request< Content-Length: 334< Content-Type: text/html< Date: Thu, 09 Nov 2023 17:26:26 GMT< Server: Kestrel<<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><HTML><HEAD><TITLE>Bad Request</TITLE><META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></ HEAD ><BODY><h2>Bad Request - Invalid Hostname</h2><hr><p>HTTP Error 400. The request hostname is invalid.</p>* Connection #0 to host 127.0.0.1 left intact</BODY></HTML>root@89aa2fb2bde5:/app#
My Program.cs looks like this:
public static IWebHostBuilder CreateHostBuilder(string[] args) { return WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); }
public static IWebHostBuilder CreateHostBuilder(string[] args) { return WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); }
Inside my Startup I never set any specific host. I do set the