skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search