Skip to content

Docker Cheat Sheet

Useful commands

container after docker can be omitted in the following examples.

docker version

docker run hello-world
docker container run alpine echo "Hello World"

## runs container from mentioned image
docker run <images-name>:<version>

## run with command
docker run -it <container name> <command to execute>
docker run -it python:3.5 bash

## run as a background service
docker container run -d --name trivia fundamentalsofdocker/trivia:ed2

## run once, exit docker when exiting bash
docker run -it --rm alpine /bin/bash

## run with mount volume
docker run -it -v <absolute_path>:<folder path or new folder name> date_project:1.0

## ls container
docker container ls -l

## ls just the IDs of all containers
docker container ls -q

## start and stop
docker container start trivia
docker container stop $CONTAINER_ID

## remove container
docker container rm <container ID>
docker container rm <container name>
docker rm -f trivia

## inspect container
docker container inspect trivia
docker container inspect -f "{{json .State}}" trivia | jq .

## exec into a running docker
docker container exec -it trivia /bin/sh
## enter and run one command and exit
docker container exec trivia ps
## pass the defined environment variables into container
docker container exec -it -e MY_VAR="Hello World" trivia /bin/sh

## attach terminal's standard input, output, and error (or any combination of the three) to a running container
docker container attach trivia

## retrieve container logs
docker logs <container ID>
docker container logs --tail 5 <container ID>
docker logs -f <container name>

## use a container-specific logging driver
docker container run --name test -it \
       --log-driver none \
       busybox sh -c 'for N in 1 2 3; do echo "Hello $N"; done'
##########
# logging driver
# none - No log output for the specific container is produced.
# json-file - The logging information is stored in files, formatted as JSON.
# journald - forwards logging to the journald daemon.
# syslog - forward the log messages to the syslog daemon.
# gelf - log messages are written to a Graylog Extended Log Format (GELF) endpoint.
# fluentd - Assuming that the fluentd daemon is installed on the host system, this driver writes log messages to it.
##########

## pulls image from Docker registry
docker pull <images-name>:<version>
## shows all running containers
docker ps
## shows all available containers
docker ps -a
## format the output
docker container ps -a \
    --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"
docker container inspect --format "{{.NetworkSettings.IPAddress}}" <container ID>

## filter the output
docker image ls --filter dangling=false --filter "reference=*/*/*:latest"

## limit the resource used by container
docker container run --rm -it \
    --name beta-test \
    --memory 256M \
    ubuntu:18.04 /bin/bash

## clean up container environment
docker image prune -f
docker container prune --force
docker volume prune

## stat
docker stats dockerID

docker-arch.png

Runc and Containerd

runC is a lightweight, portable container runtime. It provides full support for Linux namespaces as well as native support for all security features available on Linux, such as SELinux, AppArmor, seccomp, and cgroups.


Containerd was donated to and accepted by the CNCF in 2017. There exist alternative implementations of the OCI specification. Some of them are rkt by CoreOS, CRI-O by RedHat, and LXD by Linux Containers. However, containerd at this time is by far the most popular container runtime and is the default runtime of Kubernetes 1.8 or later and the Docker platform.

Docker Cheat Sheet

docker-cheat-sheet.png

Reference