Cloud Native 10 min read

Docker Command Cheat Sheet: Container, Image, and Container Operations

This guide provides a comprehensive collection of Docker commands for inspecting container information, managing images (listing, searching, pulling, removing, building), and operating containers (starting, stopping, attaching, executing commands, viewing logs, inspecting, committing, and copying files) with practical examples.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Docker Command Cheat Sheet: Container, Image, and Container Operations

1. Docker Container Information

Use the following commands to query Docker version, detailed daemon information, and help options.

## View Docker version
docker version
## View Docker daemon information
docker info
## Show Docker help
docker --help

2. Image Operations

Docker images can be referenced by name, long ID, or short ID.

2.1 View Images

## List local images
docker images
## Include intermediate layers
docker images -a
## Show only image IDs
docker images -q
## Show image IDs with intermediate layers
docker images -qa
## Show digests column
docker images --digests
## Show full image information without truncation
docker images --no-trunc

2.2 Search Images

## Search Docker Hub for MySQL image
docker search mysql
## Filter by stars >= 600
docker search --filter=stars=600 mysql
## Show full description without truncation
docker search --no-trunc mysql
## Show only automated images
docker search --automated mysql

2.3 Pull Images

## Pull the latest official Redis image
docker pull redis
## Pull all tags of Redis
docker pull -a redis
## Pull a private repository image
docker pull bitnami/redis

2.4 Remove Images

## Remove a single image
docker rmi redis
## Force removal (useful if containers are based on the image)
docker rmi -f redis
## Remove multiple images (space‑separated)
docker rmi -f redis tomcat nginx
## Remove all local images
docker rmi -f $(docker images -q)

2.5 Build Images

## (1) Write a Dockerfile
cd /docker/dockerfile
vim mycentos
## (2) Build the image
docker build -f /docker/dockerfile/mycentos -t mycentos:1.1

3. Container Operations

Container commands can use either CONTAINER ID or container name.

3.1 Start Containers

## Create and start a container with interactive terminal and name
docker run -i -t --name mycentos
## Start container in detached mode
docker run -d mycentos
## Start an existing stopped container
docker start redis
## Restart a container
docker restart redis

Note: Using docker ps -a after a detached start may show the container as exited because Docker requires a foreground process to keep the container alive.

3.2 Container Processes

## List processes inside a container (similar to ps)
# docker top [OPTIONS] CONTAINER [ps OPTIONS]
## Show processes in the redis container
docker top redis
## Show processes for all running containers
for i in `docker ps | grep Up | awk '{print $1}'`; do echo && docker top $i; done

3.3 Container Logs

## View logs (default)
docker logs rabbitmq
## Follow logs, show timestamps, limit to last 20 lines
docker logs -f -t --tail=20 redis
## Show the latest 10 logs since a specific date
docker logs --since "2019-05-21" --tail=10 redis

3.4 Attach / Exec

## Run container and attach immediately
docker run -it centos /bin/bash
## Exit container (stop)
exit
## Detach without stopping (Ctrl+P+Q)
## Attach to a running container (shared screen)
# Ensure Ctrl‑D/Ctrl‑C do not stop the container
docker attach --sig-proxy=false centos
## Open a new interactive exec session
docker exec -i -t centos /bin/bash
## Run a command and return output to current terminal
docker exec -i -t centos ls -l /tmp
## Run a command in detached mode (no output)
docker exec -d centos touch cache.txt

3.5 View Containers

## List running containers
docker ps
## Show only container IDs
docker ps -q
## List all containers (running + stopped)
docker ps -a
## Show total file size of running containers
docker ps -s
## Show most recently created container
docker ps -l
## Show the last 3 created containers
docker ps -n 3
## Do not truncate output
docker ps --no-trunc

3.6 Stop / Remove Containers

## Stop a running container
docker stop redis
## Kill a running container
docker kill redis
## Remove a stopped container
docker rm redis
## Force remove a running container
docker rm -f redis
## Remove multiple containers
docker rm -f $(docker ps -a -q)
# Or using xargs
docker ps -a -q | xargs docker rm
## Remove container network link named db
docker rm -l db
## Remove container and its volumes
docker rm -v redis

3.7 Commit Container to Image

## Create a new image from a running container
# -a author, -c Dockerfile command, -m commit message, -p pause container during commit
docker commit -a="DeepInThought" -m="my redis" [container_id] myredis:v1.1

3.8 Copy Files Between Host and Container

## Copy from container to host
docker cp rabbitmq:/[container_path] [local_path]
## Copy from host to container
docker cp [local_path] rabbitmq:/[container_path]/
## Copy and rename destination directory inside container
docker cp [local_path] rabbitmq:/[container_path]
cloud nativeDockerDevOpsContainersImagescommands
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.