Docker build & run .NET web-api image/container

Date: 2023-03-28
# when docker is not started (in WSL on boot)
sudo service docker start
sudo service docker status

# change to source directory with Dockerfile
cd /mnt/c/workspace/{app_source}

# build image from 'Dockerfile' in local directory
docker build -t {app_name}:1.1 .
# view local images
docker images
# run a image in a container 
docker run --name {app_name}-container -d {app_name}:1.1

# inspect container info
docker container inspect {app_name}-container
# show container logs
docker logs {app_name}-container
# remove a container (after use)
docker container rm {app_name}-container

# list (local) containers
docker ps
docker container ls

Dockerfile

## [1] De applicatie builden in een build image/container met dotnet SDK

# Basisimage
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build

# Set working directory
WORKDIR /app

# Kopieer alle source-bestanden van de host naar de container
COPY . ./

# Build de applicatie
RUN dotnet publish -c Release -o out

## [2] De gepublishde applicatie plaatsen in een runtime-image met dotnet runtime

# Bouw de runtime-image
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app
COPY --from=build /app/out ./

# Zet info
LABEL maintainer="info@solidt.eu" 

# Application arguments
# ARG 

# Environment variables
ENV ASPNETCORE_URLS=http://+:80

# Poort openzetten
EXPOSE 80

# Start de applicatie
ENTRYPOINT ["dotnet", "WebApi.dll"]
76140cookie-checkDocker build & run .NET web-api image/container