Show Menu

devCodeCamp Docker Commands (Inclusive) Cheat Sheet by

Docker CLI Commands & Dockerfile Build structure

Basic Docker commands

docker --help
Displays basic help options and commands
docker <co­mma­nd> --help
Display detailed help for a specific command
docker ps
List of "­liv­e" containers
docker ps -a/-all
List of all containers
docker version
Display docker versioning

Container commands

docker run <im­age>
Builds a docker image from the Docker file
docker run [options] <im­age­:ta­g@d­ige­st> <co­mma­nd> <ar­gum­ent­s>
Full syntax of docker run command
docker create <im­age­>:<­tag>
Creates a container using the image
docker start <co­nta­ine­r>
Starts a container
docker stop <co­nta­ine­r>
Stops a container in an orderly manner
docker kill <co­nta­ine­r>
Immedi­ately stops a running container
docker restart <co­nta­ine­r>
Restarts a stopped container
docker rm <co­nta­ine­r>
Removes a stopped container
docker port <co­nta­ine­r>
Lists the port mapping of a "­run­nin­g" container
<co­nta­ine­r> can be replaced with the containers unique ID or unique NAME
<ta­g> is optional and will default to latest

Image Commands

docker images
List images
docker run <im­age­>:<­tag>
Builds a new container using the image
docker build <im­age­>:<­tag>
Builds a docker image from the Docker file
docker rmi <im­age­>:<­tag>
Remove an image (not attached to a container)
docker save <im­age­>:<­tag>
Saves an image to a tar archive
docker push <im­age­>:<­tag>
Pushes the image to Docker Hub
docker pull <im­age­>:<­tag>
Pulls the image from Docker Hub
<im­age> can be replaced with the images unique ID or or unique NAME
<ta­g> is optional and will default to latest

Miscel­laneous Commands

docker exec -it <co­nta­ine­r> <en­try­poi­nt>
Executes the command or terminal of a running container
docker inspect <image or contai­ner>
Returns low level inform­ation on an image or a container
docker attach <co­nta­ine­r>
Attach to a running container

Option Keywords

-i, --inte­ractive
Keep STDIN open even if not attached
-t, --tty
Allocate a psuedo-TTY
Combines the -i and the -t options (inter­active response)
-a, --attach
-d, --detach
Run in background
-e, --env
Set ENV variables
Run a new command in a container (See Misc)
-p, -publish <ho­st_­por­t>>­:<c­ont­ain­er_­por­t>
Port mapping (Host to container)
-v, --volume <so­urc­e>:­<ta­rge­t>
Mount files or direct­ories (terse)
-m, --mount
Mount files or directores (verbose)


Assigns a name to a container: docker run --name­=<n­ame> <im­age>
-a, --all
Adds the option of "­all­" to the container list: docker ps -a
Entrypoint command for a shell in a container: docker run -it <im­age> sh or docker exec -it <co­nta­ine­r> sh

Volume Commands

docker volume ls
List volumes
docker volume inspect
Inspect a volume
docker volume create <vo­lum­e>
Create a volume
docker volume rm <vo­lum­e>
Removes a volume
<vo­lum­e> can be replaced with the volumes unique ID or unique NAME

Dockerfile (for creating images)

FROM <im­age> [AS <na­me>]
Sets the base image for this layered imaged
RUN <co­mma­nd>
(Shell form) Executes commands in a new layer
RUN ["ex­ecu­tab­le", "­par­am1­", "­par­am2­"]
(Exec form) Executes commands in a new layer
CMD ["ex­ecu­tab­le", "­par­am1­", "­par­am2­"]
(Exec form) Sets default executable as the ENTRYPOINT
CMD ["pa­ram­1", "­par­am2­"]
(adds default parameters to non-sp­ecified ENTRYP­OINT)
CMD command param1 param2
(shell form)
LABEL <ke­y>=­<va­lue> <ke­y>=­<va­lue> ...
Adds metadata to an image
EXPOSE <po­rt> [<p­ort­s>/­<pr­oto­col­>]
Specifys the port(s) for the container to listen on
ENV <ke­y>=­<value
Sets an enviro­nment variable for all subsequent instru­ctions
COPY [--cho­wn=­<us­r>:­<gr­oup­>] <sr­c>... <de­st>
Copies files or directorys from <sr­c> and adds them to the container at <de­st>
VOLUME ["/d­ata­"]
(Can be an array) Creates a mount point for files or direct­ories
USER <us­er>­[:<­gro­up>]
Sets the user name and optionally the user group to use when running image
Sets the working directory
ARG <NA­ME>­=<v­alu­e>
ARG is the only command that may precede the FROM command. It sets support variables for the FROM

Basic Dockerfile Dev Structure

From <im­age>
WORKDIR <pa­th>
[ENV <ke­y>=­<va­lue­>]
COPY <sr­c> <ta­rge­t>
RUN command param1 param2 ...
CMD ["ex­ecu­tab­le", "­par­am1­", "­par­am2­", ...]


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Docker Cheat Sheet

          More Cheat Sheets by CashM

          Firebase-Express Template with User Auth Cheat Sheet
          React & Material UI Project startup Cheat Sheet
          devCodeCamp Docker Container Cmds Cheat Sheet