Docker CLI Commands Cheatsheet


During the pandemic a few years ago, I had some spare-time to explore other technologies that peaked my interests. For that, both Podman and Docker served as my choice of tools to build up applications from my localhost and from my NUC-hosted CentOS. Throughout the years – I’ve saved up some of the commands that I have been using the most. I hope this can serve as a lookup list for others and give you guys a much clearer description at what some of the commands and the optional flags really means and does. Below is a ‘quicksheet’ – purposely arranged to give you a quick walkthrough from the very basics commands while diving downwards with more complex syntaxes.


A couple of ‘nice to know’ shortcuts before you begin:

Bash/CLI Shortcut keysDescription
Ctrl + Dquits Attached Shell session
Ctrl + Cquits CLI session
Both basically does the same task, quite important shortcuts to remember when working with Docker CLI ๐Ÿ˜‚๐Ÿ‘Œ!

Basic commands to get you going!

CLI CommandsDescription
docker run *image-name*Fetches image if not installed locally, creates, starts and output whatever is running to user’s console window (find them from the docker public repository: https://hub.docker.com/).
docker run *image-name* *command*Runs a docker container with a custom command that overrides the default one followed by the image.
docker create *image-name*Creates new image.
docker start *image-name*Starts image.
docker start -a *image-name*Prints output to console

-a is to attach and display outputs to the console.
docker ps -aLists all containers (both stopped and running containers) 

-a is to attach and display outputs to the console.
docker psLists currently running containers.
docker system prune -aDeletes all cached images, so that next time you run a docker image – it will re-download the latest image-build from the docker repository.
docker logs *container-ID*Prints out a logged overview/history of outputs from the specified container (outputs all information that has been emitted when the docker container ran).
docker stop *container-ID*Sends a SIGSTOP signal to attempt stopping the container before timing out in 10 seconds, then finally sends a SIGKILL signal to kill the application.
docker kill *container-ID*Sends a SIGKILL signal to kill the application instantly.
docker exec -it *container-ID* shUsed to access the terminal of the specific container-ID. My most used command.

The ‘sh‘ command is included in most images/containers. (more info about it on next command below this one).

The -i flag stands for “input” to write text to the containers CLI.

The -t ensures text being displayed back from the containers CLI and forwarded to your own console.
winpty docker exec -it *container-id* *command* shExecutes an additional command shell in a container (multi-command containers).

-exec command specifies it to run additionally

-it (-i -t) allows us to provide input to the container’s CLI sent from our console (Just as if we were connected through an SSH CLI)

sh‘ starts the shell command inside the container-ID (can also use bash, powershell, zsh etc. if the container application has these shell commands)
Ctrl+D to exit sh mode back to your CLI-environment.
docker run -it *image-name* shFetches image and runs a command shell directly on the container-ID
exitExits the command shell of the specific container-ID (equivalent to the ‘Ctrl + D' shortcut)
Docker Commands to manage containers with the Docker Client* (* now a part of Docker Desktop)

Building custom images

CLI CommandsDescription
docker build .builds a new docker container based on the Dockerfile configuration in the same directory.
docker build -t *dockeraccount*/*repositoryname*:*version* .


Example:

docker build -t gakinchi/redis:latest .
Gives a tag/alias to a docker build ID to simplify ID-references when you run an existing build (stored locally)

-t tells the command to tag the build-ID with a new reference.


A tag should include the following:

dockeraccount
your docker ID/docker account username.

/repositoryname
Repository/image name or project name

:latest
version-definition in the repository

.
Selects all files within the working directory. Can be replace to specify which directory of files/folders to use for the build from your local storage environment
docker run *dockeraccount*/*repositoryname*:*version* .


Example:

docker run gakinchi/redis
Runs the built image with the tagname
docker image lsshows a list of created images with image-ID information
docker commit -c 'CMD["redis-server"]' container-IDGenerates a new local base image for future use (pre-configured). Aka. a snapshot. And a new image ID (of sha256) will be produced for the image.
Basics of building your custom images

Starting with the basic Docker Compose CLI tool

docker build -t *dockeraccount*/*repositoryname*


Example:

docker build -t gakinchi/simpleweb
Builds the docker container with a tagname included instead of using a container-ID.

In the example, the docker tagname will be gakinchi/simpleweb.
docker run *dockeraccount*/*repositoryname*

Example:

docker run gakinchi/simpleweb
Runs the built image with the tagname
docker run -p 8080:8080 gakinchi/simplewebRuns the built image with the tagname.
-p (port flag to specify vports to bind between your local machine and running container-app)
8080:8080 Routes incoming requests on local machine from port 8080 and passes it through port inside the container-app.
docker-compose upUsing Docker Compose CLI tool to run an image.

Equivalent to the command:
docker create *image-name*
docker-compose up --buildUsing Docker Compose CLI tool to (re)build and run an image. Equivalent to the following commands together:
docker create *image-name*
docker run -it *image-name* sh
docker-compose up -dDetached mode: Run containers in the background, print new container names. Incompatible with –abort-on-container-exit.

In other words: Running docker-compose up -d starts the containers in the background and leaves them running. You will notice that the container’s terminal interface exits immediately and you don’t see the logs of the standard output on your CLI anymore.
Basics of the docker compose CLI tool that is included in the Docker Desktop. We’re also starting to incorporating the tag names instead of using ID and making it a habit is good for your soul and the planet. ๐ŸŒ

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *