Use Visual Studio 2022 Create .net core 6.0 web api.
it’s work on the Visual Studio with debug use in docker.
but when i create img and use the img create docker container.i can’t access the .net core web api.
just defalut project.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();
use VS create default dockerfile,it’s work on VS.
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["file-api/file-api.csproj", "file-api/"]
RUN dotnet restore "file-api/proever-file-api.csproj"
COPY . .
WORKDIR "/src/proever-file-api"
RUN dotnet build "file-api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "file-api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "file-api.dll"]
http://localhost:4433/GetWeatherForecast,i got http error 404
2
Answers
Your Docker Container is exposed to port 80!
Try the following instand:
By default the docker container is behaving like an isolated machine that runs just your app and doesn’t expose any ports to the outside world. So for example trying to make that request on the container from your laptop which is outside the container and therefore considered outside world fail.
You have to explicitly tell the container to expose the port to the outside world (eg your laptop). This means that if you run the container from the command line you would need to pass the argument -p 5555:5000 where 5555 is a port that you want the api to respond in the outside world and 5000 is the port that the api listens in the container. So you forward the 5555 port of your laptop to the containers 5000 port.
The 5555 port is something you can choose. You could place another available port of your choice. The 80 port however is depending on where the app you want to make the request listens to when it is running. For nginx for example this is the port 80. For net core api it is the port 5000.
So you would need to run:
docker run -d -p 5555:5000 my_image
and then make a request to:
http://localhost:5555/weatherforecast
Important 1: The EXPOSE 80 statement in the dockerfile is only used for documentation purposes. You can think it of more like a comment to let people now on which port the app is running in the container but if it is wrong then this would be incorrect information (which is still ignored however).
Important 2: Do not confuse the ports set in launchsettings.json with those that the app will run in the container. These ports are used only when running locally depending on the selected profile you have chosen (kerstel, iis express) and do not make any difference on running your image (unless you run the docker file from visual studio with the Docker profile selected and you have changed the default configuration created in the launchsettings.json by hand for the docker profile).