Essential Docker Commands Explained | Pull, Run, Inspect, Logs & Docker CP
This chapter covers the operational mechanics of Docker, bridging foundational knowledge with practical execution. You will learn how to pull, inspect, run, monitor, and transfer data between host systems and Docker containers using real-world commands and workflows.
1. Why Do We Need Operational Docker Commands?
Understanding Docker command structures is essential before executing operations. While fundamentals introduce what containers are, operational commands allow you to manage images, inspect runtime configurations, execute background demon services, and exchange files between host and container boundaries.
2. Core Mental Model: The Post Office & Shipping Container Analogy
To understand Docker registries, images, and containers, consider a global shipping network:
- DockerHub acts as the central shipping yard (similar to GitHub for code) where pre-built container images are stored and shared globally.
- Docker Images act as blueprints or locked shipping crates containing all required dependencies, binaries, and OS configurations.
- Docker Containers act as active, running trucks unpacked on the road, executing applications in isolated environments based on those blueprints.
3. Pulling Images from DockerHub (docker pull)
Before running an application, Docker must acquire its image. If the image does not exist locally on your machine, Docker queries DockerHub.
Pulling Base OS and Service Images
# Pull Ubuntu (defaults to the 'latest' tag)
docker pull ubuntu
# Pull Fedora
docker pull fedora
# Pull MySQL official image
docker pull mysql
Note: A tag is an explicit version identifier (e.g.,
latest,7,8.0). If omitted, Docker automatically defaults to pulling thelatesttag.
4. Viewing and Inspecting Images (docker images & docker image inspect)
To verify downloaded images and examine their deep metadata (environment variables, default commands, entry points, and architectural layers), use inspection tools.
Image Management Commands
# List all locally available images
docker images
# Inspect deep JSON metadata of an image
docker image inspect ubuntu
docker image inspect mysql
5. Running Containers: Interactive OS vs. Background Services
Running containers requires understanding the difference between interactive shells and background daemon services.
Running a Service Container (e.g., Nginx)
Services like Nginx run continuously in the background. We map host ports to container ports and assign custom names:
docker run -d -p 80:80 --name nginx-app nginx
-d: Run container in detached mode (background).-p 80:80: Map host port80to container port80.--name nginx-app: Assign a custom readable name instead of a random hash identifier.
Running an Interactive Base OS Container (e.g., Fedora / CentOS)
Base operating systems require an interactive terminal session to keep the container alive:
docker run -it --name fedora-app fedora
-it: Allocate a pseudo-TTY and keep stdin open for interactive terminal access.
6. Inspecting Running Containers & Extracting IPs
Just like images, running containers can be inspected. You can extract specific network details using standard Linux utilities paired with Docker inspection.
# Inspect full container metadata
docker inspect nginx-app
# Extract only the IP address of a running container using grep
docker inspect nginx-app | grep -A 4 "IPAddress"
7. Managing Container Processes, Logs, and Shell Access
Once containers are running, you need to monitor their health, view standard output logs, and execute commands inside them.
Common Runtime Management Commands
# List running containers
docker ps
# List all containers (including stopped/exited ones)
docker ps -a
# View real-time logs generated by a container
docker logs nginx-app
# View active processes inside a running container
docker top httpd-app
# Access the shell of a running container interactively
docker exec -it fedora-app /bin/bash
8. Copying Files Between Host and Containers (docker cp)
You can seamlessly transfer files back and forth across the host-container boundary without rebuilding images.
Host-to-Container File Copy
# Copy a local file into a running container's directory
docker cp new.txt centos-app:/folder/
Container-to-Host File Copy
# Copy a file generated inside a container back to the host machine
docker cp centos-app:/folder/new2.txt /home/ubuntu/
9. Cleanup & Bulk Deletion Workflows
As you build and test containers, stale images and exited containers accumulate. You can clean them up efficiently using force flags and command substitution.
Stopping and Removing Containers and Images
# Stop a running container gracefully
docker stop fedora-app
# Remove a specific container
docker rm <container-id>
# Force-remove all containers (running and stopped)
docker rm -f $(docker ps -aq)
# Force-remove all local images at once
docker rmi -f $(docker images -q)
10. Summary and Key Takeaways
- DockerHub Integration: The
docker pullcommand automatically checks local storage first before downloading images from DockerHub. - Tags & Versions: Always specify explicit version tags (e.g.,
centos:7) when dealing with deprecated or version-sensitive base images. - Detached vs. Interactive Modes: Use
-dfor background services (like Nginx/MySQL) and-itfor interactive base OS shells (like Ubuntu/Fedora). - Port Mapping (
-p): Connect host machine ports to container ports (e.g.,-p 80:80) to expose internal server apps to external browsers. - Deep Inspection:
docker inspect(for both images and containers) provides granular JSON metadata detailing environment variables, ports, layers, and entry points. - Log Monitoring: Use
docker logs <container-name>to debug container startup failures and runtime exceptions. - Process Auditing: Use
docker top <container-name>to inspect active operating system processes running inside an isolated container. - File Transfers (
docker cp): Bidirectional file copying allows seamless data exchange between host directories and container file systems. - Bulk Cleanup: Combine command substitution with force flags (
docker rm -f $(docker ps -aq)) to instantly wipe stale containers and images.