Docker Configuration and Command Reference Guide

Docker Configuration and Command Reference Guide

This guide covers essential Docker configuration settings and commonly used commands for managing images, containers, networks, and volumes.

Configuring Docker Registry Mirrors

If you experience slow or restricted access to Docker's official registries due to network conditions (common in certain regions or with specific ISPs), configuring a mirror registry significantly improves performance.

Create or edit the Docker daemon configuration file:

sudo nano /etc/docker/daemon.json

Add the following content, ensuring valid JSON syntax:

{
  "registry-mirrors": [
    "https://docker.m.daocloud.io",
    "https://registry.docker-cn.com",
    "https://hub-mirror.c.163.com"
  ]
}

Apply the changes by restarting the Docker daemon:

sudo systemctl daemon-reload
sudo systemctl restart docker

Docker User Permissions

By default, the Docker daemon communicates with CLI tools through a Unix socket located at /var/run/docker.sock. This socket is owned by root for security reasons, requiring either root access or docker group membership to execute Docker commands.

Temporary workaround: Use sudo prefix for individual commands:

sudo docker pull nginx:latest

Persistent solution: Add your user to the docker group:

sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker

Configuring HTTP/HTTPS Proxy for Docker

In environments requiring a proxy for external network access (corporate networks or VPNs), Docker daemon-level proxy configuration is necessary since host-level proxy settings won't affect container operations.

Create the systemd service configuration directory:

sudo mkdir -p /etc/systemd/system/docker.service.d

Create the proxy configuration file:

sudo nano /etc/systemd/system/docker.service.d/http-proxy.conf

Add your proxy settings:

[Service]
Environment="HTTP_PROXY=http://192.168.1.100:8080/"
Environment="HTTPS_PROXY=http://192.168.1.100:8080/"
Environment="NO_PROXY=localhost,127.0.0.1,.internal.local"

Restart Docker to apply proxy changes:

sudo systemctl daemon-reload
sudo systemctl restart docker

Docker System Information Commands

  • Display detailed Docker version: docker version
  • Display concise version: docker --version or docker -v
  • Show system-wide information: docker info (includes container count, image count, storage driver, resources)
  • List all available commands: docker --help
  • Help for specific command: docker [command] --help
  • Check disk usage: docker system df
    • -v: Show detailed breakdown
  • Clean up unused data: docker system prune
    • -a: Include all unused images
    • --volumes: Include unused volumes
    • -f: Force cleanup without confirmation

Image Management Commands

  • List local images: docker images [OPTIONS] [REPOSITORY[:TAG]]
    • -a, --all: Show all images including intermediate layers
    • -q, --quiet: Display only image IDs
    • --digests: Show digests
    • --filter: Filter output (e.g., dangling=true)
  • Pull image from registry: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
    • -a, --all-tags: Pull all tagged images in repository
    • --quiet: Suppress progress output
  • Push image to remote registry: docker push [OPTIONS] NAME[:TAG]
  • Remove image: docker rmi [OPTIONS] IMAGE [IMAGE...]
    • -f, --force: Force removal even if containers are using it
    • --no-prune: Do not delete parent images
  • Build image from Dockerfile: docker build [OPTIONS] PATH | URL | -
    • -t, --tag list: Name and tag in name:tag format
    • -f, --file: Path to Dockerfile
    • --build-arg: Set build-time variables
    • --no-cache: Build without using cache
  • Tag image for registry: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
  • View image build history: docker history IMAGE
  • View image metadata: docker inspect [OPTIONS] IMAGE
  • Export image to tar: docker save -o filename.tar IMAGE [IMAGE...]
  • Import image from tar: docker load -i filename.tar

Container Lifecycle Commands

  • Create and run container: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
    • -d, --detach: Run container in background
    • -it: Allocate pseudo-TTY and keep stdin open
    • --name: Assign custom name to container
    • -p, --publish: Map host port to container port
    • -P, --publish-all: Map all exposed ports to random host ports
    • -v, --volume: Mount volume (host_path:container_path or volume_name:container_path)
    • -e, --env: Set environment variables
    • --network: Connect container to network
    • --network=host: Use host's network namespace
    • --cpus: Limit CPU cores
    • --memory: Limit maximum memory
    • --restart: Set restart policy (no, always, on-failure, unless-stopped)
    • --rm: Auto-remove container on exit
  • List containers: docker ps [OPTIONS]
    • -a, --all: Show all containers (including stopped)
    • -q, --quiet: Display only container IDs
    • -l, --latest: Show latest created container
    • -n: Show last n created containers
    • --filter: Filter by status or attributes
  • Start stopped container: docker start CONTAINER...
  • Stop running container gracefully: docker stop CONTAINER...
  • Kill container immediately: docker kill CONTAINER...
  • Restart container: docker restart CONTAINER...
  • Suspend all processes in container: docker pause CONTAINER...
  • Resume all processes in container: docker unpause CONTAINER...
  • Remove container: docker rm [OPTIONS] CONTAINER...
    • -f, --force: Force removal including running containers
    • -v, --volumes: Remove associated anonymous volumes
  • Execute command in running container: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
    • -it: Allocate pseudo-TTY
  • Attach to container's main process: docker attach CONTAINER
  • View container logs: docker logs [OPTIONS] CONTAINER
    • -f, --follow: Follow log output in real-time
    • --tail: Show last n lines
  • Inspect container details: docker inspect CONTAINER
  • List running processes in container: docker top CONTAINER
  • Show port mappings: docker port CONTAINER
  • Copy files between host and container: docker cp [OPTIONS] SRC_PATH DEST_PATH
  • Rename container: docker rename OLD_NAME NEW_NAME
  • Update container resources dynamically: docker update [OPTIONS] CONTAINER
    • --cpus: Update CPU limit
    • --memory: Update memory limit
  • Wait for container to stop and return exit code: docker wait CONTAINER
  • Stream real-time events from daemon: docker events [OPTIONS]
    • --since: Show events since timestamp
  • Export container filesystem: docker export -o filename.tar CONTAINER
  • Import container as image: docker import filename.tar IMAGE_NAME:TAG

