Docker Series Part 5: Debugging, Logs, and Best Practices
Table of Contents
Docker Series Part 5: Debugging, Logs, and Best Practices
Now that you have a solid workflow with Docker Compose, volumes, and environment variables, let’s look at how to debug your containers, view logs, and follow some best practices for smoother development.
Viewing Logs
To see the output from your containers, use:
docker compose logs
Or for a specific service:
docker compose logs web
Add -f to follow logs in real time:
docker compose logs -f
Debugging Containers
Sometimes you need to get inside a running container to inspect files or run commands. Use:
docker compose exec web sh
This opens a shell inside the web service container.
Common Issues & Solutions
- Port conflicts: Make sure the ports you expose aren’t already in use on your host.
- File permissions: When using volumes, permissions may differ between host and container. Adjust with
chownor DockerfileUSERinstructions if needed. - Dependency issues: Always rebuild your image (
docker compose build) if you change dependencies.
Best Practices
- Use
.dockerignoreto avoid copying unnecessary files into your image. - Keep images small by using slim or alpine base images.
- Pin dependency versions in
requirements.txtfor reproducible builds. - Use environment variables for configuration, not hardcoded values.
- Clean up unused containers and images regularly with:
docker system prune
Recap
- Use
docker compose logsandexecfor troubleshooting. - Follow best practices for efficient, maintainable Docker projects.
This wraps up the basics of Docker for development! Explore more advanced topics like CI/CD integration, multi-stage builds, and security as you grow.