Install Docker and Docker Compose on ubuntu desktop

Install Docker and Docker Compose according to the official instructions. The OS to be installed is Ubuntu 22.04 Desktop.

https://docs.docker.com/engine/install/ubuntu/

Prerequisite Setup

Install the following packages

  • ca-certificates: This is a package containing a generic root certificate, required to validate the server’s certificate during HTTPS communication. this package is needed to validate the certificate when downloading from the official Docker repository.
  • curl: a command line tool, used to transfer data from a URL. In this guide, it is used to download the official Docker GPG key.
  • gnupg: GnuPG is a set of tools for encrypting and digitally signing data. This guide uses it to handle Docker’s official GPG keys. (No longer needed)
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
If you check the official site, you will see that the command has been changed to the following
sudo apt-get update
sudo apt-get install ca-certificates curl

Add the official Docker GPG key

Docker software is provided by the official sources, but to avoid installing software from unauthorized sources, use the official GPG keys to verify their authenticity. The following command sets up the configuration for this purpose.

  1. Create a directory for the key ring:
sudo install -m 0755 -d /etc/apt/keyrings

This command creates a directory (keyrings) for storing GPG keys.

  1. Download the official Docker GPG key & save it in the keyring:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
Update article as the following command has been changed
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc

Download the GPG key from the official Docker website with curl and add that key to the keyring with the gpg command.

  1. Change the keyring permissions:
sudo chmod a r /etc/apt/keyrings/docker.gpg
Update the article as the following command has been changed
sudo chmod a r /etc/apt/keyrings/docker.asc

Change permissions to allow the system to read the GPG key stored in the keyring.

Add official Docker repositories

Repositories are like online databases that store software packages and update information; Linux distributions such as Ubuntu download packages from these repositories when installing certain software In addition to the official repositories, there are also third-party repositories. In addition to the official repositories, third-party software providers (in this case, Docker) may also offer their own repositories.

By adding the official Docker repositories to your system, you can install Docker-related software and updates directly from the official sources.

The following command adds the official Docker repository to the Ubuntu source list.

echo \foo}
  "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
This command has also changed
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | }
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

This command creates the URL of the official Docker repository for your system’s architecture and Ubuntu version, and stores it in the /etc/apt/sources.list.d/ directory.

Update the package list

Software on your computer is installed from a sort of online “store” (called a repository). By updating the product list in this “store” to the latest state, you can download and install the latest version of the software.

Use the following command to update the product list to the latest version.

sudo apt-get update

Installing Docker and Docker Compose

Docker is a tool for running applications as lightweight, portable “package” like objects using a technology called containers. This step installs the necessary Docker-related software.

Use the following command to install Docker and related tools

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

The installation is now complete. Enter some simple commands to verify the installation.

Confirmation of operation

After installing Docker, a common command to check if it is actually working is docker ps. This command displays a list of containers that are currently running.

docker ps

However, sometimes when you run this command, you may get an error like the following

permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get “http:///var/run/docker.sock/v1.24/ containers/json”: dial unix /var/run/docker.sock: connect: permission denied

Reason for error

This error is caused by insufficient permission when communicating with the Docker daemon (process running in the background).

How to deal with the error

  1. Temporary solution: you can run the command with administrative privileges by running the command using sudo
sudo docker ps

Permanent solution: To run the docker command without having to use sudo every time, follow these steps

  • Add the current user to the Docker group: Docker only allows users belonging to a specific “group” to access it without privileges. Add the current user to that group.
sudo gpasswd -a mamushi docker

Change ownership of Docker socket files:

A specific “socket file” is used to communicate with Docker, and we can change the ownership of this file so that the current user can also access it.

sudo chown mamushi:mamushi /var/run/docker.sock

Now you can run docker commands without sudo. However, be aware that adding a user to the Docker group will give that user extensive permissions to the system with Docker, so be careful for security reasons.

Please share if you like it!
TOC