最近在學習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