DEV Community

Cover image for Build a Minimal API with HereDoc and Docker
Mohamad Lawand
Mohamad Lawand

Posted on

Build a Minimal API with HereDoc and Docker

With Docker supporting HereDoc we are now able to create a full minimal API within a docker file, allowing us to use a single file build everything we need.

You can watch the full video on YouTube

# syntax=docker/dockerfile:1.4

# specify base image
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env

# create directory
WORKDIR /app

# Create the csproj file with Here Dock
COPY <<EOF demo.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
    </PropertyGroup>
</Project>
EOF

# restoring the csproj file
RUN dotnet restore

# creating the program.cs for out .net application with heretic
COPY <<EOF Program.cs
    var builder = WebApplication.CreateBuilder(args);
    var app = builder.Build();
    app.MapGet("/hello", () => "Hi There from docker file");
    app.MapGet("/teamLH", () => "7 times world champion");
    app.Run();
EOF

# publishing our application
RUN dotnet publish -c Release -o out

# preparing the final image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
EXPOSE 80
EXPOSE 443
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT [ "dotnet", "demo.dll" ]
Enter fullscreen mode Exit fullscreen mode

Please reach out for any questions or clarifications

Top comments (0)