Docker Commands Cheat Sheet



Docker kill $ (docker ps -q): Kill all containers that are currently running. Docker rm container: Delete a particular container that is not currently running. Docker rm $ (docker ps -a -q): Delete all containers that are not currently running. Hopefully this guide will serve as your go to Docker cheat sheet. The Ultimate Docker Cheat Sheet Docker - Beginners Intermediate Advanced View on GitHub Join Slack The Ultimate Docker Cheat Sheet. Complete Docker CLI. Container Management CLIs. Inspecting The Container. Interacting with Container. Image Management Commands. Image Transfer Comnands. Builder Main Commands.

Docker

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.

Basic terminology

TermShort explanation
DockerDocker 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.
ImageAn 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.
ContainerA container image becomes a container at runtime when they run on Docker Engine
Docker EngineDocker 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 HubDocker Hub is a service provided by Docker for finding and sharing container images with your team.
DockerfileA Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Read more information about Docker Container Images and Docker Containers here.

Basic commands

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.

TermShort explanation
docker pullRetrieve 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.
e.g. docker pull mcr.microsoft.com/dotnet/core/aspnet:3.1 pulls the 3.1 runtime, where docker pull mcr.microsoft.com/dotnet/core/sdk pulls the latest .NET Core SDK.
docker buildCreate a new image by running a Dockerfile. User the -t flag to specify the name of the new image and don’t forget the . (build context for the source files for the COPY command)
e.g. docker build -t image.name.for.my.app:v1 .
docker image listAfter pulling an image, view the images in your local registry with the docker image list command.
docker psView active containers. Use the -a flag to view all.
e.g. docker ps -a
docker runRun an image – it will become a container. Specify the option -p for port mapping (left hand side local port, right hand side port exposed by docker) and -d to run it as a background service.
Speficy the --name option to set the name of the container.
e.g. docker run -p 8080:80 -d --name container.name image.name.for.my.app
docker stopStop an active container by specifying the container ID. Get that with the docker ps command
e.g. docker stop elegant_ramanujan
docker startRestart a stopped container.
e.g. docker start elegant_ramanujan
docker container rmRemove a stopped container. Add the -f flag to force remove a running container (not a graceful shutdown)
e.g. docker container rm -f elegant_ramanujan
docker image rmRemove an image. There is no force flag here, all containers using this image must be stopped.
e.g. docker image rm mcr.microsoft.com/dotnet/core/samples:aspnetapp

A Dockerfile sample

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:

A cheat with Microsoft Visual Studio

Dockerfile commands

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.

This is a list of commands I find myself writing all the time to managed containers and docker swarm services on my Windows 10 dev machine.These scripts are compatible with Docker 1.12 and Docker for Windows.

Swarm Mode

Enable swarm mode:

Create a custom overlay network:

Create and start a service using a custom overlay network:

Start a service and expose a port on the host system:

Start a service and mount a local system directory:

Start a service and mount a local volume:

List all matching services:

Get CPU, memory, network and IO statistics for all running instances of a swarm service:

Get the virtual IP list of all running instances of a swarm service:

Get the virtual IP of all running instances of a swarm service for a specific overlay network:

Get the volume list of all running instances of a swarm service:

Remove all instances of a swarm service that aren’t currently running:

Update a service:

Docker run command options

Stop a service:

Remove a service:

Docker host cleanup

Remove all stopped containers:

Remove untagged images:

Remove orphaned volumes:

Containers

Start a Bash session in a running container:

Stop and remove all containers by image name or image ID:

Docker Cli Commands Cheat Sheet

Connect to a docker server over TCP:

Monitoring

Monitor MobyLinuxVM with cAdvisor container:

Dockerfile Commands Cheat Sheet

Note that --volume=/:/rootfs:ro mount is not available in Docker for Windows VM, that’s why I removed it from the run command.Access cAdvisor interface on your Windows machine at http://localhost:8090/.

Aggregate all containers logs with logspout:

Connect with PowerShell to see your local aggregated logs in realtime: