Deploy ASP.NET Core with Docker on Elastic Beanstalk¶
This recipe uses Elastic Beanstalk Docker instead of the native .NET platform. It is useful when you need full control over the runtime image, OS packages, or deployment packaging.
Prerequisites¶
- Working ASP.NET Core application.
- Docker installed locally.
- Familiarity with Elastic Beanstalk Docker environments.
What You'll Build¶
You will build:
- A multi-stage Dockerfile for ASP.NET Core.
- A Docker-based Elastic Beanstalk deployment package.
- A validation path for the
/healthendpoint.
Steps¶
- Create a multi-stage Dockerfile.
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .
RUN dotnet publish GuideApi.csproj --configuration Release --output /app/publish
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app
COPY --from=build /app/publish .
ENV ASPNETCORE_URLS=http://0.0.0.0:8080
EXPOSE 8080
ENTRYPOINT ["dotnet", "GuideApi.dll"]
- Build the image locally.
- Create an Elastic Beanstalk Docker environment.
eb init --platform "Docker running on 64bit Amazon Linux 2023" --region "$REGION"
eb create "$ENV_NAME" --single
eb deploy "$ENV_NAME" --staged
- Validate the deployed container.
flowchart TD
A[Source Code] --> B[Docker Multi-stage Build]
B --> C[Runtime Image]
C --> D[Elastic Beanstalk Docker Platform]
D --> E[Container Answers /health] Verification¶
Run these checks after deployment:
Expected outcomes:
- The Docker image starts successfully on Elastic Beanstalk.
- The container listens on the expected port.
/healthreturns200through the environment URL.