Search code examples
asp.netamazon-web-servicesdockernginxamazon-elastic-beanstalk

502 Bad Gateway when accessing Dockerized ASP.NET Core Web API on Elastic Beanstalk


My docker-compose.override.yml is as follows:

version: '3.4'

services:
  helen.api:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://+:5000
    ports:
      - "5001:443"
      - "5000:80"
    volumes:
      - ~/.aspnet/https:/root/.aspnet/https:ro
      - ~/.microsoft/usersecrets:/root/.microsoft/usersecrets:ro

Dockerfile:

#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
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyProjectWebAPI/MyProject.API.csproj", "MyProjectWebAPI/"]
COPY ["MyProject.Globals/MyProject.Globals.csproj", "MyProject.Globals/"]
RUN dotnet restore "MyProjectWebAPI/MyProject.API.csproj"
COPY . .
WORKDIR "/src/MyProjectWebAPI"
RUN dotnet build "MyProject.API.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyProject.API.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.API.dll"]

Docker-compose.yml

version: "3.4"

services:
  helen.api:
    image: ${DOCKER_REGISTRY-}helenapi
    build:
      context: .
      dockerfile: HelenWebAPI/Dockerfile

When trying to access any URL of the Dockerized ASP.NET Web API, I get a 502 Bad Gateway Error:

img


Solution

  • Perhaps the problem could be motivated that, according to the AWS documentation:

    If you manage your Docker environment with Docker Compose, Elastic Beanstalk assumes that you run a proxy server as a container. Therefore it defaults to None for the Proxy server setting, and Elastic Beanstalk does not provide an NGINX configuration.

    Note

    Even if you select NGINX as a proxy server, this setting is ignored in an environment with Docker Compose. The Proxy server setting still defaults to None.

    Please, following the suggested advice, try providing your own nginx container and the corresponding configuration in one of your docker-compose files. For example:

    version: '3.4'
    
    services:
      helen.api:
        environment:
          - ASPNETCORE_ENVIRONMENT=Development
          - ASPNETCORE_URLS=http://+:5000
        volumes:
          - ~/.aspnet/https:/root/.aspnet/https:ro
          - ~/.microsoft/usersecrets:/root/.microsoft/usersecrets:ro
      nginx:
        image: nginx:alpine
        volumes:
          - ~/nginx.conf:/etc/nginx/nginx.conf:ro
        depends_on:
          - helen.api
        ports:
          - "80:80"
    

    Microsoft as well as AWS provide excellent guides about how your nginx.conf file could look like:

    server {
        listen        80;
        
        location / {
            proxy_pass         http://helen.api:5000;
            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            proxy_set_header   Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
        }
    }
    

    Note we are referencing the container helen.api as the server which is being proxied and that we are using port 5000 as configured in the ASPNETCORE_URLS setting.