系统要求:
安装步骤:
系统要求:
安装步骤:
卸载旧版本(如有):
sudo apt-get remove docker docker-engine docker.io containerd runc
设置仓库:
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
安装Docker引擎:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
验证安装:
sudo docker run hello-world
错误信息:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
解决方案:
# 启动Docker服务
sudo systemctl start docker
# 设置开机自启
sudo systemctl enable docker
# 检查Docker状态
sudo systemctl status docker
错误信息:
Got permission denied while trying to connect to the Docker daemon socket...
解决方案:
# 将当前用户加入docker组
sudo usermod -aG docker $USER
# 重新登录使更改生效
newgrp docker
错误信息:
no space left on device
解决方案:
# 清理未使用的容器、网络、镜像和构建缓存
docker system prune
# 清理所有未使用的镜像(包括悬空镜像)
docker system prune -a
# 查看Docker磁盘使用情况
docker system df
错误信息:
Bind for 0.0.0.0:8080 failed: port is already allocated
解决方案:
# 查找占用端口的进程
sudo lsof -i :8080
# 停止占用端口的容器
docker stop $(docker ps -q --filter ancestor=your_image_name)
# 或者强制停止所有容器
docker stop $(docker ps -aq)
解决方案:
1. 确保已启用Hyper-V:
- 以管理员身份打开PowerShell
powershell
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
2. 确保已启用WSL 2(Windows 10 2004或更高版本)
3. 重启计算机
4. 尝试重置Docker Desktop到出厂设置
错误信息:
Error response from daemon: conflict: unable to delete image (cannot be forced) - image is being used by running container
解决方案:
# 先停止使用该镜像的容器
docker stop container_id
# 然后删除容器
docker rm container_id
# 最后删除镜像
docker rmi image_id
错误信息:
toomanyrequests: You have reached your pull rate limit. You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limit
解决方案:
1. 登录Docker Hub账户:
bash
docker login
2. 考虑使用镜像加速器(如阿里云、腾讯云等提供的镜像服务)
解决方案: - 检查Dockerfile语法是否正确 - 确保构建上下文包含所有必要文件 - 增加构建超时时间(如果有超时问题) - 检查网络连接是否正常
配置Docker镜像加速器(中国用户推荐):
{
"registry-mirrors": [
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
编辑或创建/etc/docker/daemon.json
文件添加上述内容,然后重启Docker服务。
限制容器资源使用:
docker run -it --cpus="1.5" --memory="1g" your_image
使用.dockerignore文件减少构建上下文大小
多阶段构建减少最终镜像大小
通过以上步骤和解决方案,您应该能够顺利安装Docker并解决大多数常见问题。如遇特殊问题,建议查阅Docker官方文档或社区支持。