Azure VM自定义数据执行Docker安装脚本失败问题排查
在Azure Debian Bullseye 11虚拟机上通过Custom Data执行Docker安装脚本失败的问题
我在Azure的Debian Bullseye 11虚拟机上使用以下脚本安装Docker:
echo "** Updating package lists..." sudo apt update echo "** Installing dependencies..." sudo apt install -y apt-transport-https ca-certificates curl gnupg software-properties-common echo "** Adding Docker GPG key..." curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - echo "** Adding Docker repository..." sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian bullseye stable" echo "** Updating package lists again..." sudo apt update echo "** Installing Docker..." sudo apt install -y docker-ce echo "** Docker installation complete!"
手动逐行执行脚本时安装正常,但将其作为Custom Data用于VM初始化时出现报错,以下是/var/log/cloud-init-output.log中的错误信息:
** Updating package lists... WARNING: apt does not have a stable CLI interface. Use with caution in scripts. Reading package lists... E: Could not get lock /var/lib/apt/lists/lock. It is held by process 700 (apt-get) E: Unable to lock directory /var/lib/apt/lists/ ** Installing dependencies... WARNING: apt does not have a stable CLI interface. Use with caution in scripts. Reading package lists... Building dependency tree... Reading state information... Package gnupg is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source E: Package 'gnupg' has no installation candidate E: Unable to locate package software-properties-common ** Adding Docker GPG key... E: gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this operation (23) Failed writing body ** Adding Docker repository... sudo: add-apt-repository: command not found ** Updating package lists again... WARNING: apt does not have a stable CLI interface. Use with caution in scripts. Reading package lists... E: Could not get lock /var/lib/apt/lists/lock. It is held by process 700 (apt-get) E: Unable to lock directory /var/lib/apt/lists/ ** Installing Docker... WARNING: apt does not have a stable CLI interface. Use with caution in scripts. Reading package lists... Building dependency tree... Reading state information... E: Unable to locate package docker-ce ** Docker installation complete!
问题原因及修复方案
1. APT锁冲突问题
VM初始化过程中,系统后台会自动运行apt进程(比如默认更新配置),导致脚本中的apt命令无法获取锁,出现Could not get lock错误。
修复:在执行apt命令前添加等待逻辑,直到锁释放:
# 等待APT锁释放 while sudo fuser /var/lib/apt/lists/lock >/dev/null 2>&1; do echo "Waiting for APT lock to be released..." sleep 5 done
2. 依赖包找不到问题
Debian Bullseye中gnupg是虚拟包,实际对应gnupg2;同时software-properties-common需要确保main仓库已启用,且仓库列表更新完成后再执行安装。
修复:修改依赖安装命令,明确安装gnupg2,并确保仓库更新完成:
sudo apt install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common
3. add-apt-repository命令缺失
该命令属于software-properties-common包,解决依赖安装问题后,此错误会自动消失。另外,推荐使用更安全的仓库配置方式替代add-apt-repository:
# 添加Docker GPG密钥到系统密钥环 curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg # 写入Docker仓库配置 echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian bullseye stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
(注:apt-key在Debian Bullseye中已标记为过时,上述方式是官方推荐的新配置方法)
优化后的完整脚本
整合所有修复后的脚本如下,可直接作为Custom Data使用:
#!/bin/bash echo "** Waiting for APT lock release..." while sudo fuser /var/lib/apt/lists/lock >/dev/null 2>&1; do echo "APT lock is held, waiting 5 seconds..." sleep 5 done echo "** Updating package lists..." sudo apt update -y echo "** Installing dependencies..." sudo apt install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common echo "** Adding Docker GPG key..." curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg echo "** Adding Docker repository..." echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian bullseye stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null echo "** Updating package lists again..." while sudo fuser /var/lib/apt/lists/lock >/dev/null 2>&1; do echo "APT lock is held, waiting 5 seconds..." sleep 5 done sudo apt update -y echo "** Installing Docker..." sudo apt install -y docker-ce docker-ce-cli containerd.io echo "** Docker installation complete!"
内容的提问来源于stack exchange,提问作者buga
相关产品推荐
相关产品推荐

