🚀 Fix the GPG Key Error and Install Docker on Ubuntu 22.04
Follow these steps carefully to resolve the GPG key issue and ensure a clean Docker installation. Run each command exactly as shown to avoid syntax errors.
1. Remove Any Existing Docker Repository Configuration
To avoid conflicts or malformed entries, let’s clean up any existing Docker repository files:
sudo rm -f /etc/apt/sources.list.d/docker.list
sudo rm -f /etc/apt/keyrings/docker.gpg
2. Update and Install Prerequisites
Ensure your system has the necessary tools:
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg lsb-release
- The
-y
flag automatically confirms the installation. - This step ensures
curl
andgnupg
are available for fetching and processing the GPG key.
3. Add Docker’s GPG Key Securely
Set up the keyring directory and fetch Docker’s GPG key:
sudo mkdir -p /etc/apt/keyrings
sudo chmod 755 /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
-
mkdir -p
ensures the directory is created if it doesn’t exist. -
chmod 755
sets correct directory permissions. -
chmod a+r
makes the key readable by all users, whichapt
requires.
4. Add the Docker Repository
Add the Docker repository to your APT sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- This command dynamically sets the architecture (e.g.,
amd64
,arm64
) and Ubuntu codename (e.g.,jammy
for 22.04). - The
tee
command writes the repository configuration to/etc/apt/sources.list.d/docker.list
. - Verify the file content to ensure no syntax errors:
cat /etc/apt/sources.list.d/docker.list
Expected output (example for amd64
on Ubuntu 22.04):
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu jammy stable
If the output looks malformed (e.g., extra characters, missing spaces), delete the file (sudo rm /etc/apt/sources.list.d/docker.list
) and rerun the echo
command.
5. Update APT and Install Docker
Now update the package index and install Docker:
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
- If you see the GPG error again (
NO_PUBKEY 7EA0A9C3F273FCD8
), double-check the GPG key step and permissions on/etc/apt/keyrings/docker.gpg
. - The
-y
flag skips confirmation prompts.
6. Verify Docker Installation
Test Docker to ensure it’s working:
sudo docker run hello-world
This pulls a test image and runs a container. If successful, you’ll see a message confirming Docker is working.
🙌 Optional: Run Docker Without sudo
To avoid typing sudo
for Docker commands:
sudo usermod -aG docker $USER
Log out and back in, or run:
newgrp docker
Test it:
docker run hello-world
🛠 Troubleshooting the "Token" Error
The "found character that cannot start any token" error is likely unrelated to the GPG key issue but could stem from:
-
Malformed
/etc/apt/sources.list.d/docker.list
:- If the repository file contains invalid characters (e.g., extra quotes, spaces, or tabs),
apt
might fail. - Check the file:
cat /etc/apt/sources.list.d/docker.list
- If the repository file contains invalid characters (e.g., extra quotes, spaces, or tabs),
- If it’s incorrect, remove it and recreate it using the
echo
command in step 4.
-
Docker Compose YAML File (if applicable):
- If you’re using Docker Compose and have a
docker-compose.yml
file, the error could indicate a syntax issue (e.g., invalid indentation, stray characters). - Check the file for errors:
docker-compose config
- If you’re using Docker Compose and have a
- If you have a
docker-compose.yml
, share its contents, and I can help identify the issue.
-
Corrupted APT Cache:
- Clear the APT cache and try again:
sudo apt-get clean sudo rm -rf /var/lib/apt/lists/* sudo apt-get update
-
Locale or Environment Issues:
- Ensure your system’s locale is set correctly, as some tools misbehave with invalid locales:
locale
-
If the output shows errors, set a valid locale:
sudo locale-gen en_US.UTF-8 export LC_ALL=en_US.UTF-8
🔍 If the Issue Persists
If you still encounter the GPG key error or the "token" error, please provide:
- The exact error message when running
sudo apt-get update
. - The contents of
/etc/apt/sources.list.d/docker.list
(runcat /etc/apt/sources.list.d/docker.list
). - If using Docker Compose, share your
docker-compose.yml
file. - The output of
ls -l /etc/apt/keyrings/docker.gpg
to verify permissions.
✅ Final Notes
- The steps above align with Docker’s official installation guide for Ubuntu and account for Ubuntu 22.04’s stricter APT security.
- The "token" error is likely a secondary issue, possibly from a malformed configuration. The cleanup and precise commands above should prevent it.
- If you’re still stuck, share the additional details, and I’ll tailor the solution further.
Happy containerizing! 🐳 Let me know if you need more help.