Docker Cheat Sheet
Docker Cheat Sheet
Docker Cheat Sheet
Introduction
Containers allow the packaging of your application (and everything that you need to run it) in a
"container image". Inside a container you can include a base operational-system, libraries, files
and folders, environment variables, volumes mount-points, and the application binaries.
A "Docker image" is a template for the execution of a container --- It means that you can have
multiple containers running from the same image, all sharing the same behavior, which
promotes the scaling and distribution of the application. These images can be stored in a remote
registry to ease the distribution.
Once a container is created, the execution is managed by the "Docker Engine" aka "Docker
Daemon". You can interact with the the Docker Engine through the "docker" command. These
three primary components of Docker (client, engine and registry) are diagramed below:
Docker Cheat Sheet
Mithun Technologies Bangalore +91-9980923226, +91-8296242028
Docker Engine
Examples:
6. List containers
$ docker ps # List only active containers
$ docker ps -a # List all containers
7. Stop a container
$ docker stop <container-name> # Stop a container
$ docker stop -t 1 <container-name> # Stop a container (timeout = 1 second)
8. Remove a container
$ docker rm <container-name> # Remove a stopped container
Docker Cheat Sheet
Mithun Technologies Bangalore +91-9980923226, +91-8296242028
port List port-mappings, or lookup the public-facing port that is NAT-ed to the
PRIVATE_PORT
period.
Examples
1. Build an image using a Dockerfile
$ docker build -t [username/]<image-name>[:tag] <dockerfile-path> # Build an image
$ docker build -t myimage:latest . # Build an image called myimage using the
Dockerfile in the same folder where the command was executed.
5. Tag an image
$ docker tag jboss/wildfly myimage:v1 # Creates an image called
"myimage" with the tag "v1" for the image jboss/wildfly:latest
$ docker tag <image-name> <new-image-name> # Creates a new image with
the latest tag
Docker Cheat Sheet
Mithun Technologies Bangalore +91-9980923226, +91-8296242028
import Create an empty filesystem image and import the contents of the tarball
into it
logout Log out from a Docker registry server. If no server is specified then the
default is used.
ls List volumes
rm Remove a volume
Related commands
docker events Get real-time information from the server
Dockerfile
The Dockerfile provides the instructions to build a container image through the `docker build -t
[username/]<image-name>[:tag] <dockerfile-path>` command. It starts from a previous existing
Base image (through the FROM clause) followed by any other needed Dockerfile instructions.
This process is very similar to a compilation of a source code into a binary output, but in this
case the output of the Dockerfile will be a container image.
Example Dockerfile
# Use the existing WildFly image
FROM jboss/wildfly
RUN Executes commands in a new layer on top of the current image and
commits the results
CMD Allowed only once (if many, then only the last one takes effect)
EXPOSE Informs Docker that the container listens on the specified network ports
at runtime.
ADD Copies new files, directories or remote file URLs into the filesystem of
the container
COPY Copies new files or directories into the filesystem of the container
USER Sets the user name or UID to use when running an image
WORKDIR Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY,
and ADD commands
ARG Defines a variable that users can pass at build-time to the builder using
--build-arg
ONBUILD Adds an instruction to be executed later, when the image is used as the
base for another build