Docker Cheatsheet r4v2
Docker Cheatsheet r4v2
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.
Container Architecture
Client Runtime Registry
Daemon
Loca
l
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”)
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]
ls Lists volumes
rm Remove a volume
1.6 Other
commands
Command Description
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.
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
CMD Allowed only once (if many then last one takes effect)
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
STOPSIGNAL Sets the system call signal that will be sent to the container to exit
Example: Running a web server container
To successfuly run the following example in a RHEL environment you must first run the following command:
$ chcon -Rt svirt_sandbox_file_t `pwd `
$ echo “Server is up” > www/index.html already exist) # Make a text file to
working