Essential Docker Commands and Troubleshooting Techniques
Container Management Commadns
Container Lifecycle Operations
docker start: Launch one or more stopped containers.
docker stop: Terminate a running container gracefully.
docker restart: Reinitialize a container.
Container Removal and Execution
docker kill: Forcefully terminate a running container. Options:
- -s: Specify signal to send (default: KILL)
Example: docker kill webapp (by name) or docker kill a1b2c3d4 (by ID)
docker rm: Remove containers. Use -f for forced removal:
docker rm -f container1 container2
docker exec: Execute commands within running containers. Key flags:
- -d: Run in detached mode
- -i: Keep STDIN open
- -t: Allocate pseudo-TTY
Example: docker exec -it myapp /scripts/start.sh
Container State Control
docker pause: Suspend all processes in a container.
docker unpause: Resume paused container processes.
Example: docker pause database followed by docker unpause database
Container Inspecsion and Monitoring
Listing Containers
docker ps: Display container information. Common options:
- -a: Show all containers (including stopped)
- -n: Show last N containers
- -q: Queit mode (container IDs only)
- -f: Filter output
Examples:
docker ps
docker ps -n 3
docker ps -a -q
Container states include: created, restarting, running, removing, paused, exited, dead.
Container Metadata and Logs
docker inspect: Retrieve detailed container or image metadata.
Example: docker inspect redis:latest
docker logs: Access container logs. Useful flags:
- -f: Follow log output
- -t: Show timestamps
- --tail: Show last N lines
Example: docker logs -f --tail=50 appserver
Image Management Commands
Local Image Operations
docker images: List local images. Options:
- -a: Show all images (including intermediates)
- -q: Show only image IDs
- --no-trunc: Display full information
Example: docker images --filter=reference='nginx:*'
docker rmi: Remove local images. Use -f for forced deletion:
docker rmi -f oldimage:v1
docker tag: Tag local images for repository organization:
docker tag app:1.0 registry.example.com/app:prod
Image Building and Distribution
docker build: Create images from Dockerfile. Essential parameters:
- -t: Set image name and tag
- --no-cache: Build without cache
- --rm: Remove intermediate containers
Example: docker build -t myapp:latest .
docker save: Export image to tar archive:
docker save -o backup.tar myimage:version
docker load: Import image from tar file:
docker load -i backup.tar
Troubleshooting Techniques
Preventing Container Exit
To maintain a problematic container for investigation:
docker run -d --name=debug-container \
--entrypoint=/bin/bash \
image:tag \
-c "while true; do echo 'Container active'; sleep 30; done"
This keeps the container running with periodic activity, allowing exec access for diagnostics.
Practical Debugging Example
For a malfunctioning application container:
# Start container with maintained execution
docker run -d --name=temp-debug -p 8080:8080 \
app-image:broken \
-c "while true; do /app/start.sh; sleep 15; done"
# Access container shell for investigation
docker exec -it temp-debug /bin/bash
# Examine logs and processes
docker logs temp-debug
docker exec temp-debug ps aux