A Docker Cheat Sheet
There are so many articles and tutorials (both in text and videos) on Docker beyond its official documentation. So, I think it’s a waste of time to write detailed articles on that; it would be reinventing the wheels. Hence, I just write this to serve as a cheat sheet on Docker, at least for myself, and it might also be useful for others. The reason I write this is to quickly remind me of key ideas and important commands of Docker in case I forget after not using it for a quite long time.
Why Using Docker
With Docker, you can create one or more container that is “independent” from the host system. A container is “independent” from the host system, and a container is independent from another container. You can consider a container as a separate/independent development environment. For example, if you already have Ubuntu 24.04 and you need to run something that only runs on Ubuntu 22.04, you don’t need to partition your computer just to install Ubuntu 22.04. You can install Ubuntu 22.04 inside your existing Ubuntu 24.04 using Docker. In this case, you need to pull a Docker image of Ubuntu 22.04 and subsequently create a Docker container from that Docker image. Afterwards, you can build anything inside the created container.
Another benefit of using Docker is portability. What I mean by this is, the ease of copying/sharing a whole system for other people to reuse it in their own system without any dependency issue. This is because you copy/share the whole system as it is. This is done by saving your Docker container to a Docker image and then sharing the created Docker image with other people.
At least, three things are usually used in Docker: image, container, and volume. I will discuss image and container later in this post. A volume is folder outside your container, but you can connect to it and access it from your container. You can store your data in this volume. In case you delete your container, this volume will not be deleted. This way you don’t lose your data.
Docker Desktop vs Docker Engine
Docker Desktop is a Docker engine in a virtual machine. Unlike Docker engine that completely uses text to interface with us, Docker Desktop provides a GUI to make it more convenient for the users. However, its non-text usage is, so far, only limited to basic management tasks, such as 1) searching and pulling a Docker image and 2) creating, running, stopping, and deleting a Docker container. Almost all other Docker functions still need to be used via command line interface (CLI) that is available within the Docker Desktop GUI.
One way I like to check if Docker engine or Docker Desktop is already installed in my Ubuntu machine is by checking /opt folder. If “containerd” folder exists, it means Docker engine is installed. If “docker-desktop” folder exists, it means Docker Desktop is already installed. Both of them can be independently installed. Another way to check if Docker Desktop is installed is by searching it on the Search.
To list all the active docker:
sudo docker context ls
The running one is indicated by the asterisk sign.
To switch between dockers:
Using docker engine: sudo docker context use default
Using docker desktop: sudo docker context use docker-desktop
Docker Image and Container
Docker images use parent-child mechanism.
If a docker image is built on top of a parent image, then the parent image cannot be deleted.
Hence, be careful to build a docker image in a large series of parent images.
Docker image that is used as a base of a container cannot be deleted if the using container still exists.
Docker image is static. But a container based on that image can grow from time to time.
Some Useful Docker Commands
To pull a docker image:
First, search the docker image to reveal its image name and tag name. Once you get this information, use: sudo docker pull <image_name:image_tag>
To check the existing docker images:
sudo docker images
or
sudo docker image ls
To remove a docker image:
sudo docker rmi <image_name:image_tag>
or
sudo docker rmi <image_ID>
To check all the existing containers:
sudo docker ps -a
To remove a created container.
sudo docker rm <container_name>
or
sudo docker rm <container_ID>
To query the size of a running docker container:
docker ps –size
To query the size of all the containers:
sudo docker ps –all –size
To start a NEW container interactively of a docker image:
sudo docker run -it <image_name:image_tag>
For example:
sudo docker run -it ubuntu:22.04
To start an EXISTING container interactively:
xhost +local:docker
sudo docker start -ai <container_name>
Please note that you cannot start a docker container that is already running.
If a container is already running, you can enter the container in a new terminal using:
sudo docker exec -it <container_name> bash
If you close the terminal without typing “exit” from a docker container, then the container is still running.
If “exit” is typed, the container is no more running.
One indicator of a Docker container still running is when your cursor returns nothing forever upon your your typing: sudo docker start -ai <container_name>. If this happens, you should use: sudo docker exec -it <container_name> bash.
Please note that inside docker, i.e., in your docker prompt, you should avoid using sudo because you are already at the root.
To save a container to an image:
sudo docker commit <container_name> <image_name:tag_name>
If the tag name is not specified, the default “latest” tag name will be used.
Running Docker Container with GUI
To run a container from an image by allowing GUIs and connecting the container to a volume in /home, without GPU acceleration, you use:
docker run -it –name <container_name> -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v ~/volume_folder_name:/home/volume_folder_name <image_name:tag_name>
To run a container from an image by allowing GUIs and connecting the container to a volume in /home, with NVIDIA GPU acceleration, you use:
docker run -it –name <container_name> –gpus all -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v ~/volume_folder_name:/home/volume_folder_name -v /dev/snd:/dev/snd -v /run/user/$(id -u)/pulse:/run/user/$(id -u)/pulse -e PULSE_SERVER=unix:/run/user/$(id -u)/pulse -e __GLX_VENDOR_LIBRARY_NAME=nvidia -e __NV_PRIME_RENDER_OFFLOAD=1 <image_name:tag_name>
To run a Docker container that allows GUIs to be functional, first of all you should allow X11 connection by typing the following in your host prompt:
xhost +local:docker
