1

I have a web api application, I need to run it in a docker container. This is my dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS base
WORKDIR /app
EXPOSE 80
    
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY *.csproj .
RUN dotnet restore "WebApp.csproj"
COPY . .
    
    
WORKDIR "/src/WebApp"
RUN dotnet build "../WebApp.csproj" -c Release -o /app/build
    
FROM build AS publish
RUN dotnet publish "../WebApp.csproj" -c Release -o /app/publish
    
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApp.dll"]

This is my Program.cs config:

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.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});
    
app.Run();

Those are logs of an app:


    info: Microsoft.Hosting.Lifetime[14]
          Now listening on: http://localhost:80
    info: Microsoft.Hosting.Lifetime[0]
          Application started. Press Ctrl+C to shut down. 
    info: Microsoft.Hosting.Lifetime[0]
          Hosting environment: Production
    info: Microsoft.Hosting.Lifetime[0]
          Content root path: C:\app\

But the application is not working when I try to open it in chrome, it gives me 'This site can’t be reached'

Command To Build Image:

docker build --pull -t web-app .

Command To Run Image:

docker run -d -p 5000:80 -it web-app

Stack
  • 11
  • 5
  • Please [edit] the post and add the command used to start the container. – Turing85 Jul 03 '22 at 10:36
  • @Turing85 i do this thank you – Stack Jul 03 '22 at 10:40
  • 1
    In the `Dockerfile`, you wrote `EXPOSE 80`, suggesting that the application is running on port 80, but in the `docker run...` command, you export `-p 5000:5000`. On what port is the application *actually* listening? – Turing85 Jul 03 '22 at 11:06
  • @Turing85 first i try with 80 and not working and after i try with 5000 – Stack Jul 03 '22 at 11:10
  • When you start the application locally (i.e. not in docker), what port is it listening to? – Turing85 Jul 03 '22 at 11:10
  • @Turing85 56916 – Stack Jul 03 '22 at 11:15
  • That looks suspiciously high... anyway. If this is the port (and it's always the same, i.e. not random), then this is the port we should `EXPOSE` in the `Dockerfile` and forward with `docker run -p ...`. On the other hand, the application reports listening on `http://localhost:5000`.... you try to access the app through `http://localhost:5000`? – Turing85 Jul 03 '22 at 11:19
  • @Turing85 Not working ! – Stack Jul 03 '22 at 11:49
  • @Turing85 `EXPOSE` is merely documentation, it only declares that you are listening on that port, but it doesn't actually have any impact in the app itself. – jccampanero Jul 03 '22 at 15:00
  • @jccampanero I know. – Turing85 Jul 03 '22 at 15:05
  • Sorry @Turing85, I brought it up just to clarify the point to the OP to avoid confusion due to your previous comment. – jccampanero Jul 03 '22 at 15:11
  • @jccampanero np :) – Turing85 Jul 03 '22 at 15:14
  • what are you trying to access from chrome, swagger page or direct api call ? in case you trying swagger then it won't be accessible due to condition `app.Environment.IsDevelopment()` – CodingMytra Jul 04 '22 at 05:27

0 Answers0