Docker cheatsheet for .NET Core
This is just a cheat sheet of commands and terminology for Docker and ASP.NET Core; it contains commands that you can find in the original cheat sheet, plus a
Dockerfile for ASP.NET Core and a quick guide on how to created one from Visual Studio. Hopefully, both developers that are in the process of getting into the containerize world with Docker and developers that are already in but need a quick recap will find it useful.
|Docker||Docker is a set of platform as a service products that uses OS-level virtualization to deliver software in packages called containers. Download Docker for Windows here.|
|Image||An image, or more correct, a Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.|
|Container||A container image becomes a container at runtime when they run on Docker Engine|
|Docker Engine||Docker Engine is a container runtime that runs on various Linux (CentOS, Debian, Fedora, Oracle Linux, RHEL, SUSE, and Ubuntu) and Windows Server operating systems…|
|Docker Hub||Docker Hub is a service provided by Docker for finding and sharing container images with your team.|
|Dockerfile|| A |
Read more information about Docker Container Images and Docker Containers here.
Follows, a list of basic commands that you will regularly need. Run them using command line from the root of your application – where the
Dockerfile should exists.
| Retrieve an image from a registry. If you specify only the repository name, Docker will download the image tagged latest from that repository on Docker Hub.|
| Create a new image by running a Dockerfile. User the -t flag to specify the name of the new image and don’t forget the |
| After pulling an image, view the images in your local registry with the |
| View active containers. Use the |
| Run an image – it will become a container. Specify the option |
| Stop an active container by specifying the container ID. Get that with the |
| Restart a stopped container.|
| Remove a stopped container. Add the |
| Remove an image. There is no force flag here, all containers using this image must be stopped.|
Living in the root of the application, a
Dockerfile is just a plain text file; you can either use the following command to create it in Windows, or anyway you like:
copy NUL Dockerfile. The sample below contains everything necessary to build and run an image. Comments above each command attempt to provide a bit of clarity:
# This is a sample Dockerfile for building and running ASP.NET Core applications # This is part of the cheat sheet at https://blog.georgekosmidis.net/2020/06/12/docker-cheat-sheet-for-dotnet-core/ # Pull ASP.NET Core 3.1 runtime and give the name 'base' # More info on 'FROM' instruction here: https://docs.docker.com/engine/reference/builder/#from FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base # Set the ports where the container listens at runtime # More on the 'EXPOSE' instruction here: https://docs.docker.com/engine/reference/builder/#workdir EXPOSE 80 EXPOSE 443 # Pull the ASP.NET Core 3.1 SDK and give the name 'build' # The SDK runs an app on ports 5000 and 5001, that's why we also need runtime FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build # Set the Working Dir to '/src'. This is where the source code is # The WORKDIR instruction sets the working directory for any # RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow # More on the 'WORKDIR' instruction here: https://docs.docker.com/engine/reference/builder/#workdir WORKDIR /src # Copy your projects # More on the 'COPY' instruction here: https://docs.docker.com/engine/reference/builder/#copy COPY ["Name.Of.Your.Project.csproj", "Name.Of.Your.Project/"] COPY ["Dependancy.Of.Your.Project.csproj", "Dependancy.Of.Your.Project/"] # Run in a shell, in this case run 'dotnet restore' # More the 'RUN' instruction here: https://docs.docker.com/engine/reference/builder/#run RUN dotnet restore "Name.Of.Your.Project/"Name.Of.Your.Project.csproj" WORKDIR "/src/"Name.Of.Your.Project" COPY . . RUN dotnet build "Name.Of.Your.Project.csproj" -c Release -o /app/build # Run the publish command using the SDK named as 'build' FROM build AS publish RUN dotnet publish "Name.Of.Your.Project.csproj" -c Release -o /app/publish # Pull 'base' image and name it as 'final' to allow pull access for publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . # Set the entry point, in other words, what to run! # More on the 'ENTRYPOINT' instruction here https://docs.docker.com/engine/reference/builder/#entrypoint ENTRYPOINT ["dotnet", "Name.Of.Your.Project.dll"]
A cheat with Microsoft Visual Studio
If it happens to have a Visual Studio around, just right click on your main project, select ‘Add’ and then ‘Docker Support…’:
Usually, for ASP.NET Core, I choose ‘Linux’ as Operating System; at the end it comes cheaper if you want to host it, for example, in Azure.