Troubleshooting Common Docker Issues
Even with a properly installed Docker environment, you may encounter issues during regular usage. Let's explore some common Docker problems and their solutions.
Issue: Docker Daemon Not Running
You can skip this step if you have already started the Docker service in the previous step.
If you try to run a Docker command and get an error like this:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
This means the Docker daemon is not running. To resolve this:
- Check the status of the Docker service:
sudo systemctl status docker
- If it's not running, start it:
sudo systemctl start docker
- If the service fails to start, check the logs for errors:
sudo journalctl -u docker
Let's simulate this issue and its resolution:
## First, stop the Docker service to simulate the issue
sudo systemctl stop docker
## Try to run a Docker command
docker ps
## You'll see the "Cannot connect" error
## Now restart the service to fix it
sudo systemctl start docker
## Verify Docker is working again
docker ps
Issue: Permission Denied
If you see an error like:
Got permission denied while trying to connect to the Docker daemon socket
This usually means your user doesn't have permission to access the Docker socket. The solution is to add your user to the docker group:
sudo usermod -aG docker $USER
After running this command, you would normally need to log out and log back in for the changes to take effect. Since we're in a lab environment with the labex
user that already has proper permissions, we don't need to perform this step.
Issue: Disk Space Problems
Docker can consume significant disk space over time with unused images, containers, and volumes. If your system is running low on disk space:
- Check Docker disk usage:
docker system df
- Remove unused resources:
## Remove all stopped containers
docker container prune
## Remove all unused images
docker image prune
## Remove all unused volumes
docker volume prune
## Or remove everything unused in one command
docker system prune
Let's demonstrate the pruning command:
## Create some containers that will exit immediately
docker run hello-world
docker run ubuntu echo "This will exit immediately"
## Now prune stopped containers
docker container prune
You'll be asked to confirm the operation:
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Type y
to confirm. You should see output showing the removed containers.
Issue: Container Not Starting
If a container fails to start, you can investigate by checking its logs:
## First, try to start a container that might fail
docker run --name failing-container ubuntu apt-get update
## Check the logs
docker logs failing-container
You might see errors in the logs that indicate why the container failed.
Issue: Network Problems
If containers can't communicate with each other or with the outside world:
- Check Docker's network configuration:
docker network ls
- Inspect a specific network:
docker network inspect bridge
- Test connectivity from within a container:
## Start a container with networking
docker run -it ubuntu bash
## From inside the container, install ping
apt-get update && apt-get install -y iputils-ping
## Try pinging a website
ping google.com
## Exit the container
exit
Docker Logs and Debugging
For general Docker troubleshooting, checking Docker daemon logs can be helpful:
sudo journalctl -u docker
For a specific container's logs:
docker logs <container_id>
You can also get a real-time stream of logs:
docker logs -f <container_id>
These troubleshooting techniques will help you diagnose and resolve most common Docker issues.