Show Menu
Cheatography

Docker Cheat Sheet (DRAFT) by

A detailed cheatsheet on Docker

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Dockerfile

FROM <ex­ist­ing­-im­age>
Specifies an existing image to build on top of
WORKDIR <pa­th>
Set the working directory inside the container for all subsequent commands in the Dockerfile
COPY <so­urc­e> <de­sti­nat­ion>
Takes files or folders from your build context on your machine and places them inside the image’s filesystem
RUN <co­mma­nd>
Prepare the enviro­nment once, so every container starts ready to go
CMD ["ex­ec", "­arg­1", "­arg­2"]
Define what the container actually does when launched

Container and Image Management

docker ps
Lists currently running containers.
-n
Show last
n
created containers
-a
List all containers
-n
Last
n
containers
-q
Only container IDs
docker start <co­nta­ine­r_n­ame­_or­_id>
Restart a stopped container
docker stop <co­nta­ine­r_n­ame­_or­_id>
Stop a container
docker rm <co­nta­ine­r_n­ame­_or­_id>
Remove a container
docker images
List all images
docker rmi <im­age­_id>
Remove an image
docker pull <im­age>
Download an image from a registry

docker build

-t
/
--tag
Give your image a name
-f
/
--file
Path to a specific Docker­file. Need when your Dockerfile is not named "­Doc­ker­fil­e" or not in the current directory.
--buil­d-arg
Passes build-time variables (used with
ARG
in Docker­file)
--no-cache
Forces all layers to rebuild. Good when debugging incons­istent builds
--pull
Always pulls the latest version of the base image
--platform
Specifies archit­ecture (helpful for multi-arch builds, Apple Silicon, etc.)
-q
/
--quiet
Outputs only the final image ID (surpr­esses verbose logs)
Turns a directory (with a Docker­file) into an image
 

docker run

-d
/
--detach
Runs the container in the background
-p
/
--publish
Maps a container port to a host port
--name
Gives the container a specific name
-v
/
--volume
Real-time sharing of given directory between your computer and the container
--rm
Automa­tically removes the container when it stops
-it
Used for intera­ctive shells
-e
Pass given variables into into the container
Starts a container from an image