DOCKER COMMANDS

docker init - Initialize a project with Dockerfile, ignorefile, etc.
docker <command> --help - Help
docker pull nginx:stable-alpine3.21-perl - Pull an Image
docker images - List Images
docker ps - List Containers
docker rmi <name> - Remove an Image

docker run command creates a new container from an image and starts it immediately, It is equivalent to first executing docker create to build the container and then docker start to launch it.

Arguments:

-d - In background
--name - Name the container
--rm - Remove container when done
-p - Map My PCs port 8080 to internal port 80
-v - Add a volume to save date between container re-creation
docker run --name new-container-name -d --rm -p 8080:80 image-name
docker run -d --rm -p 8080:80 -v <volumeName>:<location> <name>|<id>
docker run -d --rm -p 8080:80 -v nginx-data:/usr/share/nginx/html --name nginx-test nginx
docker run --name blazor-site-from-docker --rm -d -p 8080:8080 blazorapp2:1.3

Running SQL Server

docker run --name sql-server-2022 -e "ACCEPT_EULA=Y" -e \"MSSQL_SA_PASSWORD=asdf_ASDF!1234" -v SQLServer2022Volume:/var/opt/mssql -p 1433:1433 -d mcr.microsoft.com/mssql/server:2022-latest

Running Ubuntu

docker run -it -rm ubuntu /bin/bash\
docker run -it -rm sha256:a61c057b4f69200ecf031519a20db79b8683837ba1dc2a59458d333eb75b174d /bin/bash\
docker stop <name>|<id>\
docker start <name>|<id>

Logs

docker logs <container_name>\
docker inspect <container_name>

Enter a Container with sh and born-again sh

docker exec -it blissful_montalcini /bin/sh  
docker exec -it blissful_montalcini /bin/bash
exit

Publish/Build Images

-o Specify output files
-t tag/name
. current directory, where the dockerfile is

Remove the exe file which isn't needed:

dotnet publish .\hello-docker.csproj -o publishFolder /p:UseAppHost=false
docker build -t <tag-name> .

Build image with .NET SDK

csproj file: Contains project name, target framework, etc. Add criteria like ContainerBaseImage, etc.

dotnet publish --ok linux --arch x64 /t:PublishContainer 

Multi-Stage Build e.g.

Delete the published folder we made with dotnet publish. Then use this Dockerfile.

FROM mcr.microsoft.com/dotnet/sdk:9.0-alpine AS build
WORKDIR /src
COPY . . 
RUN dotnet publish "BlazorApp1.csproj" -o /publishedFolder /p:UseAppHost=false
FROM mcr.microsoft.com/dotnet/sdk:9.0-alpine
WORKDIR /app
COPY --from=build /publishedFolder .	 
ENTRYPOINT ["dotnet", "BlazorApp1.dll"]

Tag an image e.g.

This will create a new image with the same ImageId as 'latest'

docker tag blazorapp2:latest blazorapp2:1.0
docker tag hello-docker:latest hello-docker:1.0
## However, we can build a new image which will also create a new image, but with a new ImageId
docker build -t blazorapp2:1.1 .

Troubleshooting

You need to create a RELEASE build in VS if you want the container to work after shutting down VS.

SQL Server Config

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=asdf_ASDF!1234" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2022-latest

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=asdf_ASDF!1234" -v SQLServer2022Volume:/var/opt/mssql -p 1433:1433 -d mcr.microsoft.com/mssql/server:2022-latest