最近在学习docker,安装和使用折腾了好久,在这里记录一下。
下载
# 依赖安装
sudo apt update
sudo apt install -y \ca-certificates \curl \gnupg \lsb-release# 使用清华镜像源(Ubuntu 24.04 noble)
echo \"deb [arch=$(dpkg --print-architecture)] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \noble stable" | sudo tee /etc/apt/sources.list.d/docker.list# 更新软件包列表
sudo apt update# 安装 Docker
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin# 这一步一般会提示失败,少密钥# 方法1:用清华镜像的密钥
curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg# 方法2:如果还不行,用这个
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7EA0A9C3F273FCD8
使用
# 启动 Docker
sudo systemctl start docker# 运行测试容器
sudo docker run hello-world
我在这里遇到了问题如下:
sudo docker run hello-worldUnable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": context deadline exceededRun 'docker run --help' for more information
查完资料有这么几种方案
1. 换国内源
2. 直接下载镜像文件(需要是tar.gz格式)
2种都失败,进一步验证是网络问题,解决后通过
# 测试是否能访问 Docker Hub
curl -v https://registry-1.docker.io/v2/# 如果连不上,检查代理或 DNS
ping registry-1.docker.io# 但是访问用梯子访问是正常的
# 给Docker设置代理
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/proxy.conf <<-'EOF'
[Service]
Environment="HTTP_PROXY=http://你的代理IP:端口"
Environment="HTTPS_PROXY=http://你的代理IP:端口"
EOFsudo systemctl daemon-reload
sudo systemctl restart docker# 测试连接
curl -4v https://registry-1.docker.io/v2/# 重新拉取镜像
sudo docker run --network=host hello-world