Currently I'm working on a backend with GraphQL and Hot Chocolate.
The backend gets data from openweathermap.org.
If I'm running dotnet run locally everything is fine. I can see the interface of Banana Cake (http://localhost:5000/graphql). Now I want to use a docker for this. So I've created a Dockerfile and build my container and run it. It is also working. But I can't connect to the graphql interface.
Startup.cs file
using HotChocolate.AspNetCore.Playground;
namespace WeatherBackend
{
public class Startup
{
private readonly IWebHostEnvironment _env;
public Startup(IWebHostEnvironment env)
{
_env = env;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddHttpClient<WeatherService>();
services.AddGraphQLServer()
.AddQueryType<WeatherQueryType>()
.AddApolloTracing()
.AddType<WeatherType>()
.ModifyRequestOptions(opt => opt.IncludeExceptionDetails = _env.IsDevelopment());
services.AddOptions();
services.Configure<OpenWeatherMapOptions>(options =>
{
options.ApiKey = Environment.GetEnvironmentVariable("OpenWeatherMapApiKey");;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseCors(builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapGraphQL("/graphql");
});
}
}
}
Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG configuration=Release
WORKDIR /src
COPY ["WeatherBackend.csproj", "./"]
RUN dotnet restore "WeatherBackend.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WeatherBackend.csproj" -c $configuration -o /app/build
FROM build AS publish
ARG configuration=Release
RUN dotnet publish "WeatherBackend.csproj" -c $configuration -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WeatherBackend.dll"]
Running docker build -t weather-backend-image . and than docker run -p 8080:5000 weather-backend-image:latest and than connecting to http://localhost:5000/graphql no Banana Cake Interface is seen.
Also the logs shows no errors
docker logs 0c750cfe551d
dbug: Microsoft.Extensions.Hosting.Internal.Host[1]
Hosting starting
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://[::]:8080
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: /app
dbug: Microsoft.Extensions.Hosting.Internal.Host[2]
Hosting started
Can someone please help to dockerize my backend, please.
docker logs <container_id>shows me that everything is working finedbug: Microsoft.Extensions.Hosting.Internal.Host[1] Hosting starting info: Microsoft.Hosting.Lifetime[14] Now listening on: http://[::]:8080 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: /app dbug: Microsoft.Extensions.Hosting.Internal.Host[2] Hosting startedNow listening on: http://[::]:8080. You forwarded to 5000 instead.-p 8080:5000means that the external port 8080 is forwarded to the internal 5000. Nothing listens to that though