How to Install and Use Docker on Ubuntu 24.04


Docker is a tool that simplifies the development, deployment, and management of applications by enabling them to be packaged with their dependencies in portable containers.

This guide aims to provide detailed instructions on how to install Docker on the most recent Ubuntu 24.04 Noble Numbat; enabling users to successfully set up and utilize Docker.

Installing Docker on Ubuntu

Let’s take a look at the prerequisites before proceeding to get Docker on Ubuntu.

 

Prerequisites

1. Operating system requirement:

  • The minimum OS requirement is Ubuntu 20.04 Focal Fossa or later
  • The OS should be 64bit

2. Compatible Architecture:

Before installing, it is also important to know for what system architectures Docker is available. The Docker is available for the following architecture:

  • x86_64 (amd64)
  • armhf
  • arm64
  • s390x
  • ppc64le

To know the architecture of your Ubuntu version, use the following command:

dpkg --print-architecture

3. Uninstalling the old version of Docker:

  • Uninstall the old version of Docker to avoid package conflicts

Docker installed through the official package manager (APT) may provide you with unofficial versions of Docker. It is crucial to uninstall them to get the official version of Docker on Ubuntu.

To completely uninstall the older version of Docker, use the following command:

for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done

The above command is a bash script that loops through all the packages needing to be removed. Note that you may get output saying no package is uninstalled if these packages are not available on your system.

 

Docker Desktop vs Docker Engine

The Docker is available in two different variations:

  • Docker Desktop
  • Docker Engine

The Docker Desktop is a GUI version that makes it easier to handle containers and images. On the other hand, the Docker Engine is responsible for the creation, deployment, and management of containers, providing a streamlined and efficient environment for developers to package their applications into portable units known as containers. Moreover, the Docker Desktop is managed through UI while Docker Engine is through CLI.

Note: At the core, Docker Desktop uses Docker Engine to maintain the applications.

Installing Docker Desktop or Docker Engine depends upon personal preference. If you are using Ubuntu with a desktop environment, then I would recommend installing Docker Desktop. If you like to prefer CLI then Docker Engine will do the job.

Note: It is important to note that Docker Desktop is only available for x86_64 or amd64 system architecture. While, Docker Engine is more versatile across different architectures, including ARM. So, if you are using ARM-based Ubuntu even with GUI, then you will have to install Docker Engine.

 

Installing Docker Desktop on Ubuntu

To install the Docker Desktop on Ubuntu, use the following steps:

1. To proceed with the installation of Docker Desktop, begin by downloading the Docker Desktop Debian (Deb) package from the given link.

2. Install the Deb package using the apt package manager:

sudo apt install ./docker-desktop.deb

3. To verify the installation of Docker Desktop, use the following command:

docker --version

4. Launch the Docker desktop from the application menu. Or execute the docker-desktop in the terminal.

 

Installing Docker Engine on Ubuntu

There are three methods to install Docker on Ubuntu:

  • Using APT Repository (Recommended)
  • Using Snap
  • Using Deb Packages
  • Using Script

1. Using APT Repository

To install the Docker Engine on Ubuntu, follow the steps given below:

1. Add GPG key

To enhance the reliability and safety of Docker installations, add Docker’s official GPG key to your system, you can utilize the following commands. These commands are essential for ensuring the authenticity and security of Docker packages during the installation process.

sudo apt install ca-certificates curl

sudo install -m 0755 -d /etc/apt/keyrings

sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc

sudo chmod 444 /etc/apt/keyrings/docker.asc

The first command is installing two packages, ca-certificates, and curl. The second command is creating a directory /etc/apt/keyrings providing read, write, and execute permissions to the owner. The -d flag ensures that the directory does not exist already. The third command is downing the GPG using curl and saving it to /etc/apt/keyrings/docker.asc file. The last command is giving read permissions of docker.asc file to all users.

2. Add the Docker Repository

Now, set up the Docker repository for Ubuntu by executing the following command.

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

The above command defines the repository for Docker on Ubuntu setting up the environment variable.

Now, update the package repository using the apt update command.

sudo apt update

3. Install the Docker Engine Packages

After setting up the repository, install the required packages of Docker.

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

To install the specific version of Docker, first list all the versions using:

apt-cache madison docker-ce | awk '{ print $3 }'

Select the version and then assign it to a variable:

Docker_Version=[Docker Version selected from the above output]

Now, install the selected version using the following command.

sudo apt-get install docker-ce=$Docker_Version docker-ce-cli=$VERSION_STRING containerd.io docker-buildx-plugin docker-compose-plugin

4. Verify the Installation

To verify the Docker installation, run the default hello-world image using the docker run command.

docker run hello-world

The hello-world is a test image that is downloaded and run in a container.

2. Using Snap

The snap package manager can also be used to install Docker on Ubuntu.

sudo snap install docker

This is the quickest way to get Docker on the latest Ubuntu.

3. Using Deb Packages

If you want to manually install Docker for a specific distribution then you need to download the deb packages individually.

1. Access the Deb Package

Access the deb packages of a specific Ubuntu version from https://download.docker.com/linux/ubuntu/dists/.

2. Select the Ubuntu Version

Select the Ubuntu version name and navigate to the pool/ and then stable/ directories.

3. Download the deb Packages

Select the Architecture and download the following deb packages.

  • containerd
  • docker-buildx-plugin
  • docker-ce
  • docker-compose-plugin
  • docker-ce-cli

Save these packages in a directory and proceed with the next step.

5. Install the deb Packages

Install all the deb packages using dpkg:

