Essential Docker Commands
ជំពូកនេះមិនទាន់ត្រូវបានបកប្រែជាភាសាខ្មែរនៅឡើយទេ — សូមអានជាភាសាអង់គ្លេសខាងក្រោម។
Table of Contents
- Inspection & Monitoring Commands
- Image Management Commands
- Execution & Lifecycle Commands
- Maintenance & Garbage Collection
Mastering Docker comes down to understanding a core set of CLI commands. This reference guide breaks down the essential commands every developer needs for inspecting, executing, managing, and maintaining Docker containers and system resources.
1. Inspection & Monitoring Commands
-
docker psLists active and running containers on your system.# List only currently running containers docker ps # List all containers (running, stopped, and exited) docker ps -a # Show only container IDs (useful for scripts/automation) docker ps -q -
docker imagesLists all Docker images stored locally on your machine.# View all local images with tags and sizes docker images -
docker logsFetches stdout/stderr logs from a specific container. Critical for debugging background processes.# Fetch current logs docker logs <container_name_or_id> # Stream/follow logs in real-time docker logs -f <container_name_or_id> # View the last 50 log entries docker logs --tail 50 <container_name_or_id> -
docker inspectReturns detailed low-level internal configurations and metadata (IP address, volume mounts, network settings, environment variables) in JSON format.# Inspect complete container configuration docker inspect <container_name_or_id> # Extract specific metadata using formatting (e.g., retrieve IP address) docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name>
2. Image Management Commands
-
docker pullDownloads a Docker image from a remote registry (like Docker Hub) without instantiating a container.# Pull a specific image tag docker pull postgres:16-alpine -
docker rmiRemoves one or more local Docker images from your system.# Remove an image by tag or ID docker rmi postgres:16-alpine # Force removal of an image in use by a stopped container docker rmi -f <image_id>
3. Execution & Lifecycle Commands
-
docker runCreates and starts a new container from an image. Combinesdocker createanddocker start.# Common pattern: run detached (-d), named (--name), with port mapping (-p host:container) docker run -d --name my-redis -p 6379:6379 redis:alpine -
docker execExecutes a new command inside an already running container.# Open an interactive shell inside a running container docker exec -it my-redis sh # Run a single non-interactive command inside a container docker exec my-redis redis-cli ping -
docker stopGracefully stops a running container by sending aSIGTERMsignal, followed bySIGKILLif it doesn't stop within the timeout period.# Stop a running container docker stop <container_name_or_id> -
docker startStarts one or more stopped containers without re-creating them.# Restart a previously stopped container docker start <container_name_or_id> -
docker restartStops and immediately restarts a running or stopped container.# Restart a container docker restart <container_name_or_id> -
docker rmRemoves stopped containers from disk.# Remove a stopped container docker rm <container_name_or_id> # Force remove a currently running container (-f) docker rm -f <container_name_or_id>
4. Maintenance & Garbage Collection
-
docker system pruneFrees up disk space by removing all stopped containers, unused networks, dangling images, and build caches in one command.# Clean up unused Docker resources docker system prune # Comprehensive deep clean (removes all unused images and persistent volumes) docker system prune -a --volumes
Command Summary Quick Reference
| Command | Purpose |
|---|---|
docker ps | List containers |
docker images | List local images |
docker pull | Fetch an image from registry |
docker run | Create and start a container |
docker exec | Execute command in running container |
docker logs | Print container logs |
docker inspect | Output low-level JSON details |
docker stop | Gracefully stop container |
docker start | Start stopped container |
docker restart | Stop and restart container |
docker rm | Delete stopped container |
docker rmi | Delete local image |
docker system prune | Remove unused/dangling resources |
ទិញកាហ្វេជូនខ្ញុំ
ចូលចិត្តមេរៀននេះមែនទេ? សូមពិចារណាទិញកាហ្វេជូនខ្ញុំមួយកែវ ដើម្បីគាំទ្រការបង្កើតមាតិកាឥតគិតថ្លៃបន្ថែមទៀត!