C
C#13mo ago
Jan Krüger

✅ 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/... 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 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 AllowedHosts through my AppSettings tho:
"AllowedHosts": "*",
"AllowedHosts": "*",
That results in the application listening on Now listening on: http://[::]:80 which is what I expect. Just for completeness here is my dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app

COPY ./dist/apps/navigatem-pze-api/net7.0/ .

# Set Date/Timezone to Europe/Berlin
RUN ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime

# Add puppeteer dependencies
RUN apt-get update && apt-get install -yq libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libgbm1 libasound2 libpangocairo-1.0-0 libxss1 libgtk-3-0

# Copy the Templates folder
COPY ./apps/navigatem-pze-api/Templates /app/Templates

EXPOSE 80

ENTRYPOINT [ "dotnet", "Application.dll"]
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app

COPY ./dist/apps/navigatem-pze-api/net7.0/ .

# Set Date/Timezone to Europe/Berlin
RUN ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime

# Add puppeteer dependencies
RUN apt-get update && apt-get install -yq libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libgbm1 libasound2 libpangocairo-1.0-0 libxss1 libgtk-3-0

# Copy the Templates folder
COPY ./apps/navigatem-pze-api/Templates /app/Templates

EXPOSE 80

ENTRYPOINT [ "dotnet", "Application.dll"]
2 Replies
Dusty
Dusty13mo ago
Are your headless chrome & api in 2 different containers?
Jan Krüger
Jan KrügerOP13mo ago
Hello no they are not in different containers. I was able to fix it by setting the external hostname inside the hosts file of the container and pointing this to 127.0.0.1
Want results from more Discord servers?
Add your server