Skip to content

Instantly share code, notes, and snippets.

@rwcitek
Last active December 24, 2023 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwcitek/e11fe43af7595b901389c533289506e4 to your computer and use it in GitHub Desktop.
Save rwcitek/e11fe43af7595b901389c533289506e4 to your computer and use it in GitHub Desktop.
.NET Core in Docker

Create a background container

docker container run \
  --rm \
  --detach \
  --name iis \
  --publish 8080:5000 \
  mcr.microsoft.com/dotnet/sdk:latest \
    sleep inf

sleep 2

Configure it

{
cat <<'eof2'
apt-get update &&
  apt-get install -y tree procps vim net-tools curl

mkdir -p /tmp/project
cd /tmp/project
dotnet new web -n HelloWorldApp

cd HelloWorldApp/
mkdir Controllers
cut -c3- <<'eof' > Controllers/HomeController.cs
  using Microsoft.AspNetCore.Mvc;
  
  public class HomeController : Controller
  {
      public IActionResult Index()
      {
          return Content("Hello, World!");
      }
  }
eof
eof2
} | docker container exec -i iis /bin/bash

Run the app

docker container exec \
  --workdir /tmp/project/HelloWorldApp/ \
  iis \
    dotnet run --urls http://0.0.0.0:5000

Test the app

curl -s http://localhost:8080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment