Docker Installation On Ubuntu

Docker Installation On Ubuntu Step By Step

First uninstall old versions if any

adminuser@mysystem:~$ sudo apt-get remove docker docker-engine docker.io containerd runc

Reading package lists… Done

Building dependency tree

Reading state information… Done

E: Unable to locate package docker-engine

adminuser@mysystem:~$ sudo apt-get update

Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease

Hit:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease

Hit:3 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease

Hit:4 http://in.archive.ubuntu.com/ubuntu focal-security InRelease

Reading package lists… Done

adminuser@mysystem:~$

Install using the repository

Set up the repository

Update the apt package index and install packages to allow apt to use a repository over HTTPS:

 sudo apt-get update

 sudo apt-get install \

    ca-certificates \

    curl \

    gnupg \

    lsb-release

adminuser@mysystem:~$ sudo apt-get install \ca-certificates \curl \gnupg \lsb-release

Reading package lists… Done

Building dependency tree

Reading state information… Done

lsb-release is already the newest version (11.1.0ubuntu2).

lsb-release set to manually installed.

ca-certificates is already the newest version (20210119~20.04.2).

ca-certificates set to manually installed.

curl is already the newest version (7.68.0-1ubuntu2.11).

curl set to manually installed.

gnupg is already the newest version (2.2.19-3ubuntu2.1).

gnupg set to manually installed.

0 upgraded, 0 newly installed, 0 to remove and 35 not upgraded.

Add Docker’s official GPG key

adminuser@mysystem:~$ sudo mkdir -p /etc/apt/keyrings

adminuser@mysystem:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg –dearmor -o /etc/apt/keyrings/docker.gpg

adminuser@mysystem:~$

Use the following command to set up the repository:

adminuser@mysystem:~$ 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

Install Docker Engine

Update the apt package index, and install the latest version of Docker Engine, containerd, and Docker Compose, or go to the next step to install a specific version:

adminuser@mysystem:~$ sudo apt-get update

Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease

Hit:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease

Get:3 https://download.docker.com/linux/ubuntu focal InRelease [57.7 kB]

Hit:4 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease

Hit:5 http://in.archive.ubuntu.com/ubuntu focal-security InRelease

Get:6 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages [17.6 kB]

Fetched 75.2 kB in 1s (53.6 kB/s)

Reading package lists… Done

adminuser@mysystem:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Reading package lists… Done

Building dependency tree

Reading state information… Done

The following additional packages will be installed:

  docker-ce-rootless-extras docker-scan-plugin pigz slirp4netns

Suggested packages:

  aufs-tools cgroupfs-mount | cgroup-lite

The following NEW packages will be installed:

  containerd.io docker-ce docker-ce-cli docker-ce-rootless-extras docker-compose-plugin docker-scan-plugin pigz slirp4netns

0 upgraded, 8 newly installed, 0 to remove and 35 not upgraded.

Need to get 108 MB of archives.

After this operation, 449 MB of additional disk space will be used.

Do you want to continue? [Y/n] y

Get:1 https://download.docker.com/linux/ubuntu focal/stable amd64 containerd.io amd64 1.6.6-1 [28.1 MB]

Get:2 http://in.archive.ubuntu.com/ubuntu focal/universe amd64 pigz amd64 2.4-1 [57.4 kB]

………

Install a specific version of Docker Engine

To install a specific version of Docker Engine, list the available versions in the repo, then select and install:

a. List the versions available in your repo:

apt-cache madison docker-ce

adminuser@mysystem:~$ apt-cache madison docker-ce

 docker-ce | 5:20.10.17~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages

 docker-ce | 5:20.10.16~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages  docker-ce | 5:20.10.15~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable

Install a specific version using the version string from the second column, for example, 5:20.10.16~3-0~ubuntu-jammy.

sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io docker-compose-plugin

Verify that Docker Engine is installed correctly by running the hello-world image.

$ sudo docker run hello-world

Install Docker on Ubuntu From Package

Go to https://download.docker.com/linux/ubuntu/dists/, choose your Ubuntu version, then browse to pool/stable/, choose amd64armhfarm64, or s390x, and download the .deb file for the Docker Engine version you want to install.

Install Docker Engine, changing the path below to the path where you downloaded the Docker package.

  $ sudo dpkg -i /path/to/package.deb

The Docker daemon starts automatically.

  • Verify that Docker Engine is installed correctly by running the hello-world image.
$ sudo docker run hello-world

Upgrade Docker Engine

You can run the script with the DRY_RUN=1 option to learn what steps the script will execute during installation:

$ curl -fsSL https://get.docker.com -o get-docker.sh
$ DRY_RUN=1 sh ./get-docker.sh

This example downloads the script from get.docker.com and runs it to install the latest stable release of Docker on Linux:

$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh

Uninstall Docker Engine

Uninstall the Docker Engine, CLI, Containerd, and Docker Compose packages:

 $ sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-compose-plugin
  • Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:
$ sudo rm -rf /var/lib/docker
$ sudo rm -rf /var/lib/containerd

You must delete any edited configuration files manually.

Manage Docker as a non-root user

To create the docker group and add your user

Create the docker group.

$ sudo groupadd docker

Add your user to the docker group.

$ sudo usermod -aG docker $USER

Log out and log back in so that your group membership is re-evaluated.

On Linux, you can also run the following command to activate the changes to groups

newgrp docker

Verify that you can run docker commands without sudo.

$ docker run hello-world

Configure Docker to start on boot

$ sudo systemctl enable docker.service
$ sudo systemctl enable containerd.service

To disable this behavior, use disable instead.

$ sudo systemctl disable docker.service
$ sudo systemctl disable containerd.service