Essential Docker Commands

ជំពូកនេះមិនទាន់ត្រូវបានបកប្រែជាភាសាខ្មែរនៅឡើយទេ — សូមអានជាភាសាអង់គ្លេសខាងក្រោម។

Table of Contents

  1. Inspection & Monitoring Commands
  2. Image Management Commands
  3. Execution & Lifecycle Commands
  4. 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 ps Lists 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 images Lists all Docker images stored locally on your machine.

    # View all local images with tags and sizes
    docker images
    
  • docker logs Fetches 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 inspect Returns 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 pull Downloads 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 rmi Removes 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 run Creates and starts a new container from an image. Combines docker create and docker 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 exec Executes 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 stop Gracefully stops a running container by sending a SIGTERM signal, followed by SIGKILL if it doesn't stop within the timeout period.

    # Stop a running container
    docker stop <container_name_or_id>
    
  • docker start Starts one or more stopped containers without re-creating them.

    # Restart a previously stopped container
    docker start <container_name_or_id>
    
  • docker restart Stops and immediately restarts a running or stopped container.

    # Restart a container
    docker restart <container_name_or_id>
    
  • docker rm Removes 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 prune Frees 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

CommandPurpose
docker psList containers
docker imagesList local images
docker pullFetch an image from registry
docker runCreate and start a container
docker execExecute command in running container
docker logsPrint container logs
docker inspectOutput low-level JSON details
docker stopGracefully stop container
docker startStart stopped container
docker restartStop and restart container
docker rmDelete stopped container
docker rmiDelete local image
docker system pruneRemove unused/dangling resources

ទិញកាហ្វេជូនខ្ញុំ

ចូលចិត្តមេរៀននេះមែនទេ? សូមពិចារណាទិញកាហ្វេជូនខ្ញុំមួយកែវ ដើម្បីគាំទ្រការបង្កើតមាតិកាឥតគិតថ្លៃបន្ថែមទៀត!