0

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.

8
  • Any errors in the container logs? Commented Apr 26, 2024 at 9:14
  • docker logs <container_id> shows me that everything is working fine 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 Commented Apr 26, 2024 at 9:16
  • 1
    Post the logs in the question itself. There's nothing special to GraphQL anyway. You need to create a docker container that forwards the correct external ports to the ports ASP.NET Core listens. In .NET 7 and later though you can publish directly to an image, without having to create a Dockerfile. That reduces the number of things that can go wrong Commented Apr 26, 2024 at 9:19
  • 1
    Also notice Now listening on: http://[::]:8080. You forwarded to 5000 instead. -p 8080:5000 means that the external port 8080 is forwarded to the internal 5000. Nothing listens to that though Commented Apr 26, 2024 at 9:26
  • 1
    Okay that was the main issue. I totally mixed up the ports! Thanks @PanagiotisKanavos Commented Apr 26, 2024 at 9:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.