Docker Cheat Sheet

Terminology

Layer A set of read-only files to provision the system.
Image A read-only layer that is the base of your container. Might have a parent image.
Container A runnable instance of the image.
Registry / Hub Central place where images live.
Docker machine A VM to run Docker containers (Linux does this natively)
Docker compose A utility to run multiple containers as a system

Info & Status

Show installed version
docker version
Show installed version
docker version
Display system-wide information
docker info
Show status of running containers
docker stats
Get detailed info about an object
docker inspect NAME
docker inspect nginx
Show mapped ports of a container
docker port CONTAINER
docker port web
Show the logs of a container
docker logs CONTAINER
docker run web
Show all modified files in a container
docker diff CONTAINER
docker diff web
Show processes of containers
docker top CONTAINER
docker top web

Manage Images

Download an image
docker pull IMAGE[:tag]
docker ps nginx
Upload an image to repository
docker push IMAGE[:tag]
docker ps nginx:latest
Delete an image
docker rmi IMAGE
docker rmi nginx
Show list of images
docker images
Delete dangling images
docker image prune
Delete all unused images
docker image prune -a
Build an image from a Dockerfile
docker build DIRECTORY
docker build ./
Tag an image
docker tag IMAGE NEWIMAGE
docker tag nginx nginx:2.05
Build and tag an image from Dockerfile
docker build -t IMAGE DIRECTORY
docker build -t nginx ./
Save an image to tar file
docker save IMAGE > FILE
docker save nginx > nginx.tar
Load an image from tar file
docker load -i TARFILE
docker load -i nginx.tar

Cleanup

Kill all running containers
docker kill $(docker ps -q)
Delete dangling images
docker rm $(docker images -q -f dangling=true)
Remove all stopped containers
docker rm $(docker ps -a -q)

Run container

Start a new container from an image
docker run IMAGE
docker run nginx
Assign name to container
docker run CONTAINER IMAGE
docker run web nginx
Map a port
docker run -p HOSTPORT:CONTAINERPORT IMAGE
docker run -p 8080:80 nginx
Assign a hostname
docker run –hostname HOSTNAME IMAGE
docker run –hostname srv nginx
Assign a dns entry
docker run –add-host HOSTNAME:IP IMAGE
docker run –add-host=docker:10.180.0.1 nginx
Map a local directory into the container
docker run -v HOSTDIR:TARGETDIR IMAGE
docker run -v /data/app:/app nginx
Change the entry point
docker run –it –entry-point EXECUTABLE IMAGE
docker run -it –entry-point bash nginx

Manage Containers

Show list of running containers
docker ps
Show list of all containers
docker ps -a
Delete a container
docker rm CONTAINER
docker rm web
Delete a running container
docker rm -f CONTAINER
docker rm -f web
Stop a running container
docker stop CONTAINER
docker stop web
Start a stopped container
docker start CONTAINER
docker start web
Delete stopped containers
docker container prune
Copy file from container to host
docker cp CONTAINER:SOURCE TARGET
docker cp web:/info.html info.html
Copy a file from host to container
docker cp TARGET CONTAINER:SOURCE
docker cp info.html web:/info.html
Start a shell inside the running container
docker exec -it cp CONTAINER EXECUTABLE
docker exec -it cp web bash
Create an image from container
docker commit CONTAINER
docker commit web
Rename a container
docker rename OLDNAME NEWNAME
docker rename web newweb

Docker Management

All commands are called as options to the base docker command. Run docker –help for more information on a particular command
app Docker Application
assemble Framework-aware builds (Docker Enterprise)
builder Manage builds
cluster Manage Docker clusters (Docker Enterprise)
config Manage Docker configs
context Manage contexts
engine Manage the docker Engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
registry Manage Docker registries
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage swarm
system Manage Docker
template Quickly scaffold services (Docker Enterprise)
trust Manage trust on Docker images
volume Manage volumes