Commonly Used Docker Commands
Basic Docker Commands
docker version: Retrieve installed Docker version detailsdocker info: View full Docker system overview, including total images, containers, and storage driver infodocker --help: Access built-in Docker help documentation for command references
Image Management Commands
docker images: List all locally stored Docker imagesdocker search <image-name>: Search for official and community images on Docker Hubdocker pull <image-name>:<tag>: Download a specified image from Docker Hub; omitting the tag defaults to thelatestversiondocker rmi <image-id>: Delete a local image by its IDdocker image prune: Remove all unused local images not referenced by any container
Container Management Commands
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]: Create and launch a new cotnainer from a specified imagedocker ps: List currently running containers; add the-aflag to show all containers including stopped onesdocker stop <container-id/name>: Gracefully stop a running containerdocker start <container-id/name>: Launch a previously stopped containerdocker restart <container-id/name>: Restart a running or stopped containerdocker rm <container-id/name>: Delete a stopped container; use the-fflag to force remove a running containerdocker exec -it <container-id/name> /bin/sh: Attach an interactive shell to a running container
Core Container Operation Examples
- Pull the official Nginx image:
docker pull nginx:latest
- List all local images:
docker images
- Create and run a detached Nginx container named
web-demomapped to host port 8080:
docker run --name web-demo -p 8080:80 -d nginx:latest
- View all containers (running and stopped):
docker ps -a
- Stop the
web-democontainer:
docker stop web-demo
- Reactivate the stopped
web-democontainer:
docker start web-demo
- Delete the stopped
web-democontainer:
docker rm web-demo
- Attach an interactive shell to the running
web-democontainer:
docker exec -it web-demo /bin/bash
Network Management Commands
docker network ls: List all existing Docker networksdocker network create <network-name>: Create a custom Docker bridge networkdocker network rm <network-name>: Delete an unused Docker networkdocker network connect <network-name> <container-name>: Attach a container to a specified networkdocker network disconnect <network-name> <container-name>: Remove a container from a specified network
Volume Management Commands
docker volume ls: List all locally created Docker volumesdocker volume create <volume-name>: Create a named Docker volume for persistent storagedocker volume inspect <volume-name>: View detailed configuration and path info for a volumedocker volume rm <volume-name>: Delete an unused Docker volume
Docker Compose Commands
Docker Compose is a tool for defining and running multi-container Docker applications using YAML configuration files.
docker-compose up -d: Launch all services defined indocker-compose.ymlin detached modedocker-compose down --volumes: Stop and remove all services, along with associated volumesdocker-compose ps: List active Compose-managed servicesdocker-compose logs <service-name>: View log output for a specified servicedocker-compose exec <service-name> <command>: Run a command inside a running Compose service
Docker Swarm Cluster Commands
Docker Swarm provides native container orchestration for deploying and managing clustered container applications.
docker swarm init --advertise-addr <node-ip>: Initialize a new Swarm cluster on the current nodedocker swarm join --token <join-token> <manager-ip:port>: Add a worker node to an existing Swarm clusterdocker node ls: List all nodes in the active Swarm clusterdocker service create --replicas 3 -p 80:80 --name web-service nginx:latest: Deploy a replicated service on the Swarmdocker service ls: List all services running in the Swarm clusterdocker service scale web-service=5: Adjust the number of replicas for a Swarm servicedocker service rm web-service: Remove a specifeid Swarm service
Advanced Container and Image Operations
- Create a custom image from a modified container: First list all containers to get the target ID:
docker ps -a
Then commit the changes to a new image:
docker commit <container-id> custom-nginx:v1
- Export a container to a tar archive:
docker export <container-id> > demo-container.tar
- Import an exported tar archive as a new image:
cat demo-container.tar | docker import - imported-image:v1
- Save a local image to a compressed tar file:
docker save -o nginx-backup.tar nginx:latest
- Load an image from a tar archive:
docker load -i nginx-backup.tar
- Build an image from a Dockerfile in the current directory:
docker build -t my-app:v1 .
- View real-time resource usage for a running container:
docker stats web-demo
- Clean up unused Docker resources:
- Remove all stopped containers:
docker container prune - Remove all unused volumes:
docker volume prune - Remove all unused images, networks, and volumes:
docker system prune -a
- Remove all stopped containers: