Debug School

rakesh kumar
rakesh kumar

Posted on

How to debug in docker

Debugging in Docker can be crucial when troubleshooting issues with your containers or applications. Here's a checklist of different ways to debug in Docker, along with examples:

View Container Logs:

Use the docker logs command to view container logs.

docker logs <container_id>
Enter fullscreen mode Exit fullscreen mode

Attach to a Running Container:

Use the docker exec command to attach to a running container and execute commands inside it.

docker exec -it <container_id> /bin/bash
Enter fullscreen mode Exit fullscreen mode

Inspect Container Details:

Use the docker inspect command to get detailed information about a container.

docker inspect <container_id>
Enter fullscreen mode Exit fullscreen mode

Interactive Mode:

Run a container in interactive mode to see real-time output.

docker run -it <image_name> /bin/bash
Enter fullscreen mode Exit fullscreen mode

Docker Compose Debugging:

Use docker-compose to manage multi-container applications.

docker-compose logs
Remote Debugging:

Enable remote debugging inside your application and expose the required ports.
Attach a debugger from your IDE or use tools like gdb.

docker run -p 5005:5005 -it <image_name> java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -jar app.jar
Enter fullscreen mode Exit fullscreen mode

Volume Inspection:

Check if volumes are mounting correctly.

docker inspect -f '{{ .Mounts }}' <container_id>
Enter fullscreen mode Exit fullscreen mode

Health Checks:

Ensure that your Dockerfile includes health checks to monitor container health.

HEALTHCHECK CMD curl --fail http://localhost:8080/ || exit 1
Enter fullscreen mode Exit fullscreen mode

Environment Variables:

Check if environment variables are set correctly.

docker exec <container_id> printenv
Enter fullscreen mode Exit fullscreen mode

Networking:

Verify container network settings.

docker network inspect <network_name>
Enter fullscreen mode Exit fullscreen mode

Container Resource Usage:

Monitor container resource usage.

docker stats <container_id>
Enter fullscreen mode Exit fullscreen mode

Docker Events:

Check Docker events for any issues.

Dockerfile Debugging:

Use RUN commands in your Dockerfile for debugging.

RUN echo "Debugging step: XYZ"
Check Container Health Status:

Inspect the health status of a container.

docker inspect --format='{{json .State.Health}}' <container_id>
Enter fullscreen mode Exit fullscreen mode

Container Inspect for IP Address:

Obtain the IP address of a running container.

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_id>
Enter fullscreen mode Exit fullscreen mode

Remember to adapt these examples to your specific use case, and use a combination of these debugging techniques based on the nature of the problem you are trying to solve.

Practical Example

Image description

Solution

Image description

Image description

Image description

Top comments (0)