Install Docker Engine, Docker CLI & Docker Compose on Ubuntu 20.04/22.04
Docker provides a suite of development tools, services, trusted content, and automations, used individually or together, to accelerate the delivery of secure applications. Docker helps developers build, share, and run applications anywhere — without tedious environment configuration or management.
The Compose plugin is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration. Compose works in all environments; production, staging, development, testing, as well as CI workflows.
You can install Docker Engine and the Compose plugin in different ways, depending on your needs. In this tutorial, I am installing Docker and Compose from Docker's Apt repository. To see other ways to install, see https://docs.docker.com/engine/install/ubuntu/
Note: Before you can install Docker Engine, you must first make sure that any conflicting Docker packages are uninstalled. See the section titled Uninstall old versions below if you have installed a previous version of Docker.
1. Set up Docker's Apt repository
$ sudo apt-get update $ sudo apt-get install ca-certificates curl gnupg $ sudo install -m 0755 -d /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 $ echo \ "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null $ sudo apt-get update
2. Install the latest Docker packages
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
3. Verify that the Docker Engine installation is successful by running the hello-world image.
$ sudo docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints a confirmation message and exits.
You have now successfully installed and started Docker Engine/Compose.
Manage Docker as a non-root user
If you are receiving errors when trying to run Docker without root, this is because the
docker user group exists but contains no users, which is why you’re required to use
sudo to run Docker commands. If you DO NOT want to preface the docker command with
sudo, add your user to the
1. Add your user to the
$ sudo usermod -aG docker $USER
Log out and log back in so for this to take effect.
2. Verify that you can run
docker commands without
$ docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits.
If you initially ran Docker CLI commands using
sudo before adding your user to the
docker group, you may see the following error:
WARNING: Error loading config file: /home/user/.docker/config.json - stat /home/user/.docker/config.json: permission denied
This error indicates that the permission settings for the ~/.docker/ directory are incorrect, due to having used the
sudo command earlier. To fix this problem, either 1) remove the ~/.docker/ directory (it's recreated automatically, but any custom settings are lost), or 2) change its ownership and permissions using the following commands:
$ sudo chown "$USER":"$USER" /home/"$USER"/.docker -R $ sudo chmod g+rwx "$HOME/.docker" -R
On Debian and Ubuntu, the Docker service starts on boot by default.
Uninstall old versions
Before you can install Docker Engine, you must first make sure that any conflicting packages are uninstalled.
$ for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
apt-get might report that you have none of these packages installed. Images, containers, volumes, and networks stored in
/var/lib/docker/ aren't automatically removed when you uninstall Docker. If you want to start with a clean installation, and prefer to clean up any existing data, follow the Uninstall Docker Engine section below.
Uninstall Docker Engine
$ sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
Images, containers, volumes, or custom configuration files on your host aren't automatically removed. To delete all images, containers, and volumes:
$ sudo rm -rf /var/lib/docker $ sudo rm -rf /var/lib/containerd
You have to delete any edited configuration files manually.
Update Docker Engine and Compose:
$ sudo apt-get update $ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Using Uncomplicated Firewall (ufw) or fiewalld
If you use ufw (Uncomplicated Firewall) or firewalld to manage firewall settings, be aware that when you expose container ports using Docker, these ports bypass your firewall rules. ufw is a frontend that ships with Debian and Ubuntu, and it lets you manage firewall rules. Docker and ufw use iptables in ways that make them incompatible with each other. When you publish a container's ports using Docker, traffic to and from that container gets diverted before it goes through the ufw firewall settings. Docker routes container traffic in the nat table, which means that packets are diverted before it reaches the INPUT and OUTPUT chains that ufw uses. Packets are routed before the firewall rules can be applied, effectively ignoring your firewall configuration. https://docs.docker.com/network/packet-filtering-firewalls/#docker-and-ufw
Useful Docker commands
Check Docker Engine version:
$ docker version
Check Compose plugin version:
$ docker compose version
Start the daemon with this command:
$ sudo systemctl start docker
To enable autostart at boot time:
$ sudo systemctl enable docker
Stop and remove docker image:
$ docker stop CONTAINER_ID
$ docker rm CONTAINER_ID
To get CONTAINER_ID:
$ docker ps
To check the ports:
$ sudo netstat -lnpt
List all Docker containers (running, all, all in quiet mode)
$ docker container ls $ docker container ls --all $ docker container ls -aq
List Docker images
$ docker image ls
Remove one or more images
$ docker image rm <docker image id>
Remove unused images
$ docker image prune <docker image id>
Docker provides a single command that will clean up any resources — images, containers, volumes, and networks — that are dangling (not associated with a container)
$ docker system prune
To additionally remove any stopped containers and all unused images (not just dangling images), add the -a flag to the command:
$ docker system prune -a
Install Docker Desktop on Linux
What is the difference between Docker Desktop for Linux and Docker Engine
– Docker Desktop for Linux and Docker Engine can be installed side-by-side on the same machine. Docker Desktop for Linux stores containers and images in an isolated storage location within a VM and offers controls to restrict its resources. Using a dedicated storage location for Docker Desktop prevents it from interfering with a Docker Engine installation on the same machine. While it's possible to run both Docker Desktop and Docker Engine simultaneously, there may be situations where running both at the same time can cause issues.