Jan Krüger
Jan Krüger
CC#
Created by Jan Krüger on 11/9/2023 in #help
✅ 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"]
4 replies
CC#
Created by Jan Krüger on 7/5/2023 in #help
Return NotFound with details
Hello. I have a asp.net core Web API with controllers and I want to return for example a NotFound Error with a custom message attached, that I can then show to the user.
[HttpGet("{id:guid}", Name = "GetUrlaubsantragById")]
[ProducesResponseType(typeof(UrlaubsantragDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GetUrlaubsantragById([FromRoute] Guid id)
{
var result = await _urlaubsantragService.GetUrlaubsantragAsync(id);
if (result == null)
{
return NotFound();
}

return Ok(result);
}
[HttpGet("{id:guid}", Name = "GetUrlaubsantragById")]
[ProducesResponseType(typeof(UrlaubsantragDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GetUrlaubsantragById([FromRoute] Guid id)
{
var result = await _urlaubsantragService.GetUrlaubsantragAsync(id);
if (result == null)
{
return NotFound();
}

return Ok(result);
}
This for example creates a problem, so the format that I want:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.4",
"title": "Not Found",
"status": 404,
"traceId": "00-cb37e3ba34b597ba19e7cf39665feb58-73e25a81a8b6a327-00"
}
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.4",
"title": "Not Found",
"status": 404,
"traceId": "00-cb37e3ba34b597ba19e7cf39665feb58-73e25a81a8b6a327-00"
}
But when I add a message to the ActionResult I only get a text/plain message:
public async Task<IActionResult> GetUrlaubsantragById([FromRoute] Guid id)
{
var result = await _urlaubsantragService.GetUrlaubsantragAsync(id);
if (result == null)
{
return NotFound("My custom error message");
}

return Ok(result);
}
public async Task<IActionResult> GetUrlaubsantragById([FromRoute] Guid id)
{
var result = await _urlaubsantragService.GetUrlaubsantragAsync(id);
if (result == null)
{
return NotFound("My custom error message");
}

return Ok(result);
}
Does someone have an idea, what is going on?
12 replies