Sub topic 12
Upgrading and uninstalling
This chapter assumes you have a working Docker installation, you can use sudo or install software on your machine, and you know what images, containers, and volumes are. It covers the last step of the install life cycle: keeping Docker current, and removing it without losing data you wanted to keep or leaving data you wanted gone.
What an upgrade changes
An upgrade replaces the programs: the daemon, the CLI, containerd, and the plugins. It does not replace your data. Images, containers, networks, and volumes live in /var/lib/docker on Linux Engine. Docker Desktop keeps them inside its own virtual machine disk image.
Two things can still go wrong during an upgrade:
- The daemon restarts, so running containers stop unless you enabled live restore in
daemon.json. - A new version can change defaults or remove features. Read the release notes before a major upgrade.
Upgrading Docker Engine on Ubuntu
Because you installed from Docker's apt repository, upgrades arrive through apt. Refresh the package index, then upgrade only the Docker packages.
sudo apt-get update
sudo apt-get install --only-upgrade docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
docker versionThe --only-upgrade flag upgrades packages that are already installed and installs nothing new. Run docker version afterward. The Client and Server sections should show the new version, and both should respond.
To move to a specific version instead of the latest, list what the repository offers and install a pinned version.
apt-cache madison docker-ce | head -5Pick a version string from the second column of the output. Then install it with sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING>. Add sudo apt-mark hold docker-ce docker-ce-cli if you want routine apt upgrade runs to leave Docker alone.
Upgrading Docker Desktop
Docker Desktop on macOS and Windows checks for updates and shows a notification. Accept it, and the app downloads the new version and restarts. On Linux, download the new .deb package from Docker and install it over the old one with sudo apt-get install ./docker-desktop-amd64.deb. Take the exact filename from the download page.
Uninstalling Docker Engine
Removal has two separate stages: the packages, then the data. Keeping them separate protects you from deleting images and volumes by accident.
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extrasThis removes the packages and their configuration files. It leaves /var/lib/docker and /var/lib/containerd alone, so the images, containers, and volumes are still on disk. If you reinstall later, Docker finds them again.
If you want a clean machine, remove the data directories as a deliberate second step.
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerdYou may also want to remove the apt source and key, so apt stops contacting Docker's repository.
sudo rm /etc/apt/sources.list.d/docker.list
sudo rm /etc/apt/keyrings/docker.ascConfig files you created yourself, such as /etc/docker/daemon.json and ~/.docker, are not removed by purge. Delete them by hand if you no longer need them.
Uninstalling Docker Desktop
On macOS, quit Docker Desktop, then use its Troubleshoot menu and choose Uninstall. After that you can drag the app to the Trash. On Windows, use Settings > Apps to uninstall it. On Linux, run sudo apt-get remove docker-desktop. The Desktop uninstaller can delete its virtual machine disk, which holds your images and volumes, so check what the dialog says before you confirm.
The full life cycle
Common errors
- The upgrade finishes but
docker versionstill shows the old server version. The daemon has not restarted. Runsudo systemctl restart docker. - Containers disappear after the upgrade. They stopped when the daemon restarted. Start them again, or set a restart policy such as
--restart unless-stoppedfor next time. - You reinstall and find old images. You purged the packages but not
/var/lib/docker. This is the expected behavior. - You purged the packages and
dockeris still on the path. A Docker Desktop installation or a snap package can supply its own binary. Runwhich dockerto find where it comes from.
Exercise
You run the apt-get purge command on an Ubuntu server, then reinstall Docker Engine a week later. Your old images and a PostgreSQL volume are still there. Why, and what single step would have removed them before reinstalling?
Show answer
apt-get purge removes the packages and their configuration, but it does not touch /var/lib/docker, where Docker stores images, containers, and volumes. Running sudo rm -rf /var/lib/docker after the purge would have deleted them. You should back up any volume you still need first, because the deletion is permanent.
Exercise
Which flag makes apt-get install upgrade Docker packages that are already installed without installing anything new, and which command confirms that both the client and the daemon are on the new version?
Show answer
The --only-upgrade flag does this. Running docker version shows the Client and Server sections, so you can check that both report the new version.