infuerno.github.io

Freeman: Essential Docker for ASP.NET Core MVC

Quick Reference

Chapter 1 Understanding Docker

Consistency Problem

With Docker

Responsiveness Problem

With Docker

Containers v VMs

Limitations

Other containerisation options

Chapter 2 Essential Docker Quick Reference

Images

The Docker image used to deploy ASP.NET Core applications does not contain the .NET Core compiler so the application must be compiled with all dependencies. Within the project folder of the application run to publish the output artefacts to a folder called dist.

dotnet publish --framework netcoreapp1.1 --configuration Release --output dist

Use docker build to build the image from a Docker file e.g. docker build . -t apress/exampleapp -f Dockerfile where . indicates the current directory as the working directory; -t is the name of the image; -f specifies the Docker file

Command Description
docker build This command processes a Docker file and creates an image.
docker images This command lists the images that are available on the local system. The -q argument returns a list of unique IDs that can be used with the docker rmi command to remove all images.
docker pull This command downloads an image from a repository (often don’t need to run explicitly)
docker push This command publishes an image to a repository. You may have to authenticate with the repository using the docker login command.
docker tag This command is used to associate an (alternative) name with an image.
docker rmi This command removes images from the local system (specifying the image ID). The -f argument can be used to remove images for which containers exist. To remove all images use docker rmi -f $(docker images -q) to pump all images IDs to the rmi command.
docker inspect Displays details about the image

Useful prebuilt images include images containing: ASP.NET Core runtime; .NET Core runtime; .NET Core SDK (for development); MySQL; HAProxy, useful as a load balancer / configured to respond to containers starting and stopping

Docker files contain a series of commands including:

Containers

Containers are created from an image and used to execute an application in isolation.

Other useful commands include:

Volumes

Volumes allow data files to be stored outside of a container, which means they are not deleted when the container is deleted or updated.

Software-Defined Networks

Software-defined networks are used to connect containers together, using networks that are created and managed using Docker.

Compose

Docker Compose is used to describe complex applications that require multiple containers, volumes, and software-defined networks, using the YAML format.

Other useful commands include:

Swarm

A Docker swarm is a cluster of servers that run containers. Worker nodes run the containers and manager nodes distribute the containers between nodes.

Chapter 4 Docker Images and Containers

Creating Images from Modified Containers

Either run an existing container, or run a dynamic container from an image using:

docker run -it microsoft/dotnet:2.1-aspnetcore-runtime /bin/bash to create a container, start it and enter an interactive shell

Make any changes and exit. After exiting run docker ps -a to see the container. The name will be randomly generated for the container in this last case. See https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go

Create a new image from the container using docker commit:

docker commit hungry_torvalds infuerno/dotnet:2.1-aspnetcore-runtime

Publishing Images

Chapter 5 Docker Volumes and Networks