3

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

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117

2 Answers2

2

Elastic Beanstalk's proxy per defaults forwards traffic on port 5000. So either you should configure nginx to forward request to the port, your app listens on - 80, or you should make your app listen on port 5000.

your current config:

ingress traffic ---> 80: nginx :any ---5000 --> ??? 80: your container

should be:

ingress traffic ---> 80: nginx :any ----80 ---> 80: your container

or maybe easier to configure:

ingress traffic ---> 80: ngnix :any ---5000 ---> 5000: your container

The latter can be easier changed by simply changing the environment property of your container.

I'm zero in .NET-terms, but look here:

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/dotnet-linux-platform-nginx.html

  • I changed the 80 port in my docker-compose.override.yml to 5000, and the same issue persists. See my updated question. – Ali Bdeir Oct 15 '22 at 20:55
  • Have you checked the log file of your application? Does is start up and run on without any exceptions inside of elastic beanstalk environment? – DmitriKonnov Oct 16 '22 at 15:25
  • No errors at all, in fact, checking eb-stouterror.log, I can find "Now listening on: http://[::]:5000". – Ali Bdeir Oct 16 '22 at 17:56
  • Well, if the requests reach the container and are processed, whereupon valid responses are sent, then something might be off with the elastic load balancer, that could handle the response correctly. Maybe you ain't read this article yet: https://aws.amazon.com/premiumsupport/knowledge-center/load-balancer-http-502-errors/ Otherwise, some other service inside your app might throw this error. – DmitriKonnov Oct 16 '22 at 19:27
1

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.

jccampanero
  • 50,989
  • 3
  • 20
  • 49