Contents

TehGM's Docker Cheatsheet

Docker is awesome. However, more often than not I keep googling or copying from thing I previously used.

For this reason, I decided to build up a small personal cheatsheet of Docker commands/snippets. It’s primarily for my own purposes, but hey, why not share it publicly!

Commands

Note: NAME in all commands is the Container’s name.

Note: all commands are for Linux, some even Debian/Ubuntu specific.

Installing Docker on Debian/Ubuntu

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# uninstall old version
sudo apt-get remove docker docker-engine docker.io containerd runc

# pre-install steps
sudo apt-get update

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

## Standard:
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

## armhf (Raspberry Pi):
echo \
  "deb [arch=armhf signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

## arm64:
echo \
  "deb [arch=arm64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# install
sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io

List Containers

1
docker container ls

Build Image with Dockerfile

1
docker build -t repo/name:tag .

Stop and remove existing Container (CI/CD safe)

1
2
docker stop NAME || true
docker rm NAME || true

Run Container in background

1
docker run -d --name=NAME repo/name:tag

Run Container with auto-restart (like service)

1
docker run -d --name=NAME --restart=always repo/name:tag

Prune unused Docker Containers

1
docker image prune -f -a

Expose ports

1
docker run -d --name=NAME -p [host-port]:[container-port]

Mount directory

1
2
3
docker run -d --name=NAME -v [host-location]:[container-location]
# example:
docker run -d --name=NAME -v "/var/log/MyApp":"/logs"

View running Container’s files

1
2
docker exec -i -t NAME /bin/bash
cat [file]

View Container’s logs

1
docker logs NAME

Dockerfile snippets

ASP.NET Core 5.0

1
2
3
4
FROM mcr.microsoft.com/dotnet/aspnet:5.0
EXPOSE 80
COPY . .
ENTRYPOINT ["dotnet", "MyApp.dll"]

ASP.NET Core 5.0 (for armhf)

1
2
3
4
FROM mcr.microsoft.com/dotnet/aspnet:5.0.10-bullseye-slim-arm32v7
EXPOSE 80
COPY . .
ENTRYPOINT ["dotnet", "MyApp.dll"]

Keep Container without background Entrypoint running

1
2
...
ENTRYPOINT ["tail", "-f", "/dev/null"]

Other?

This list is not exhaustive by any means, and I’ll probably keep updating it.

For now these are my most used commands and snippets.