Network Management Commands

  • List networks: docker network ls
  • Create custom network: docker network create [OPTIONS] NETWORK
    • --driver: Specify driver (bridge, host, overlay, none)
  • Connect container to network: docker network connect NETWORK CONTAINER
  • Disconnect container from network: docker network disconnect NETWORK CONTAINER
  • Inspect network details: docker network inspect NETWORK
  • Remove network: docker network rm NETWORK
  • Prune unused networks: docker network prune

Volume Management Commands

  • List volumes: docker volume ls
  • Create volume: docker volume create VOLUME_NAME
  • Inspect volume details: docker volume inspect VOLUME_NAME
  • Remove volume: docker volume rm VOLUME_NAME
  • Prune unused volumes: docker volume prune

Registry Commands

  • Login to registry: docker login [OPTIONS] [SERVER]
  • Logout from registry: docker logout [SERVER]
  • Search Docker Hub for images: docker search TERM

Dockerr Compose Commands (v2)

  • Start services in background: docker compose up [OPTIONS]
    • -d: Run in detached mode
    • --build: Build images before starting
  • Stop and remove services: docker compose down [OPTIONS]
    • -v: Remove volumes defined in compose file
  • List service status: docker compose ps
  • View service logs: docker compose logs [OPTIONS] [SERVICE]
    • -f: Follow logs in real-time
  • Execute command in service container: docker compose exec SERVICE COMMAND
  • Restart services: docker compose restart [SERVICE]
  • Pull service images: docker compose pull
  • Start services: docker compose start
  • Stop services: docker compose stop
  • Pause services: docker compose pause

Docker Swarm Commands

  • Initialize Swarm cluster: docker swarm init [OPTIONS]
    • --advertise-addr: Specify IP for manager advertising
  • Get join token: docker swarm join-token worker|manager
  • Join existing Swarm: docker swarm join --token TOKEN MANAGER_IP:2377
  • Leave Swarm cluster: docker swarm leave [OPTIONS]
    • -f: Force leave if manager node
  • List cluster nodes: docker node ls
  • Inspect node details: docker node inspect NODE
  • Update node: docker node update [OPTIONS] NODE
    • --role: Change node role (manager/worker)
    • --availability: Change task scheduling (active/pause/drain)
  • Remove node from cluster: docker node rm NODE
  • Create service: docker service create [OPTIONS] IMAGE
    • --name: Service name
    • -p: Publish port
    • --replicas: Number of replicas
  • List services: docker service ls
  • List service tasks: docker service ps SERVICE
  • Scale service: docker service scale SERVICE=REPLICAS
  • Update service: docker service update [OPTIONS] SERVICE
    • --image: New image and tag
  • Remove service: docker service rm SERVICE
  • View service logs: docker service logs -f SERVICE

Common Docker One-Liner Commands

  • Remove all stopped containers:``` docker rm $(docker ps -a -q -f "status=exited")

  • Remove all dangling (untagged) images:``` docker rmi $(docker images -q -f "dangling=true")

  • Complete system cleanup:``` docker system prune -a --volumes -f

  • Access all running containers:``` for c in $(docker ps -q); do docker exec -it $c sh; done

    
    

Dockerfile Instructions Reference

  • FROM image:tag - Base image (must be first instruction)
  • LABEL key="value" - Add metadata
  • ENV key value - Set environment variable
  • WORKDIR /path - Set working directory
  • COPY src dest - Copy files from host to container
  • ADD src dest - Copy and auto-extract archives
  • RUN command - Execute command during build
  • EXPOSE port - Declare ports to listen on
  • VOLUME ["/path"] - Create mount point for external storage
  • CMD ["executable", "param"] - Default command (overridable)
  • ENTRYPOINT ["executable"] - Main executable (harder to override)
  • USER user - Set user for subsequent commands
  • ARG name=default - Build-time variable (not persisted to container)

Tags: docker containerization docker-compose dockerfile docker-swarm

Posted on Sun, 23 Aug 2026 16:52:33 +0000 by trawets