Skip to content

Instantly share code, notes, and snippets.

@seesharprun
Last active April 7, 2022 20:06
Show Gist options
  • Save seesharprun/8526cedadf5b46a5fd3bf56119c6f397 to your computer and use it in GitHub Desktop.
Save seesharprun/8526cedadf5b46a5fd3bf56119c6f397 to your computer and use it in GitHub Desktop.
[Show Notes] Improve your .NET developer workflow with Docker and Visual Studio Code

Improve your .NET developer workflow with Docker and Visual Studio Code

Docker & .NET

.NET Dockerfile

dotnet --version
dotnet --list-sdks
dotnet --info
FROM mcr.microsoft.com/dotnet/sdk:7.0
docker build --tag dotnetenv .
docker run --interactive --tty dotnetenv
dotnet --version
dotnet --list-sdks
exit
FROM mcr.microsoft.com/dotnet/sdk:7.0

COPY --from=mcr.microsoft.com/dotnet/sdk:6.0 /usr/share/dotnet/sdk /usr/share/dotnet/sdk

COPY --from=mcr.microsoft.com/dotnet/sdk:5.0 /usr/share/dotnet/sdk /usr/share/dotnet/sdk

COPY --from=mcr.microsoft.com/dotnet/sdk:3.1 /usr/share/dotnet/sdk /usr/share/dotnet/sdk
COPY --from=mcr.microsoft.com/dotnet/runtime:6.0 /usr/share/dotnet/shared /usr/share/dotnet/shared

COPY --from=mcr.microsoft.com/dotnet/runtime:5.0 /usr/share/dotnet/shared /usr/share/dotnet/shared

COPY --from=mcr.microsoft.com/dotnet/runtime:3.1 /usr/share/dotnet/shared /usr/share/dotnet/shared
COPY --from=mcr.microsoft.com/dotnet/aspnet:6.0 /usr/share/dotnet/shared /usr/share/dotnet/shared

COPY --from=mcr.microsoft.com/dotnet/aspnet:5.0 /usr/share/dotnet/shared /usr/share/dotnet/shared

COPY --from=mcr.microsoft.com/dotnet/aspnet:3.1 /usr/share/dotnet/shared /usr/share/dotnet/shared

Razor Pages Dockerfile

dotnet new webapp
dotnet new gitignore

dotnet watch

dotnet publish --configuration Release --output out

dotnet .\out\<name-of-project>.dll
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish --configuration release --output out

FROM mcr.microsoft.com/dotnet/aspnet:6.0

WORKDIR /host

COPY --from=build /app/out ./

ENTRYPOINT [ "dotnet", "<name-of-project>.dll" ]
[Bb]in/
[Oo]bj/
[Oo]ut/
docker build --tag aspnetrazor .
docker run --detach --publish 80 aspnetrazor

Blazor Dockerfile

dotnet new blazorwasm

dotnet run
events { }
http {
    include mime.types;
    types {
        application/wasm wasm;
    }
    server {
        listen 80;
        location / {
            root      /usr/share/nginx/html;
            try_files $uri $uri/ /index.html =404;
        }
    }
}
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build

WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish --configuration Release --output out

FROM nginx:alpine

COPY --from=build /app/out/wwwroot /usr/share/nginx/html

COPY nginx.conf /etc/nginx/nginx.conf
docker build --tag aspnetblazor .
docker run --detach --publish 5555:80 aspnetblazor

VSCode Devcontainer

Running in a Docker Hub container image

https://hub.docker.com

https://hub.docker.com/_/node

{
    "name": "user-group-container",
    "image": "node"
}
node --version
"image": "node:14"

https://hub.docker.com/_/microsoft-dotnet-sdk

"image": "mcr.microsoft.com/dotnet/sdk:5.0"
dotnet --version
"image": "mcr.microsoft.com/dotnet/sdk:6.0"

Using pre-built VSCode-specific containers

https://hub.docker.com/_/microsoft-vscode-devcontainers

"image": "mcr.microsoft.com/vscode/devcontainers/python:3.9"
python --version
"image": "mcr.microsoft.com/vscode/devcontainers/dotnet:3.1"
dotnet --version
dotnet --list-sdks
pwsh

Adding extensions

"extensions": [        
    "ms-dotnettools.csharp"
],
"settings": {
    "csharp.suppressDotnetInstallWarning": true,
    "csharp.suppressDotnetRestoreNotification": true,
    "csharp.supressBuildAssetsNotification": true
}
dotnet new console
dotnet new gitignore

