Docker: CLI & Dockerfile Cheat Sheet
Docker: CLI & Dockerfile Cheat Sheet
Table of Contents
Introduction 1
1. docker CLI 2
1.1 Container Related Commands 2
1.2 Image Related Commands 4
1.3 Network Related Commands 5
1.4 Registry Related Commands 6
1.5 Volume Related Commands 6
1.6 All Related Commands 6
2. Dockerfile 6
About the Authors 8
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 operating system, libraries,
files and folders, environment variables, volume mount-points, and your application binaries.
A “container 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 container runtime. You can
interact with the container runtime through the “docker” command. The three primary
components of a container architecture (client, runtime, & registry) are diagrammed below:
Container Architecture
Client Runtime Registry
Daemon
Local
or
Examples
All examples shown work in Red Hat Enterprise Linux
6. List containers:
# List only active containers
$ docker ps
# List all containers
$ docker ps -a
7. Stop a container:
# Stop a container
$ docker stop [container-name|container-id]
# Stop a container (timeout = 1 second)
$ docker stop -t1
8. Remove a container:
# Remove a stopped container
$ docker rm [container-name|container-id]
# Force stop and remove a container
$ docker rm -f [container-name|container-id]
# Remove all containers
$ docker rm -f $(docker ps-aq)
# Remove all stopped containers
$ docker rm $(docker ps -q -f “status=exited”)
port List port mappings, or look up the public-facing port that is NAT-
ed to the PRIVATE_PORT
ps List containers
wait Block until a container stops, then print its exit code
1.2 Image Related Commands
docker [CMD] [OPTS] [IMAGE]
Examples
All examples shown work in Red Hat Enterprise Linux
5. Tag an image:
# Creates an image called “myimage” with the tag “v1” for the image jboss/wildfly:latest
$ docker tag jboss/wildfly myimage:v1
# Creates a new image with the latest tag
$ docker tag <image-name> <new-image-name>
# Creates a new image specifying the “new tag” from an existing image and tag
$ docker tag <image-name>[:tag][username/] <new-image-name>.[:new-tag]
import Create an empty filesystem image and import the contents of the
tarball into it
ls Lists volumes
rm Remove a volume
2. 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 previously 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
This example creates a custom WildFly container with a custom administrative user. It also
exposes the administrative port 9990 and binds the administrative interface publicly through
the parameter ‘bmanagement’.
#Access the WildFly administrative console and log in with the credentials admin/Admin#70635
open http://<docker-daemon-ip>:9990 in a browser
RUN Execute commands in a new layer on top of the current image and
commit the results
CMD Allowed only once (if many then last one takes effect)
EXPOSE Informs container runtime that the container listens on the speci-
fied network ports at runtime
ADD Copy new files, directories, or remote file URLs from into the
filesystem of the container
COPY Copy new files or directories into the filesystem of the container
USER Sets the username or UID to use when running the 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
STOPSIGNAL Sets the system call signal that will be sent to the container to exit
Example: Running a web server container
$ echo “Server is up” > www/index.html # Make a text file to serve later