Introduction
Docker has revolutionized how we deploy applications, but even experienced DevOps engineers encounter issues. This comprehensive guide covers the most common Docker errors and provides practical solutions to keep your containerized applications running smoothly.
1. Docker Daemon Connection Issues
Error:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
docker: Error response from daemon: Bad response from Docker engine
Solutions:
- Check Docker service status:
sudo systemctl status docker
- Start Docker service:
sudo systemctl start docker
sudo systemctl enable docker
- Fix permissions:
sudo chmod 666 /var/run/docker.sock
sudo usermod -aG docker $USER
- Restart Docker daemon:
sudo systemctl daemon-reload
sudo systemctl restart docker
2. Image Registry Authentication Problems
Error:
Error response from daemon: Head "https://registry.example.com/v2/myapp/manifests/latest": unauthorized: authentication required
denied: requested access to the resource is denied
Solutions:
- Check registry credentials:
docker login registry.example.com
- Verify configuration:
cat ~/.docker/config.json
- Use credential helper:
docker-credential-helper list
3. Container Networking Issues
Error:
ERROR: for frontend Cannot start service frontend: driver failed programming external connectivity
failed to create endpoint networking_frontend on network bridge: Error starting userland proxy
Solutions:
- Check network status:
docker network ls
docker network inspect bridge
- Reset Docker networking:
sudo systemctl restart docker
sudo iptables -F
sudo iptables -t nat -F
- Create custom network:
docker network create --driver bridge my-network
docker run --network=my-network myapp
4. Storage and Resource Constraints
Error:
failed to register layer: Error processing tar file(exit status 1):
no space left on device
container_linux.go:235: starting container process caused "process_linux.go:258:
applying cgroup configuration for process caused \"failed to write ..."
Solutions:
- Clean up resources:
# Remove unused containers
docker container prune
# Remove unused images
docker image prune -a
# Remove all unused objects
docker system prune -a --volumes
- Monitor disk usage:
docker system df
df -h /var/lib/docker
- Configure storage driver:
# Edit daemon.json
{
"storage-driver": "overlay2",
"storage-opts": ["overlay2.size=20G"]
}
5. Build Context and Dockerfile Issues
Error:
error from sender: failed to resolve symlinks in context directory: lstat /var/lib/docker/tmp/docker-builder123456789/src: no such file or directory
COPY failed: file not found in build context or excluded by .dockerignore
Solutions:
- Check build context:
# List files in build context
docker build --no-cache --progress=plain .
# Debug build context
tar -czf context.tar.gz .
tar -tvf context.tar.gz
- Fix .dockerignore:
# Example .dockerignore
node_modules
*.log
.git
!package.json
!src/
- Use multi-stage builds:
# More efficient build
FROM node:16 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
6. Container Runtime Errors
Error:
OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "app": executable file not found in $PATH
standard_init_linux.go:228: exec user process caused: no such file or directory
Solutions:
- Check container environment:
# Debug container
docker run -it --entrypoint sh myimage
# Check executable permissions
ls -la /app/entrypoint.sh
chmod +x /app/entrypoint.sh
- Fix line endings:
# Convert CRLF to LF
dos2unix entrypoint.sh
- Verify base image compatibility:
# Use specific base image
FROM ubuntu:22.04
# Install runtime dependencies
RUN apt-get update && apt-get install -y \
python3 \
python3-pip \
&& rm -rf /var/lib/apt/lists/*
Best Practices for Error Prevention
- Use Health Checks:
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost/ || exit 1
- Implement Logging:
# Configure logging driver
docker run --log-driver json-file --log-opt max-size=10m myapp
# Monitor logs
docker logs --tail 100 -f container_name
- Resource Limits:
docker run -m 512m --cpu-quota 50000 myapp
Conclusion
Understanding these common Docker errors and their solutions will help you maintain robust containerized environments. Remember to:
- Keep your Docker installation updated
- Implement monitoring and logging
- Follow security best practices
- Document your solutions for team reference
For more complex issues, consult the official Docker documentation or reach out to the Docker community.
Happy containerizing!