Guide to Essential Docker Commands
Docker has long become a standard tool for containerizing applications, and this guide will help you understand the basic commands needed for everyday work.
How Docker Commands Work
Most Docker commands follow a simple pattern:
docker + command + optional parameters and flags.
It’s best to start with commands that are useful in any CLI tool: checking the version and using the built-in help.
Check the Docker version:
docker --version
Open the general Docker help:
docker --help
If you need more details about a specific command, you can request help for that command to see all available flags and usage options. For example, for run:
docker run --help
Working with Docker Images
Images are the foundation of containers. An image contains everything required to run an application: code, runtime environment, libraries, and dependencies.
Below are the key commands for searching, downloading, building, and removing images.
Searching for Images
Before using an image, you need to find it in a registry. By default, Docker works with Docker Hub, which hosts thousands of ready-made images.
For example, searching for nginx:
sudo docker search nginx
The command displays a list of matching images with descriptions and ratings, making it easier to choose a popular and well-supported image.
Pulling Images
Once you’ve found the image you need, you can download it to your server. To pull the latest version:
sudo docker pull nginx
If you need a specific version, specify the tag:
sudo docker pull nginx:1.25.3
Viewing Local Images
To see all images already present on your system, run:
sudo docker images
The output typically includes the repository name, tag, image ID, creation date, and size.
Building Your Own Image
If you need a custom image, it’s usually built from a Dockerfile. Here’s an example of a simple Dockerfile in the current directory:
FROM ubuntu:24.04
RUN apt update && apt install -y nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
This Dockerfile creates an image based on Ubuntu 24.04, installs Nginx, and runs it as a web server.
Build the image:
sudo docker build -t myapp:1.0 .
The -t flag sets the image name and version (tag). The dot at the end means the build context is the current directory, where Docker will look for the Dockerfile.
Removing Images
To remove an unused image:
sudo docker rmi nginx:latest
If the image is used by a running or stopped container, Docker won’t allow you to remove it. In that case, remove the container first or use force removal:
docker rm container_name
docker rmi -f nginx:latest
Managing Docker Containers
A container is a running instance of an image. In practice, it’s important to know how to start containers, list them, stop, restart, and remove them.
Running a Container
The simplest way to run a container from an image:
sudo docker run nginx
In this case, the container runs in the foreground and occupies the terminal. In real-world usage, background mode is more common:
sudo docker run -d nginx
It’s also useful to map ports and assign a readable name:
sudo docker run -d -p 8080:80 --name my-webserver nginx
Here, -d runs the container in the background, -p 8080:80 maps port 8080 on the host to port 80 inside the container, and –name assigns the name my-webserver.
Listing Containers
Show only running containers:
sudo docker ps
Show all containers, including stopped ones:
sudo docker ps -a
Stopping, Starting, and Restarting
First, check which containers exist:
sudo docker ps -a
Stop a container:
sudo docker stop my-webserver
Start a stopped container:
sudo docker start my-webserver
Restart a container (stop + start):
sudo docker restart my-webserver
Removing Containers
Remove a container:
sudo docker rm test
If the container is running, Docker won’t allow its removal without extra steps. You can either stop it first or remove it forcefully:
sudo docker rm -f test
The -f flag immediately terminates the container without a graceful shutdown, so it should be used carefully.
Monitoring and Debugging Containers
To keep containers running smoothly and troubleshoot issues faster, it’s useful to know commands for logs, container access, and diagnostics.
Viewing Logs
Show container logs:
sudo docker logs test
Follow logs in real time (similar to tail -f):
sudo docker logs -f test
Executing Commands Inside a Container
One of Docker’s most convenient features is the ability to enter a running container and execute commands directly in its environment:
sudo docker exec -it test /bin/bash
The -it flags open an interactive terminal, allowing you to inspect files, view configuration, and debug application behavior.
Inspecting Container Details
The inspect command outputs detailed information about a container in JSON format: configuration, networking, volumes, environment variables, and current state.
sudo docker inspect test
Resource Usage
To see real-time CPU, memory, and I/O usage by containers:
sudo docker stats
Docker Cleanup and Maintenance
Over time, Docker accumulates stopped containers, unused images, networks, and other leftovers. Regular cleanup helps free disk space and keep your environment tidy.
Remove all stopped containers:
sudo docker container prune
Remove unused images:
sudo docker image prune
Now you know a basic set of Docker commands that covers the core tasks: searching and pulling images, running and managing containers, viewing logs and diagnostics, and keeping your system clean with regular maintenance.