dotnet add package Colorful.Console
using static System.Drawing.Color;
using Console = Colorful.Console;

Console.WriteAscii("Hello, User Group!", Green);
dotnet run

Enabling features

"features": {
    "azure-cli": "latest",
    "powershell": "7"
}
az --version
pwsh

$PSVersionTable

Setting environment variables

"settings": {
    "terminal.integrated.profiles.linux": {
        "pwsh": {
            "path": "pwsh",
            "args": [
                "/nologo"
            ]
        }
    },
    "terminal.integrated.defaultProfile.linux": "pwsh"
},
"containerEnv": {
    "POWERSHELL_UPDATECHECK": "Off"
}

Build with Dockerfile

"build": { 
    "dockerfile": "Dockerfile" 
}
FROM mcr.microsoft.com/dotnet/sdk:3.1
dotnet new --list

dotnet tool list --global

dotnet/dotnet-docker#520

RUN dotnet new --install microsoft.azure.functions.worker.projecttemplates

ENV PATH="${PATH}:/root/.dotnet/tools"

RUN dotnet tool install --global cosmicworks
cosmicworks --help

dotnet new func

Build with Docker Compose

"dockerComposeFile": "docker-compose.yml",
"service": "dev",
"workspaceFolder": "/workspace",
version: '3'
services:
  dev:
    image: mcr.microsoft.com/dotnet/sdk:3.1
    command: sleep infinity
    volumes:
      - ..:/workspace:cached
    depends_on:
      - cosmos
  cosmos:
    image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
    ports:
      - 8081:8081
dotnet --version
ping cosmos

Combine it together

FROM mcr.microsoft.com/dotnet/sdk:6.0

COPY --from=mcr.microsoft.com/dotnet/runtime:3.1 /usr/share/dotnet/shared /usr/share/dotnet/shared

ENV PATH="${PATH}:/root/.dotnet/tools"

RUN dotnet tool install --global microsoft.dotnet-msidentity

RUN dotnet new --install microsoft.identity.web.projecttemplates
version: '3'
services:
  dev:
    build:
      context: .
      dockerfile: Dockerfile
    command: sleep infinity
    volumes:
      - ..:/workspace:cached
    depends_on:
      - storage
  storage:
    image: mcr.microsoft.com/azure-storage/azurite
    ports:
      - 10000:10000
{
    "name": "user-group-container",
    "dockerComposeFile": "docker-compose.yml",
    "service": "dev",
    "workspaceFolder": "/workspace",
    "extensions": [        
        "ms-dotnettools.csharp"
    ],
    "features": {
        "azure-cli": "latest"
    },
    "settings": {
        "csharp.suppressDotnetInstallWarning": true,
        "csharp.suppressDotnetRestoreNotification": true,
        "csharp.supressBuildAssetsNotification": true
    }
}
dotnet --version
dotnet --list-sdks
dotnet --info

dotnet-msidentity --help
dotnet new --list

dotnet new console
dotnet new gitignore

dotnet add package azure.storage.blobs
git init

https://docs.microsoft.com/azure/storage/common/storage-use-azurite

using Azure.Storage.Blobs;

string connectionString = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://storage:10000/devstoreaccount1;";
BlobServiceClient client = new (connectionString);

var container = client.GetBlobContainerClient("files");
await container.CreateIfNotExistsAsync();
dotnet run
az storage container list --connection-string 'DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://storage:10000/devstoreaccount1;'

Use with GitHub Codespaces

dotnet build
await client
    .GetBlobContainerClient("images")
    .CreateIfNotExistsAsync();
    
await client
    .GetBlobContainerClient("videos")
    .CreateIfNotExistsAsync();
dotnet run
az storage container list --connection-string 'DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://storage:10000/devstoreaccount1;' --query '[].name'
az login --use-device-code

az group create --name <resource-group-name> --location <location>

az storage account create --name <storage-account-name> --resource-group <resource-group-name> --location <location>

az storage account show-connection-string --name <storage-account-name> --resource-group <resource-group-name>

az storage container list --connection-string '<storage-account-connection-string>'
string connectionString = "<storage-account-connection-string>";
dotnet run
az storage container list --connection-string '<storage-account-connection-string>' --query '[].name'

Advanced examples

github.com/microsoftlearning/az-dev-workspace/.devcontainer

github.com/seesharprun/multidb-devcontainer/.devcontainer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment