Docker Cheat Sheet
Commands
Container vs image ids
Note in the following examples <container> is either a container id, or a container name (if such is given to a container with the –name option on start). Both can be obtained with docker ps -a. <image> is either an image id, or an image name. Those can be obtained with the docker image command. Do not confuse with container id/name!
Listing Containers
docker ps # List running containers
docker ps -a # List all containers
docker ps -s # List running containers including CPU/memory sizeList machine readable:
docker ps -a --format "{{.ID}},{{.Names}},{{.Status}},{{.Image}},{{.Ports}}"Inspecting containers
docker exec -it <container> bash # Log into container bash environment
docker inspect <container> # Instance details
docker top <container> # Instance processes
docker logs <container> # Instance console log
docker port <container> # Shows container's port mapping. The same can be seen with "docker ps" though (row - "PORTS")
docker diff <container> # Shows changes on container's filesystem. Will produce a list of files and folders prefixed by a
# character. "A" is for "added", "C" is for changed.
docker stats <container> # Shows the consumed resources (memory, CPU, network bandwidth)
docker export --output="latest.tar" <container> #Export a container’s filesystem as a tar archiveStarting containers
Start a container with default entrypoint and in background
Start a container with a command like /bin/bash
Start with port forwarding
Create a network and start container in this network
Container and image lifecycle
Building Images
Using BuildKit
BuildKit is Docker next-gen build derived from Moby BuildKit. In Docker v18 and v19 it needs to be explicitely enabled. There are two ways to use it.
1.) via environment
2.) via new “buildx” command (v19+ only)
Note: here “buildx” just serves as a wrapper to provide compatible build commands.
Releasing Images
To a private/remote registry
Networks
Docker Registry v2 API
https://docs.docker.com/registry/spec/api/
DockerHub Rate Limits + Solutions
https://inlets.dev/blog/2020/10/29/preparing-docker-hub-rate-limits.html
Misc
Amazon EC2 Container Service - Docker container support on AWS
Docker Patterns - container inheritance examples
Docker Bench Security - Test Docker containers for security issues
Best Practices for Images
When using ext4: disable journaling
Last updated
Was this helpful?