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.
Can someone please help to dockerize my backend, please.