Skip to content

This provides sample .NET apps using container images with Dockerfile, with MSBuild, and with .NET Aspire support on Docker Desktop.

License

Notifications You must be signed in to change notification settings

devkimchi/msbuild-for-containers

Repository files navigation

MSBuild for Containers

As a .NET developer, when you build a container image for your app, you can use the Dockerfile to define the container image. However, you can also use the dotnet publish command to build and publish the container image without a Dockerfile. This repository provides sample .NET apps using container images with Dockerfile and with dotnet publish.

In addition to that, if you want to orchestrate containers Docker Compose is usually the first approach. However, you can also use the .NET Aspire to generate the Docker Compose file from the .NET Aspire manifest JSON file. This repository also provides a sample .NET app using the .NET Aspire to orchestrate containers.

Prerequisites

Getting Started

Run with Dockerfile

  1. Run docker init to create a new Dockerfile for web app.

    pushd ./MSBuildForContainers.WebApp
    
    docker init
    docker build . -t webapp:latest
    docker run -d -p 3000:8080 webapp:latest
    
    popd
  2. Run docker init to create a new Dockerfile for API app.

    pushd ./MSBuildForContainers.ApiApp
    
    docker init
    docker build . -t apiapp:latest
    docker run -d -p 5050:8080 apiapp:latest
    
    popd
  3. Open the browser and navigate to http://localhost:3000 to see the web app running and http://localhost:5050 to see the API app running.

Run without Dockerfile

  1. Run the following dotnet publish command to build and publish the web app.

    dotnet publish ./MSBuildForContainers.WebApp \
        -t:PublishContainer \
        --os linux --arch x64
    
    docker run -d -p 3000:8080 webapp:latest
  2. Run the following dotnet publish command to build and publish the web app.

    dotnet publish ./MSBuildForContainers.ApiApp \
        -t:PublishContainer \
        --os linux --arch x64
    
    docker run -d -p 5050:8080 apiapp:latest
  3. If you want to change the base image to Ubuntu Chiseled image, use the following command.

    dotnet publish ./MSBuildForContainers.ApiApp \
        -t:PublishContainer \
        --os linux --arch x64 \
        -p:ContainerBaseImage=mcr.microsoft.com/dotnet/aspnet:8.0-noble-chiseled \
        -p:ContainerRepository=apiapp \
        -p:ContainerImageTag=latest
    
    dotnet publish ./MSBuildForContainers.WebApp \
        -t:PublishContainer \
        --os linux --arch x64 \
        -p:ContainerBaseImage=mcr.microsoft.com/dotnet/aspnet:8.0-noble-chiseled \
        -p:ContainerRepository=webapp \
        -p:ContainerImageTag=latest
    
    docker run -d -p 3000:8080 webapp:latest
    docker run -d -p 5050:8080 apiapp:latest
  4. Check Docker Desktop to see the container image size and compare it from the previous step.

  5. Open the browser and navigate to http://localhost:3000 to see the web app running and http://localhost:5050 to see the API app running.

Run with Docker Compose

  1. Update the web app to reference API app by commenting and uncommenting the Program.cs file in the MSBuildForContainers.WebApp project.

  2. Run the following command to rebuild the container image.

    dotnet publish ./MSBuildForContainers.WebApp \
        -t:PublishContainer \
        --os linux --arch x64 \
        -p:ContainerBaseImage=mcr.microsoft.com/dotnet/aspnet:8.0-noble-chiseled \
        -p:ContainerRepository=webapp \
        -p:ContainerImageTag=latest
  3. Run the following docker compose command to run both apps.

    docker compose -f ./docker-compose.yaml up
  4. Open the browser and navigate to http://localhost:3000 to see the web app running and http://localhost:5050 to see the API app running.

Orchestrate with .NET Aspire

  1. Switch to the aspire branch.

    git switch aspire
  2. Run the following command to see whether the .NET Aspire dashboard is running

    dotnet watch run --project ./MSBuildForContainers.AppHost
  3. Generate .NET Aspire manifest JSON file.

    dotnet run --project ./MSBuildForContainers.AppHost \
        -- \
        --publisher manifest \
        --output-path ../aspire-manifest.json
  4. Generate the Docker Compose file from the .NET Aspire manifest JSON file.

    aspirate generate \
        --project-path ./MSBuildForContainers.AppHost \
        --aspire-manifest ./aspire-manifest.json \
        --output-format compose \
        --disable-secrets --include-dashboard false
  5. Run the Docker Compose file generated by the .NET Aspire.

    docker compose -f ./aspirate-output/docker-compose.yaml up
  6. Open the browser and navigate to http://localhost:10002 to see the web app running and http://localhost:10000 to see the API app running.

About

This provides sample .NET apps using container images with Dockerfile, with MSBuild, and with .NET Aspire support on Docker Desktop.

Resources

License

Stars

Watchers

Forks