Skip to content Skip to main navigation Skip to footer

Docker Cheatsheet

Run Container

Assign name and create an interactive shell [–name, -it]

docker run -it –name <CONTAINER NAME> <IMAGE>

$ docker run -it --name "test-13" debian:buster-slim
root@fb966dd9dba0:/# exit 13

$ docker ps -a
CONTAINER ID   IMAGE                COMMAND   CREATED          STATUS                     PORTS   NAMES
fb966dd9dba0   debian:buster-slim   "bash"   50 seconds ago   Exited (13) 44 seconds ago          test-13

Run in background [-d]

docker -d <IMAGE>

$ docker run -it -d --name "test-background" debian:buster-slim
b66cbcb7c7e05940d88a48ccf6466fdc264ef7b2105b68b2659ee541b30911c8
$ docker ps
CONTAINER ID   IMAGE                COMMAND   CREATED          STATUS      PORTS   NAMES
b66cbcb7c7e0   debian:buster-slim   "bash"   5 seconds ago   Up 4 seconds          test-background

Execute a command in a running container

docker exec -it <CONTAINER> <COMMAND>

# Run container in background
$ docker run -it -d --name "debian-buster" --rm debian:buster-slim

# Execute command in the container
$ docker exec -it "debian-buster" bash

Publish Port [-p]

docker run -p <HOST PORT>:<CONTAINER PORT> <IMAGE>

# Run a container from the Alpine version 1.19.3 of nginx image and
#  expose port 8080 externally, mapped to port 80 inside the container.
$ docker run -it --name "webserver" --rm -p 8080:80 nginx:1.19.3-alpine

Visit http://localhost:8080 and see nginx welcome page!

Mount Volume [-v]

docker run -v <HOST DIRECTORY>:<CONTAINER DIRECTORY> <IMAGE>

# Map a local home directory to /volumes/home in container and then list files
$ docker run -it --rm -v /$HOME:/volumes/home busybox ls -la "/volumes/home/"

Set working directory [-w]

docker -w <WORKING DIRECTORY> <IMAGE>

# Set working directory to /usr/sbin
$ docker run -it --rm -w "/usr/sbin" busybox pwd

Manage Containers

List containers

docker ps [-a]

# To see all, but not only running containers, use the -a flag 
$ docker ps -a
CONTAINER ID   IMAGE                COMMAND   CREATED          STATUS      PORTS          NAMES
51e9e91b6760   debian:buster-slim   "bash"   2 minutes ago   Exited (127) 5 seconds ago   test-commit

Remove container

docker rm [-f] <CONTAINER>

# To remove a running container, use the -f flag
$ docker rm [-f] webserver

Kill running container

docker kill <CONTAINER>

# Kill a container
$ docker kill webserver

Start & Stop containers

docker <start|stop|restart> <CONTAINER>

# Start container
$ docker start webserver

# Stop container
$ docker stop webserver

# Restart container
$ docker restart webserver

Copy files/folders between a container and the local filesystem

docker cp <CONTAINER>:<SRC_PATH> <DEST_PATH>

docker cp <SRC_PATH> <CONTAINER>:<DEST_PATH>

# Copy index.html from container to host
$ docker cp webserver:/usr/share/nginx/html/index.html index.html
# Copy index.html from host to container
$ docker cp index.html webserver:/usr/share/nginx/html/index.html

Create a new image from a container’s changes

docker commit <CONTAINER> [IMAGE[:TAG]]

$ docker run -it --name "test-commit" debian:buster-slim
root@51e9e91b6760:/# echo "hello banana" > /test-commit.txt
root@51e9e91b6760:/# cat /test-commit.txt
hello banana
root@51e9e91b6760:/# exit

$ docker ps -a
CONTAINER ID   IMAGE                COMMAND   CREATED          STATUS      PORTS          NAMES
51e9e91b6760   debian:buster-slim   "bash"   2 minutes ago   Exited (127) 5 seconds ago   test-commit

$ docker commit test-commit "local/test-commit:version_1"

$ docker images
REPOSITORY          TAG         IMAGE ID       CREATED          SIZE
local/test-commit   version_1   d24808bfa302   35 seconds ago   69.2MB

Export a container’s filesystem as a tar archive

docker export -o <FILEE.tar> <CONTAINER>

$ docker export --output="busybox_latest.tar" busybox

$ ls -la busybox_latest.tar
-rw------- 1 user user 1455104 Oct 24 15:00 busybox_latest.tar

Manage Images

List images

docker images

$ docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
<none>                    <none>              77af4d6b9913        19 hours ago        3.341 GB
yocto                     latest              b6fa739cedf5        19 hours ago        3.533 GB
docker                    latest              30557a29d5ab        20 hours ago        1.089 GB
<none>                    <none>              5ed6274db6ce        24 hours ago        1.089 GB
buildroot                 buildroot/test      9fbfc6076060        4 days ago          813.3 MB
buildroot                 2020.02.7           af6997c22195        4 days ago          751.4 MB
openwrt                   latest              cd9503ee235a        5 days ago          943.6 MB

Delete an image

docker rmi <IMAGE>

# Delete the image 
$ docker rmi "local/test-commit"

Save image to a tar archive

docker save <IMAGE> -o <FILE.tar>

# Save the image 
$ docker save busybox -o busybox.tar

Load image from a tar archive

docker load -i <FILE.tar>

# Load the image 
$ docker load -i busybox.tar

Remove unused images

docker image prune -a

$ docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: nginx:1.19.3-alpine
untagged: nginx@sha256:a2359cf962dcb9adf1c545440b856aac3a6b16a2ef4bf1acb729ca362cc6ee17
deleted: sha256:4efb29ff172a12f4a5ed5bc47eda3596f8b812173cf609cbb489253dad6e737f
deleted: sha256:24fefe025f4ff24565b3e9dbd68156e21eee89cdf5378797209d2204a55d42f0
deleted: sha256:8fc317fc6ff00ac15118973cfc423dab3df80ef420f9c5a8b5ac4dc7edc70ca1
deleted: sha256:5bf0520ccfa7f1cefe009517fc3dac8c612c01e6d1c6ced4a99b7ab70d3d3948
deleted: sha256:0e5e4eb7c75c7d3c43b413af302e1f48860e4de14c5b85630ada40df537b5c33
untagged: local/test-commit:new
deleted: sha256:b20116dc935083568690b54cf0ebacd58d61759a67f1757b47852feaea40978a
deleted: sha256:0723131dcd273b4c706816d43e42d62e40d1620c83b3f0facf2a5373988caecc

Total reclaimed space: 1.327GB

Create an image tag

docker tag <IMAGE> <NEW IMAGE[:TAG]>

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
debian              buster-slim         1153053544c3        3 months ago        69.2MB
hello-world         latest              bf756fb1ae65        9 months ago        13.3kB

$ docker tag debian:buster-slim "local/debian/buster-test:v1.43"

$ docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
debian                     buster-slim         1153053544c3        3 months ago        69.2MB
local/debian/buster-test   v1.43               1153053544c3        3 months ago        69.2MB
hello-world                latest              bf756fb1ae65        9 months ago        13.3kB

Pull an image from a registry

docker pull <NAME[:TAG]>

$ docker pull debian:buster-slim
buster-slim: Pulling from library/debian
bb79b6b2107f: Pull complete 
Digest: sha256:1be41347adaee8303bf12114b9edf4af0b35a5e1d9756b3ddad59856eaa31ea7
Status: Downloaded newer image for debian:buster-slim
docker.io/library/debian:buster-slim

Push an image to a registry

docker push <NAME[:TAG]>

# To push a local image to a remote registry, 
#   Tag the image with the host name or IP address, and the port of the registry
$ docker tag "local/debian/buster-test:v1.43" "registry-host:5000/debian/buster-test:v1.43"

# Push
$ docker push registry-host:5000/debian/buster-test:v1.43"

Miscellaneous

Attach to a running container

docker attach <CONTAINER>

To detach from container use CTRL-p CTRL-q key sequence

# Run an image and start printing to container's stdout
$ docker run --name test-logs -d busybox sh -c "while true; do $(echo date); sleep 1; done"

# Attach to the container
$ docker attach test-logs
Sat Oct 24 11:17:40 UTC 2020
Sat Oct 24 11:17:41 UTC 2020
Sat Oct 24 11:17:42 UTC 2020

Logs

docker logs [-f] CONTAINER

# Run an image and start printing to container's stdout
$ docker run --name test-logs -d busybox sh -c "while true; do $(echo date); sleep 1; done"

# Check host's date
$ date
Sat 24 Oct 2020 11:17:44 UTC

# Follow container's logs for the 2 seconds
$ docker logs -f --until=2s test-logs
Sat Oct 24 11:17:40 UTC 2020
Sat Oct 24 11:17:41 UTC 2020
Sat Oct 24 11:17:42 UTC 2020

Display the running processes of a container

docker top <CONTAINER>

# 
$ docker top test-logs 
UID     PID       PPID      C    STIME    TTY    TIME        CMD
root    111421    111400    0    14:24    ?      00:00:00    sh -c while true; do date; sleep 1; done
root    116030    111421    0    14:44    ?      00:00:00    sleep 1