sudo dpkg -i ./containerd.io_1.5.10-1_arm64.deb ./docker-buildx-plugin_0.10.2-1~ubuntu.22.04~jammy_arm64.deb ./docker-ce-cli_23.0.1-1~ubuntu.22.04~jammy_arm64.deb ./docker-ce_20.10.13~3-0~ubuntu-jammy_arm64.deb ./docker-compose-plugin_2.10.2~ubuntu-jammy_arm64.deb

The Docker service will start automatically on Ubuntu but in some distributions you may need to start it manually.

sudo systemctl start docker

6. Verify Installation

Verify the installation by running the test image:

docker run hello-world

4. Using Script

Installing Docker using convenience scrip is not recommended, especially for the production environment. However, it can be used to manage Docker according to your preferences.

1. Download the Docker Script

Download the script using the following command:

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

2. Run the Script

Now, run the script to install Docker using the sh command.

sudo sh get-docker.sh

3. Verify Installation

To verify, use the following command:

docker run hello-world

You can also download and install the pre-release of Docker on Ubuntu using the following commands:

curl -fsSL https://test.docker.com -o test-docker.sh

sudo sh test-docker.sh

 

Getting Started with Docker

In this section, we will explore how to use Docker. Let’s begin with understanding the docker command.

The docker Command

Docker includes a command line tool called docker to manage Docker, images, containers, and volumes.

The general syntax for using docker command is given below:

docker [OPTIONS] SUB_COMMAND [ARGs]

To list all the subcommands, execute the docker command:

docker

To get the help of a specific subcommand, use the –help option with the subcommand name.

docker SUB_COMMAND --help

For example, to get help with the docker stop subcommand use, docker stop –help.

Docker Images

Docker images serve as the foundational blueprint for creating Docker containers and providing the essential instructions needed to run containerized applications efficiently. You can access all the images from https://hub.docker.com/.

To access a hello-world image, use the following command:

sudo docker run hello-world

The command first checks whether the image is locally available or not, if not, then it will download the image and run it. After downloading the image, Docker creates a container and launches the application it is containing.

To search whether a Docker image is available or not in Docker Hub, use the search option with the image name.

sudo docker search python

The above command will list all the Python images from the Docker Hub.

Note that the official image has a [OK] tag against it.

To download the image, use the pull option with the image name.

sudo docker pull python

Here, I am downloading the Python image. You can download Linux images as well, such as Ubuntu, Alpine, Kali, and other Linux distributions.

To list all the downloaded images use:

docker images

The next step is creating a container using the Python image.

Containerize an Application

Before we proceed with further steps, it’s important to grasp the fundamental distinction between a Docker image and a Docker container and their roles.

A Docker image can be likened to a blueprint or template that contains all the necessary instructions and dependencies required to create a Docker container; it cannot be run. Conversely, a Docker container is a runtime instance of a Docker image, operating as an independent process within your system environment.

Let’s create, containerize, and run a Python application using the following steps:

1. Create a Project Directory and Files

Create a project folder on your PC; I am creating a docker_project folder in the home directory.

sudo mkdir docker_project

Inside this directory, create a Python application file and a dockerfile.

sudo touch app.py

sudo touch dockerfile

2. Create a Python Script

Open the app.py file in any text editor and type the code. I am employing the print command to instruct the system to display a string content to the console or standard output.

sudo nano app.py

You can create any Python application.

3. Create a dockerfile

The dockerfile is crucial to containerize or package any application. It contains instructions to build the image.

To create a dockerfile file open it in any text editor:

sudo nano dockerfile

The basic dockerfile is given below:

FROM python:3.10

WORKDIR /application

COPY . /application

CMD ["python", "./app.py"]

FROM python:3.10 specifies the base image of the application.

WORKDIR /application indicates the working directory where the commands written below will run. In the absence of this directory, the dockerfile will create it.

COPY . /application copying the current directory . to the /application directory.

CMD [“python”, “./app.py”] The Docker container will run this command on startup, in this case, the Python application that we created in the previous step.

4. Build the Image

Now, build the image using the docker build command.

sudo docker build -t python_app .

In the above command, the -t indicates the tag to identify the image, in this case, the tag is pythonApp. The period (dot) at the end of the command signifies that the image will be stored in the present working directory. However, any directory can be specified here.

Note: The tag should always be in lowercase.

The output indicates that the Python application is successfully containerized.

To list the images, use the docker image command.

sudo docker images

5. Running the Container

Now, use the docker run command to create a container from this image and run it.

docker run python_app

In the output image, it can be seen that the container has successfully run.

Uninstalling Docker from Ubuntu

To uninstall the Docker from Ubuntu, use the following steps:

1. Delete the deb Packages

Uninstall the following packages:

  • containerd.io
  • docker-ce
  • docker-ce-cli
  • docker-buildx-plugin
  • docker-compose-plugin
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

2. Delete the Docker Directories

The images, containers, and volumes do not delete automatically, to delete them use the following commands:

sudo rm -r /var/lib/docker

sudo rm -r /var/lib/containerd

You may need to delete other files related to Docker manually.

Conclusion

Docker comes in two different variations, Docker Desktop and Docker Engine. Docker Desktop is a UI-based application that can be installed on Ubuntu by downloading its deb package. However, Docker Engine needs to be installed by adding a repository. Docker Desktop is only available for x86_64(amd64) architecture. So, if you are using Docker on the arm-based system then you can only install the Docker engine which can be managed through CLI.

Print Friendly, PDF & Email
Categories