After installing Docker, most container management tasks are performed from the command line. In this cheat sheet, we will cover the most useful Docker commands for starting, stopping, and removing containers, working with images, viewing logs, managing networks and volumes, and monitoring resource usage.
If Docker is not installed yet, follow our “How to Install Docker on Ubuntu 24.04” guide first.
Check the Docker Version
To check the installed Docker version:
docker --version
To display detailed information about your Docker installation:
docker info
List Docker Containers
To display currently running containers:
docker ps
To display all containers, including stopped containers:
docker ps -a
The output includes information such as the container ID, image, command, status, exposed ports, and container name.
Run a Docker Container
The basic command for creating and starting a container is:
docker run IMAGE
For example, to run an Nginx container:
docker run nginx
To run the container in the background, use the -d option:
docker run -d nginx
You can also assign a name to the container:
docker run -d --name web nginx
Using container names makes administration easier because you do not have to remember container IDs.
Expose Container Ports
To make a service running inside a container accessible from the server, use the -p option.
For example:
docker run -d --name web -p 80:80 nginx
The port mapping uses the following format:
HOST_PORT:CONTAINER_PORT
For example:
docker run -d -p 8080:80 nginx
In this case, Nginx listens on port 80 inside the container and can be accessed through port 8080 on the server.
Stop a Docker Container
To stop a running container:
docker stop web
You can also use the container ID:
docker stop CONTAINER_ID
Start a Stopped Container
To start an existing stopped container:
docker start web
Restart a Container
To restart a running container:
docker restart web
This command is useful after changing an application's configuration or when troubleshooting a container.
Remove a Docker Container
First, stop the container:
docker stop web
Then remove it:
docker rm web
To force the removal of a running container:
docker rm -f web
Use forced removal carefully because it immediately stops and removes the container.
View Docker Container Logs
To view a container's logs:
docker logs web
To follow the logs in real time:
docker logs -f web
To display only the last 100 lines:
docker logs --tail 100 web
You can combine these options:
docker logs --tail 100 -f web
This is especially useful when troubleshooting applications running inside Docker containers.
Execute Commands Inside a Container
To execute a command inside a running container:
docker exec web COMMAND
For example:
docker exec web nginx -v
To open an interactive Bash shell:
docker exec -it web bash
Some minimal Docker images do not include Bash. In this case, try:
docker exec -it web sh
To exit the container shell:
exit
Inspect a Docker Container
To display detailed information about a container:
docker inspect web
The output contains information about networking, IP addresses, mounted volumes, environment variables, ports, and other container settings.
Monitor Docker Container Resources
To monitor CPU, memory, network, and disk I/O usage:
docker stats
To monitor a specific container:
docker stats web
Press Ctrl + C to exit.
This command can be useful when checking resource consumption on a VPS or dedicated server.
Working with Docker Images
To list downloaded Docker images:
docker images
To download an image from a registry:
docker pull nginx
To download a specific image tag:
docker pull nginx:alpine
To remove an image:
docker rmi nginx
Docker may prevent an image from being removed if it is still used by an existing container.
Working with Docker Volumes
Docker volumes provide persistent storage for container data.
To list existing volumes:
docker volume ls
Create a new volume:
docker volume create app-data
Display information about a volume:
docker volume inspect app-data
Remove a volume:
docker volume rm app-data
Be careful when removing volumes. They may contain databases, application files, configuration data, or other important information.
Working with Docker Networks
To list Docker networks:
docker network ls
Create a network:
docker network create app-network
Inspect a network:
docker network inspect app-network
Remove a network:
docker network rm app-network
Custom Docker networks are especially useful when multiple containers need to communicate with each other.
Configure Automatic Container Restart
For applications running on a server, you may want containers to start automatically after a server reboot.
For example:
docker run -d \
--name web \
--restart unless-stopped \
-p 80:80 \
nginx
The following option:
--restart unless-stopped
instructs Docker to automatically restart the container unless it was manually stopped.
Check Docker Disk Usage
Docker images, containers, volumes, and build cache can consume a significant amount of disk space over time.
To check Docker disk usage:
docker system df
For more detailed information:
docker system df -v
Clean Up Unused Docker Resources
To remove unused containers, networks, dangling images, and build cache:
docker system prune
Docker will ask for confirmation before deleting the data.
For a more aggressive cleanup:
docker system prune -a
The -a option also removes unused images.
Before running cleanup commands on a production server, make sure the resources are no longer required.
Useful Docker Compose Commands
If your application uses Docker Compose, the following commands are commonly used.
Start the project:
docker compose up -d
List containers:
docker compose ps
View logs:
docker compose logs -f
Restart services:
docker compose restart
Stop services:
docker compose stop
Stop and remove project containers:
docker compose down
Download newer versions of the images:
docker compose pull
For more information, see our “How to Install Docker Compose on Ubuntu 24.04” guide.
Docker Commands Cheat Sheet
Here are some of the most useful Docker commands for everyday server administration.
List running containers:
docker ps
List all containers:
docker ps -a
Start a container:
docker start NAME
Stop a container:
docker stop NAME
Restart a container:
docker restart NAME
View container logs:
docker logs -f NAME
Open a Bash shell inside a container:
docker exec -it NAME bash
List Docker images:
docker images
Monitor resource usage:
docker stats
Check Docker disk usage:
docker system df
Conclusion
For everyday Docker administration, you only need to remember a relatively small number of commands. Commands such as docker ps, docker logs, docker exec, docker stop, docker restart, and docker compose cover many common container management tasks.
Once you understand these commands, you can easily manage applications running in Docker containers, troubleshoot problems, monitor resource usage, and maintain Docker environments on VPS and dedicated servers.
If you are new to Docker, start with our “How to Install Docker on Ubuntu 24.04” guide and then continue with “How to Install Docker Compose on Ubuntu 24.04.”
RU
How to Install Docker Compose on Ubuntu 24.04