Docker FAQ

This page lists common issues and solutions for Docker installation and usage.

Installation Issues

Docker Desktop Fails to Start

Symptoms: Docker Desktop won't start, or crashes immediately after starting

Solutions:

  1. Ensure system meets hardware requirements:

    • Windows: Enable Hyper-V or install WSL 2
    • Mac: Ensure not using outdated Mac models
  2. Reset Docker Desktop:

    • Windows: Right-click Docker icon → Troubleshoot → Reset to factory defaults
    • Mac: Docker → Troubleshoot → Reset to factory defaults
  3. Reinstall Docker Desktop

WSL2 Installation Fails

Symptoms: On Windows, prompt says WSL2 not properly installed

Solutions:

  1. Enable Windows features:

    • Open "Windows Features"
    • Enable "Virtual Machine Platform" and "Windows Subsystem for Linux"
  2. Install WSL2 kernel update package:

    • Download and install WSL2 Linux kernel update package
  3. Set WSL2 as default version:

    wsl --set-default-version 2
    

Permission Denied on Linux

Symptoms: "permission denied" when running docker commands

Solutions:

  1. Add current user to docker group:

    sudo usermod -aG docker $USER
    
  2. Log out and log back in or restart system

  3. Verify:

    docker ps
    

Virtual Machine Issues

Symptoms: Poor performance or unable to run Docker in virtual machines

Solutions:

  1. Ensure virtualization is enabled (in BIOS)
  2. Allocate sufficient CPU and memory to the virtual machine
  3. Use Docker Desktop instead of Docker Engine

Image Issues

Pulling Images Fails

Symptoms: docker pull command times out or fails

Solutions:

  1. Check network connection

  2. Use domestic mirror sources (configure Docker registry mirrors):

    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<EOF
    {
      "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
    }
    EOF
    sudo systemctl daemon-reload
    sudo systemctl restart docker
    
  3. Check if firewall or proxy is blocking connection

Image Too Large

Solutions:

  1. Use multi-stage builds to reduce image size
  2. Choose smaller base images (like alpine versions)
  3. Clean build cache:
    docker builder prune
    

Clean Up Unused Images

# Delete dangling images (untagged)
docker image prune

# Delete all unused images
docker image prune -a

# Delete build cache
docker builder prune

Container Issues

Container Won't Start

Symptoms: Container stops immediately after creation

Solutions:

  1. View container logs:

    docker logs <container name>
    
  2. Check if CMD or ENTRYPOINT in Dockerfile is correct

  3. Ensure there's a foreground process running:

    docker run -d nginx tail -f /dev/null
    

Port Conflicts

Symptoms: Error "port is already allocated" when starting container

Solutions:

  1. Find process using the port:

    # Linux
    sudo lsof -i :8080
    
    # Windows
    netstat -ano | findstr :8080
    
  2. Stop the process using the port, or use a different port

  3. Modify container port mapping:

    docker run -d -p 8081:80 nginx
    

Containers Can't Communicate

Solutions:

  1. Ensure containers are on the same network:

    docker network ls
    docker network inspect <network name>
    
  2. Use service names for communication (in Docker Compose or same network)

  3. Check container firewall settings

Data Loss

Symptoms: Data lost after container deletion

Solutions:

  1. Use volumes to persist data:

    docker run -d -v mydata:/app/data myimage
    
  2. Regularly backup volumes:

    docker run --rm -v mydata:/data -v $(pwd):/backup alpine tar czf /backup/backup.tar.gz /data
    

Container Encoding Issues

Solutions:

Set locale in Dockerfile:

ENV LANG=C.UTF-8 LC_ALL=C.UTF-8

Or use base images with Chinese support.

Network Issues

Cannot Access External Network

Solutions:

  1. Check Docker network configuration:

    docker network inspect bridge
    
  2. Check DNS settings:

    docker run --dns 8.8.8.8 alpine ping -c 3 google.com
    
  3. Check firewall or proxy settings

Proxy Issues

If you need proxy to access external networks, configure proxy:

# Create Docker config directory
sudo mkdir -p /etc/systemd/system/docker.service.d

# Create proxy config file
sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf <<EOF
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80"
Environment="HTTPS_PROXY=http://proxy.example.com:443"
Environment="NO_PROXY=localhost,127.0.0.1"
EOF

# Restart Docker
sudo systemctl daemon-reload
sudo systemctl restart docker

Performance Issues

Docker Running Slowly

Solutions:

  1. Allocate more resources to Docker Desktop:

    • Docker Desktop → Settings → Resources
  2. Move Docker images to larger disk space

  3. Clean up unused containers, images, and volumes:

    docker system prune -a
    

Insufficient Disk Space

Solutions:

  1. Check disk usage:

    docker system df
    
  2. Clean build cache:

    docker builder prune
    
  3. Clean unused volumes:

    docker volume prune
    
  4. Move Docker data directory to another disk (requires reconfiguration)

Docker Compose Issues

Services Won't Start

Solutions:

  1. View detailed logs:

    docker-compose logs -f <service name>
    
  2. Check docker-compose.yml syntax

  3. Ensure all dependent services are started

Environment Variables Not Taking Effect

Solutions:

  1. Properly set environment in docker-compose.yml:

    environment:
      - NODE_ENV=production
    # or
    environment:
      NODE_ENV: production
    
  2. Check if .env file exists

Port Conflicts

Same as single containers, docker-compose may also encounter port conflicts. Modify port mapping in docker-compose.yml.

Uninstallation Issues

Completely Uninstall Docker

Ubuntu/Debian:

# Stop Docker service
sudo systemctl stop docker

# Uninstall Docker packages
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# Remove data and config
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

# Remove user group
sudo groupdel docker

# Remove config files
sudo rm -rf /etc/docker

macOS:

# Stop Docker
osascript -e 'quit app "Docker"'

# Remove application
rm -rf /Applications/Docker

# Remove config and data
rm -rf ~/Library/Containers/com.docker.docker
rm -rf ~/Library/Application\ Support/Docker
rm -rf ~/Library/Group\ Containers/group.com.docker

Windows:

Uninstall Docker Desktop through Settings → Apps, or use Control Panel.

Other Issues

How to Check Docker Version

docker --version
docker-compose --version
docker-machine --version

How to Get Help

Docker Command Not Found

Make sure Docker is properly installed and added to PATH. Check:

which docker
echo $PATH

If not in PATH, add to shell config file.

Containers Auto Restart

Use --restart option:

docker run -d --restart=always nginx

Restart policies include:

  • no: No auto restart
  • on-failure: Restart on failure
  • always: Always restart
  • unless-stopped: Always restart unless manually stopped

Limit Container Resources

# Limit memory
docker run -d --memory="512m" nginx

# Limit CPU
docker run -d --cpus="1.5" nginx

# Limit memory and CPU
docker run -d --memory="512m" --cpus="1.5" nginx