Docker Cheat Sheet – CLI Commands

In this article we will go through the docker cheat sheet which will serve as a guide to docker CLI commands used on a daily basis

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.

For more information on docker installation & configuration you can always refer to their official website Get Docker

As a pre-requite you may need support for the installation of the underlying operating system for the docker host you can refer our blogs How to Install Rocky Linux 8

We also have a separate blog on How to install the docker on Rocky Linux 8

Container Related Commands

docker [CMD] [OPTS] [CONTAINER]

Example : All the examples shows works on Rocky Linux (Derivatives of RHEL)

  1. Run a container in interactive mode:
$ docker run -it rhel7/rhel bash 

You can check the release inside the container

[root@.../]# cat /etc/redhat-release

2. Run a container in detached mode:

$ docker run --name mywildfly -d -p 8080:8080 jboss/wildfly

3. Run a detached container in a previously created container network:

$ docker network create mynetwork

$ docker run --name mywildfly-net -d --net mynetwork \
-p 8080:8080 jboss/wildfly

Run a detached container mounting a local folder inside the container:

$ docker run --name mywildfly-volume -d \

-v myfolder/:/opt/jboss/wildfly/standalone/deployments/ \
-p 8080:8080 jboss/wildflyjboss/wildfly

5. Follow the logs of a specific container:

$ docker logs -f mywildfly

$ docker logs -f [container-name|container-id]

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”)

9. Execute a new process in an existing container:

         # Execute and access bash inside a WildFly container

$ docker exec -it mywildfly bash

Image Related Commands

docker [CMD] [OPTS] [IMAGE]

Examples : All the examples shows works on Rocky Linux (Derivatives of RHEL)

  1. Build an image using a Dockerfile:
         #Build an image

$ docker build -t [username/]<image-name>[:tag] <dockerfile-path>
#Build an image called myimage using the Dockerfile in the same folder where the command was executed
$ docker build -t myimage:latest .

2. Check the history of an image:

 # Check the history of the jboss/wildfly image

$ docker history jboss/wildfly
# Check the history of an image
$ docker history [username/]<image-name>[:tag]

3. List the images:

$ docker images

4: Remove an image from the local registry:

$ docker rmi [username/]<image-name>[:tag]

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]

6. Exporting and importing an image to an external file:

# Export the image to an external file

$ docker save -o <filename>.tar
# Import an image from an external file
$ docker load -i <filename>.tar

7. Push an image to a registry:

$ docker push [registry/][username/]<image-name>[:tag]

Network related commands

docker network [CMD] [OPTS]

Volume related commands

docker volume [CMD] [OPTS]

Leave a comment