Containerization technologies like Docker enable efficient deployment, portability, and scalability. This guide provides a structured approach to debugging and patching Docker containers, emphasizing technical precision and operational clarity.
Terminologies
- Docker: A platform for containerizing applications using images.
- Image: A lightweight, standalone self-contained executable that contains everything needed to run a application.
- Container: A running instance of an image, isolated from other containers.
Information Gathering
Container Inspection
- List running containers:
docker ps
- List all containers (including exited):
docker ps -a
- Filter by status (e.g., exited):
docker ps -a -f "status=exited"
- Retrieve container logs:
docker logs <container_name>
- Inspect container details:
docker container inspect <container_name>
Image Inspection
- List all images:
docker images
- Inspect image details:
docker image inspect <image_name>:<image_tag>
Output Formatting
- Extract exited container names:
docker ps -a -f "status=exited" --format "{{json .Names}}"
Live Debugging
Docker is Running
- Enter container shell:
docker exec -it <container_name> /bin/sh
- List container contents:
docker exec -t <container_name> ls ./
Docker Has Exited
- Retrieve image and tag:
docker ps -f "name=<container_name>" --format "{{json .Image}}"
- Run container with shell:
docker run -it --entrypoint sh <image_name>:<image_tag>
- Execute command:
docker run -it --entrypoint ls <image_name>:<image_tag> ./
Post Debugging
Rollback
- Revert to previous tag:
docker tag <image_name>:<previous_tag> <image_name>:latest
Patching
- Commit live-debugging container:
docker container commit <container_name> <image_name>:<patched_tag>
- Kill container:
docker container kill <container_name>
- Tag patched version:
docker tag <image_name>:<patched_tag> <image_name>:latest
- Restart service:
docker restart <service_name>
Docker with Changed Entry Point
- Retrieve old entry point:
docker image inspect <image_name>:<tag> --format "Entrypoint {{json .Config.Entrypoint}}"
- Retrieve old command:
docker image inspect <image_name>:<tag> --format "CMD {{json .Config.Cmd}}"
- Commit with old configuration:
docker container commit -change "<old_entrypoint>" -change "<old_cmd>" <container_name> <image_name>:<patched_tag>
- Kill container:
docker container kill <container_name>
- Tag patched version:
docker tag <image_name>:<patched_tag> <image_name>:latest
- Restart service:
docker restart <service_name>
Cleaning Up
- Remove exited containers:
docker ps -a -f "status=exited" --format "{{json .Names}}" | xargs -r docker rm
- Prune unused images:
docker images prune
Tips
- Use
--q
for quiet mode:docker ps --q
- Transfer files between container and host:
docker cp <container_name>:<path> <local_path>
- From container:
docker cp <container_name>:<path> <local_path>
- To container:
docker cp <local_path> <container_name>:<path>
- From container:
Conclusion
Effective Docker debugging and patching requires systematic analysis, precise command execution, and careful cleanup. This guide provides a structured approach to identify issues, resolve them, and maintain containerized environments with reliability and efficiency.