DEV Community

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

Posted on

5 3

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay