I use this script to install docker on my Azure VM, OS Debian Bullseye 11:
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!"
If I run this script manually (1 line at a time) it works, however when I use it as Custom Data so it would execute on VM initialization, it gives errors, here’s the var/logs/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!
2
Answers
The issue seems to be with installing
gnupg
. This is most likely because the repositories which the APT package sources burned in to image point to don’t have gnupg1. You should try:In your script line
sudo apt install -y apt-transport-https ca-certificates curl gnupg software-properties-common
, change gnupg to gnupg2.Add the bullseye main repository to APT sources. Add the following lines to the top of your script:
Step 2 should only be requires if step 1 doesn’t work on its own.
It appears that another instance of
apt
is running:Try adding this at the top of your script to wait for the other
apt
to finish: