Post

Mastering Docker - Essential Commands and Tips

Last Updated: April 20, 2024

Introduction –

In this article, we will explore a collection of useful Docker commands that will empower you to effectively manage your containers, images, and volumes. Additionally, we’ll dive into Docker Compose commands, which enable you to define and manage multi-container applications.

Useful Docker commands

List all running and exited containers

1
docker ps -a

Start specific Docker container based on container ID

1
docker start <container>

Stop specified container based on container ID

1
docker stop <container>

Stop all containers (forcefully)

1
docker kill $(docker ps -q)

Restart a container

1
docker restart <container>

Remove all containers

1
docker rm `docker ps -qa`

Remove all unused local volumes

1
docker volume prune

Run commands inside docker container

1
docker exec -it <container-id> /bin/bash

Remove all local volumes

1
docker volume ls -q -f driver=local | xargs -r docker volume rm

List all Docker volumes

1
docker volume list

View speficic data volume details

1
docker volume inspect <volume-name>

Inspect container resource usage:

1
docker stats <container>

View logs of a running container:

1
docker logs <container>

List all Docker images

1
docker images

Remove a Docker image:

1
docker image rm <image>

Remove all Docker images:

1
docker image prune -a

Remove all unused Docker images:

1
docker image prune

Pull an image from a Docker registry:

1
docker pull <image-name>

Rename a Docker container:

1
docker rename <old-container-name> <new-container-name>

Pause a running container:

1
docker pause <container>

Unpause a paused container:

1
docker unpause <container>

Display the Docker version information:

1
docker version

Docker Compose commands

Start docker compose containers in the background

1
docker compose up -d

View the status of running docker-compose services:

1
docker compose ps

Start docker-compose containers and force a build:

1
docker compose up --build

Rebuild and start the docker-compose services in the background:

1
docker compose up -d --build

Stop and remove containers, networks, and volumes defined in the docker-compose file:

1
docker compose down

Install, start, and enable Docker on CentOS

Feel free to use these commands or read this article on how to install Docker on CentOS Stream 9,

1
2
3
4
5
6
7
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo systemctl enable docker.service
sudo systemctl enable containerd.service
sudo systemctl start docker.service
sudo docker run hello-world

Manage Docker as a non-root user

1
2
3
4
5
6
sudo groupadd docker
sudo usermod -aG docker $USER
logout

newgrp docker
docker run hello-world
This post is licensed under Apache License 2.0 by the author.