I am trying to run an asp.net web api with docker container but I get status code 400 (Bad request) as the response when I send a request to the api. The api works fine when I run it via visual studio. I mean that I can send a request to the api and get back desired response.
this is the StartUp.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApi_Token v1"));
}
//app.UseHttpsRedirection(); // this is commented out just to create container
app.UseCors(c => c.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
here is the launchSettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:65362/",
"sslPort": 44304
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApi_Token": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
//"applicationUrl": "https://localhost:44305;http://localhost:44304"
"applicationUrl": "http://localhost:44304"
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"publishAllPorts": true,
"useSSL": false
},
"WSL": {
"commandName": "WSL2",
"launchBrowser": true,
//"launchUrl": "https://localhost:4001",
"launchUrl": "http://localhost:4001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "https://localhost:4001;http://localhost:4000"
},
"distributionName": ""
}
}
}
And here is my Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_URLS=http://+:80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["myApi.csproj", "./"]
RUN dotnet restore "./myApi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "myApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "myApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "myApi.dll"]
I really appreciate any help!
2
Answers
I now nothing about dotnet and wanted to build your API but you didnt send all the required files so what i can propose is just replacing all "localhost" occurences by "0.0.0.0", this should work.
Keep me informed please, and if this doesnt work just paste a single API (like a hello world) to test, and how to build it etc. I will do all the testing here.
bguess.
It worked for me to add
"urls=http://0.0.0.0:80"
to the entrypoint of my Dockerfile.In your case, it would be something like