Docker Command Cheat Sheet

Docker Command Cheat Sheet
Written by Abhishek JalanApril 26, 2020
8 min read
Abhishek Jalan

DevSecOps Engineer

Docker is a fantastic tool designed to make it easier to create, deploy, and run applications by using containers.

Dockerfile Quick Reference

FROM: Sets the base image for subsequent instructions. Dockerfile must start with FROM instruction.

FROM <image>
FROM <image>:<tag>

MAINTAINER: Sets the Author field for the generated images


LABEL: Adds metadata to an image. A label is a key-value pair

LABEL <key>=<value> <key2>=<value2> ...
LABEL <key> <value>

RUN ... Executes any commands in a new layer on top of the current image and commits the results. There are two forms:

Executable form. The square brackets are a part of the syntax

RUN <command> Run the command in the shell /bin/sh -c
RUN ["<executable>", "<param1>", "<param2>"]

CMD Provides defaults for executing container. There could be at most one CMD instruction in a Dockerfile

Executable form

CMD ["<executable>", "<param1>", "<param2>"]

Provide default arguments to ENTRYPOINT

CMD ["<param1>", "<param2>"]

Run the command in the shell /bin/sh -c

CMD <command args ...>

ENTRYPOINT Helps you configure a container that can be run as an executable. The ENTRYPOINT instruction adds an entry command that is not overwritten when arguments are passed to docker run. This is different from the behavior of CMD. This allows arguments to be passed to the entrypoint.

Executable form

ENTRYPOINT ["<executable>", "<param1>", "<param2>"]

Run the command in the shell /bin/sh -c

ENTRYPOINT <command param1 param2 ...>

Informs Docker that the container listens on the specified network ports at runtime. Docker uses this information to interconnect containers using links and to set up port redirection on the host system

EXPOSE <port1> <port2> ...

Sets the environment variable to the value. This value is passed to all future RUN, ENTRYPOINT, and CMD instructions

ENV <key> <value>

Copies new files, directories, or remote file URLs to the filesystem of the container at path <dest>. All new files and directories are created with mode 0755 and with the uid and gid of 0.

COPY <src> <dest>
COPY ["<src>", ... "<dest>"] 

Like COPY, but additionally allows <src> to be an URL, and if <src> is an archive in a recognized format, it will be unpacked. The best practice is to prefer COPY.

ADD <src> <dest>
ADD ["<src>", ... "<dest>"]

Creates a mount point with the specified name and marks it as holding externally-mounted volumes from the native host or from other containers

VOLUME ["/some/path"]

Sets the username or UID used for running subsequent commands. <user> can be either username or UID; <group> can be either group name or GID

USER <user>
USER <user>:<group>

Sets the working directory for the RUN, CMD, ENTRYPOINT, COPY and ADD Dockerfile commands that follow. Relative paths are defined relative to the path of the previous WORKDIR


WORKDIR /path/to/workdir

Defines a variable that users can pass at build-time to the builder with the docker build command using the --build-arg <varname>=<value> flag

ARG <name>
ARG <name>=<default value>

Adds a trigger instruction to an image. The trigger is executed at a later time when the image is used as the base for another build. Docker executes the trigger in the context of the downstream build as if the trigger existed immediately after the FROM instruction in the downstream Dockerfile.

ONBUILD <instruction>

Here's a cheat-sheet to help you remember common docker commands.

Build an image from the Dockerfile in the current directory and tag the image.

docker build -t myimage:1.0 .

List all images that are locally stored with the Docker Engine

docker image ls

Delete an image from the local image store.

docker image rm alpine:3.4

Pull an image from a registry

docker pull myimage:1.0

Retag a local image with a new image name and tag

docker tag myimage:1.0 myrepo/myimage:2.0

Push an image to a registry

docker push myrepo/myimage:2.0 

Run a container from the Alpine version 3.9 image, name the running container “web” and expose port 5000 externally, mapped to port 80 inside the container.

docker container run --name web -p 5000:80 alpine:3.9

Stop a running container through SIGTERM

docker container stop web

Stop a running container through SIGKILL

docker container kill web

List the networks

docker network ls 

List the running containers (add --all to include stopped containers)

docker container ls

Delete all running and stopped containers

docker container rm -f $(docker ps -aq)

Print the last 100 lines of a container’s logs docker container

logs --tail 100 web

Soon I will come up with some more useful commands. Please leave your feedback or comment me if any doubt.

Was this blog helpful?
You must be Logged in to comment
Code Block

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance