Master Docker: Essential Commands for Efficient Container Management
This guide presents a comprehensive collection of essential Docker commands, covering installation, image handling, container lifecycle, network and volume management, logging, and system cleanup, enabling developers and operations engineers to efficiently manage Docker environments and streamline their workflows.
Docker Common Commands Reference
Docker Common Commands
Docker is an open‑source container engine widely used for developing, deploying, and running distributed applications. Mastering common Docker commands is crucial for developers and operations staff.
1. Installation and Version Management
1.1 Check Docker version
docker --versionDisplays the currently installed Docker version.
1.2 Check Docker service status
docker infoShows detailed Docker system information, helping you understand the runtime status.
1.3 Start/Stop Docker service (Linux)
# Start Docker service
sudo systemctl start docker
# Stop Docker service
sudo systemctl stop docker
# Restart Docker service
sudo systemctl restart docker2. Image Management
2.1 Pull image
docker pull <image_name>:<tag>For example, pull the latest nginx image:
docker pull nginx:latest2.2 List local images
docker imagesLists all local Docker images.
2.3 Search images
docker search <keyword>For example, search for a Redis image:
docker search redis2.4 Delete image
docker rmi <image_id_or_name>Deletes the specified image; if the image is used by a container, remove the container first.
2.5 Build image
docker build -t <image_name>:<tag> .Builds an image using a Dockerfile.
2.6 Export and import image
# Export image
docker save -o <filename>.tar <image_name>
# Import image
docker load -i <filename>.tar3. Container Management
3.1 Run container
docker run <image_name>Example: start a container from the nginx image:
docker run nginx3.2 Run container in background
docker run -d <image_name>Example: run Redis in detached mode:
docker run -d redis3.3 List running containers
docker psShows all currently running containers.
3.4 List all containers (including stopped)
docker ps -a3.5 Delete container
docker rm <container_id_or_name>Deletes a container; if it is running, stop it first.
3.6 Stop container
docker stop <container_id_or_name>Stops a running container.
3.7 Exec into container
docker exec -it <container_id_or_name> /bin/bashOpens an interactive shell inside the container.
3.8 View container logs
docker logs <container_id_or_name>Displays the container's standard output and error logs.
3.9 Follow container logs
docker logs -f <container_id_or_name>Streams logs in real time.
3.10 View container resource usage
docker stats <container_id_or_name>Shows real‑time CPU, memory, and other resource usage.
3.11 Inspect container
docker inspect <container_id_or_name>Displays detailed configuration information of the container.
3.12 Export and import container
# Export container to tar file
docker export <container_id> -o <filename>.tar
# Import container from tar file
docker import <filename>.tar <image_name>4. Network Management
4.1 List networks
docker network ls4.2 Create network
docker network create <network_name>4.3 Delete network
docker network rm <network_name>4.4 Connect container to network
docker network connect <network_name> <container_name>4.5 Disconnect container from network
docker network disconnect <network_name> <container_name>4.6 Inspect network
docker network inspect <network_name>5. Volume Management
5.1 List volumes
docker volume ls5.2 Create volume
docker volume create <volume_name>5.3 Delete volume
docker volume rm <volume_name>5.4 Inspect volume
docker volume inspect <volume_name>5.5 Mount volume to container
docker run -v <volume_name>:/path/in/container <image_name>6. Logging and Debugging
6.1 View container logs
docker logs <container_id_or_name>6.2 Follow logs in real time
docker logs -f <container_id_or_name>6.3 View container resource usage
docker stats <container_id_or_name>6.4 Inspect container details
docker inspect <container_id_or_name>7. System Management and Cleanup
7.1 View Docker system information
docker info7.2 Prune unused resources
docker system prune -fThe -f flag skips the confirmation prompt.
7.3 Prune unused images
docker image prune -a -f7.4 Prune stopped containers
docker container prune -f7.5 Prune unused volumes
docker volume prune -f7.6 Prune unused networks
docker network prune -f8. Common Composite Commands
8.1 Prune all unused resources (images, containers, volumes, networks)
docker system prune -af --volumes8.2 Stop and remove all containers
docker stop $(docker ps -q) && docker rm $(docker ps -a -q)8.3 Remove all unused images, networks and volumes
docker image prune -a -f && docker volume prune -f && docker network prune -fConclusion
By mastering these common Docker commands, you can manage Docker environments more efficiently, streamline workflows, and improve development and operations productivity. As projects grow, proper container and image management becomes critical. We hope this summary helps you enhance your Docker skills.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
