Troubleshooting Common Docker Issues
Welcome to Lesson 18! At this stage, you’re building, running, and orchestrating Docker containers with confidence. However, even seasoned professionals encounter issues—containers that won't start, mysterious network problems, or unexpected behavior in production. Understanding how to systematically troubleshoot Docker issues is an essential skill for every developer and DevOps engineer.
In this lesson, we'll cover effective troubleshooting techniques for common Docker problems, provide hands-on examples, and share best practices to help you debug and resolve issues quickly.
Table of Contents
- General Troubleshooting Approach
- Container Startup and Crash Issues
- Image Build Problems
- Networking and Connectivity Issues
- Volume and Data Persistence Issues
- Resource Constraints and Performance Problems
- Common Mistakes and Pitfalls
- Summary
- Quiz
General Troubleshooting Approach
Before diving into specific issues, adopt a systematic approach:
- Gather Information: Use logs, status commands, and config files.
- Isolate the Problem: Is it the image, container, network, or host?
- Reproduce the Issue: Does it happen every time? Only on one host?
- Check the Docker Documentation: It often lists common errors and fixes.
Useful commands:
docker ps -a # List all containers, including stopped ones
docker logs <container> # View a container’s logs
docker inspect <container> # Detailed info about a container
docker events # Real-time event stream
Container Startup and Crash Issues
Problem: Container Exits Immediately
Symptoms: docker ps -a shows your container exited right after starting.
Common Causes:
- The main process in the container ends (e.g.,
CMDorENTRYPOINTruns and exits). - Application crashes due to missing dependencies or misconfiguration.
How to Debug:
-
Check the logs:
docker logs <container>Look for error messages or stack traces.
-
Run the container interactively:
docker run -it --entrypoint /bin/sh <image>This lets you access the shell and investigate.
Example:
docker run myapp
# App exits immediately
docker logs <container_id>
# Output: "Error: Configuration file not found."
Solution: Ensure required files are present, and the CMD or ENTRYPOINT is correct.
Problem: Container Fails with "Permission Denied"
Symptoms: Logs show permission errors.
How to Debug:
- Inspect file permissions in the image.
- If using volumes, check host directory permissions.
Example:
docker run -v /host/data:/data myapp
# App fails to write to /data
ls -ld /host/data
# drwxr-xr-x 2 root root 4096 Mar 1 12:00 /host/data
Solution: Set the correct permissions or ownership:
sudo chown 1000:1000 /host/data
(Where 1000 is the UID/GID your container expects.)
Image Build Problems
Problem: "COPY failed: no source files were specified"
Symptoms: Build fails when copying files.
How to Debug:
- Check the
Dockerfilepath. - Make sure files exist in the build context.
Example:
COPY app.py /app/
If app.py does not exist in the context directory, you’ll see this error.
Solution: Ensure all files/directories referenced in COPY or ADD exist in the build context.
Problem: Build Caching Issues
Symptoms: Old code appears in the image after changes.
How to Debug:
- Docker caches layers aggressively.
- If you change files after a cached layer, your changes may not be included.
Solution: Use .dockerignore to exclude unnecessary files. Rebuild without cache:
docker build --no-cache -t myapp:latest .
Networking and Connectivity Issues
Problem: Container Can't Connect to Other Containers
Symptoms: Service in one container can’t reach another (e.g., web app can't reach database).
How to Debug:
-
Check network configuration:
docker network ls
docker network inspect <network> -
Connect containers to the same network:
docker run --network mynet ... -
Use service names as hostnames in Docker Compose:
services:
web:
...
db:
...In this setup,
webcan reachdbvia hostnamedb.
Pitfall: Using localhost from one container only refers to itself, not to other containers.
Problem: Ports Not Exposed or Mapped
Symptoms: Can't access your service from the host machine.
How to Debug:
- Ensure you have
EXPOSEd the port in your Dockerfile (for documentation). - Ensure you use
-por--publishwhen running the container:This maps port 80 in the container to port 8080 on the host.docker run -p 8080:80 myapp
Volume and Data Persistence Issues
Problem: Data Not Persisting Between Container Restarts
Symptoms: Data appears to be lost after restarting or removing a container.
How to Debug:
- Are you using named volumes or bind mounts?
- Check your
docker runordocker-compose.ymlconfiguration.
Example:
docker run -v mydata:/data myapp
Data in /data will persist as long as the mydata volume exists.
Pitfall: Data stored only in a container’s writable layer is lost when the container is removed.
Problem: File Permission Problems with Volumes
Symptoms: App can't read/write files from a mounted volume.
How to Debug:
- Check user IDs inside the container vs. ownership of files on the host.
Solution: Match UID/GID between host and container, or adjust permissions.
Resource Constraints and Performance Problems
Problem: Out of Memory (OOM) Kills
Symptoms: Container stops unexpectedly; docker logs may show OOM errors.
How to Debug:
- Check system logs (
dmesg | grep -i oom). - Inspect container resource usage.
Solution: Limit memory usage:
docker run -m 512m myapp
Or optimize your application.
Problem: High CPU or Disk Usage
Symptoms: Host slows down, containers are slow or unresponsive.
How to Debug:
- Use
docker statsto monitor resource usage. - Use monitoring tools (as covered in Lesson 15).
Solution: Optimize your application, increase resources, or scale out with more containers.
Common Mistakes and Pitfalls
- Forgetting to Map Ports: Not using
-p/--publishmeans your service is inaccessible from outside the container. - Using
localhostfor Inter-Container Communication: Containers need to use service names or container IPs, notlocalhost. - Misunderstanding Volume Persistence: Data in containers (not in volumes) is ephemeral.
- Missing Files in Build Context: Files outside the build context can't be copied into images.
- Mismatched User IDs: Causes file permission errors with mounted volumes.
- Overlapping Container Names: Causes conflicts; use unique names or Docker Compose.
Summary
Troubleshooting Docker issues requires a structured approach: gather information, isolate the problem, reproduce it, and apply targeted fixes. By mastering common troubleshooting commands and understanding the typical pitfalls, you’ll be able to resolve most Docker-related issues efficiently.
Key takeaways:
- Use logs and inspect/container status commands.
- Understand how Docker handles networking, volumes, and process management.
- Always verify your
Dockerfilepaths, volume mounts, and port mappings. - Resource constraints can silently kill or slow your containers.
- Practice debugging by running containers interactively.
Quiz
-
What command would you use to view the logs of a Docker container named
webapp?a)
docker ps webapp
b)docker logs webapp
c)docker inspect logs webapp
d)docker info webapp -
If two containers need to communicate, what is the best way to allow them to see each other?
a) Use
localhostas the hostname
b) Connect both to the same user-defined bridge network
c) Use the container's IP address on the host's network
d) Use the default bridge andlocalhost -
You run
docker run -p 8080:80 myapp, but cannot access the service on http://localhost:8080. What is a possible cause?a) The app listens on port 80 inside the container
b) The app is not running
c) You should have used-vinstead of-p
d) The app is listening on a different port inside the container -
What is a common cause of "permission denied" errors when using Docker volumes?
a) Typo in the Dockerfile
b) Host directory permissions do not match container user IDs
c) Using-dfor detached mode
d) Not exposing ports -
Which command disables Docker’s build cache for a build?
a)
docker build --no-cache -t myapp .
b)docker build --clear-cache -t myapp .
c)docker build --force -t myapp .
d)docker build --reset -t myapp .
Answers
- b)
docker logs webapp - b) Connect both to the same user-defined bridge network
- d) The app is listening on a different port inside the container
- b) Host directory permissions do not match container user IDs
- a)
docker build --no-cache -t myapp .
Continue practicing these troubleshooting techniques as you work with Docker in real projects!