Cyber Month Deal - up to 36% OFF

How to Install Docker on Ubuntu 24.04: Step-by-Step Guide

Published on Jul 30, 2024 Updated on Dec 11, 2024

In this tutorial, you will learn how to install Docker on Ubuntu 24.04. Not using Ubuntu 24.04? Check our previous guide on How to Install Docker on Ubuntu 22.04.

What is Docker?

Docker is an open-source containerization platform that lets you seamlessly build, share, deploy, and orchestrate applications anywhere - be it on Linux, Windows, Mac or any other computing environment.

Written in the Go programming language, Docker uses OS-level virtualization to package applications inside containers. Containers are isolated environments that package everything an application needs to run: source code, binaries, dependencies, libraries, etc. Docker containers ensure that applications run reliably, even when ported from one computing environment to another.

Ready to supercharge your Docker infrastructure? Scale effortlessly and enjoy flexible storage with Cherry Servers bare metal or virtual servers. Eliminate infrastructure headaches with free 24/7 technical support, pay-as-you-go pricing, and global availability.

Prerequisites

To follow along and learn how to install Docker on Ubuntu 24.04, ensure you have the following requirements in place:

An instance of Ubuntu 24.04 with SSH access; A sudo user configured on the server instance.

Without further ado, let’s get started.

How to install Docker on Ubuntu 24.04?

Follow the below seven steps to install Docker on Ubuntu 24.04.

Step 1. Update the system and install dependencies

To get off the ground, log into your server and update the package index defined in the /etc/apt/sources.list file and /etc/apt/sources.list.d/ directory.

sudo apt update

A few dependencies are needed for the installation to progress smoothly. So, run the command below to install them.

sudo apt install curl apt-transport-https ca-certificates software-properties-common 

With the package lists updated, and requisite software packages in place, proceed to install Docker.

Step 2. Install Docker

There are two ways to install Docker on Ubuntu. First, you can install it from the default repositories using the APT package manager.

sudo apt install docker.io -y

However, the version installed is not the latest. To install the most current version, you need to install it from the official Docker repository.

To do so, download the Docker GPG key using the curl command as shown.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Next, add the Docker APT repository to your system. The command creates a docker.list repository file in the /etc/apt/sources.list.d directory.

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Next, update the local package index to notify the system of the newly added repository.

sudo apt update

Now install Docker Community Edition (free to download and use) as follows. In this command, the -y option allows for non-interactive installation.

sudo apt install docker-ce -y

install-docker-on-ubuntu-24.04

The Docker service starts automatically upon installation. You can verify its status by running the command:

sudo systemctl status docker

The output below is a confirmation that Docker is running as expected.

check-docker-status-ubuntu-24.04

Also read: How to uninstall Docker

Step 3. Add user to Docker group

By default, Docker is set to run as root or a regular user with elevated privileges ( sudo user ). The implication is that only the root or sudo user can run Docker commands. Running docker commands as a regular user will result in a 'permissions error' message.

Since we already have a sudo user called cherry configured, running Docker as this user is just fine. An even better option is to add the currently logged-in user, in this case, cherry to the docker group. This ensures that we don't have to invoke sudo whenever running Docker commands since the user already belongs to the docker group.

To add the currently logged-in user, use the usermod command.

sudo usermod -aG docker $USER

Next, run the newgrp command.

newgrp

add-logged-in-user-to-docker-group-ubuntu-24.04

Now run the groups command to verify that the user has been added to the docker group.

groups cherry

check-if-regular-user-belongs-to-docker-group

To start running Docker commands without invoking sudo, close and start a new shell session. Alternatively, you can run the following command without closing your current session.

su -$USER

Moving forward, you can run Docker commands seamlessly as a regular user. For example, you can check the version of Docker by running:

docker version

check-docker-version

Step 4. Test Docker installation

Up to this point, Docker has been successfully installed. Before proceeding further, you must ensure that you can pull images and run containers from Docker Hub, the default Docker registry configured to run with Docker. To test this, we will create a container from the hello-world image.

docker run hello-world

You will get the following output on your screen.

test-docker-installation-run-hello-world

So what exactly happened?

In the background, the Docker client searched for an image called hello-world with a tag latest on the local system but could not find it. It then contacted Docker Hub, pulled the image to the local system, and created a new container from that image. The container then streams the output on your terminal and exits.

The output displayed confirms that Docker is working as expected.

Step 5. How to run Docker commands

The Docker CLI uses the following syntax:

docker [OPTIONS] command

To get a list of common Docker commands, run the docker command without any options.

docker 

check-available-docker-commands

Step 6. Working with Docker images

A docker image is essentially a file containing instructions to build a container. Docker is configured to use Docker Hub, an online registry for Docker images. You can search Docker images by running the docker search command followed by the image name.

For example, to search for the Nginx image on the Docker hub, run the command:

docker search nginx

Docker will sift through the Docker registry and generate a list of images matching the image you are searching for. In the Official column, the Ok flag indicates that the image has been developed by the official vendor behind the project, in this case, Nginx.

search-docker-image-docker-hub

To download or pull the image, run the docker pull command:

docker pull nginx

pull-docker-image-from-docker-hub

To list existing Docker images, run:

docker images

The output includes the image name, tag, Image ID, creation date, and size.

list-existing-docker-images-on-local-machine

Step 7. Working with Docker Containers

The docker run command creates a container from an image. When the image is locally available on your system, the container is created right away, and its container ID is printed on the console. If the image is unavailable, Docker pulls it from the Docker Hub registry and creates the container. To demonstrate how to work with containers, we will create an Nginx container, as shown.

docker run -d -p 8080:80 nginx

run-nginx-docker-container-in-detached-mode

The -d option runs the container in detached mode ( in the background ) while the -p option maps port 80 on the container to port 8080 on your Docker host.

To confirm that the web server is running, launch your web browser and visit the URL:

http://server-ip:8080

You will see the Nginx welcome page displayed below.

confirm-nginx-docker-container-is-running

You can view currently running containers on the terminal using the docker ps command.

docker ps 

verify-currently-running-docker-containers

To list all containers, including those that exited, pass the -a option

docker ps  -a

From the output, you can see two containers: the nginx container, which is currently running, and the hello-world.

verify-all-running-docker-containers

To stop a running container, issue the docker stop command followed by the container ID as shown.

docker stop 9bbb81d2db63

stop-a-docker-container

Alternatively, you can use the arbitrary container name assigned to the container to stop it. In this case, the container name is lucid_taussig. So the command would look like:

docker stop lucid_taussig

To remove a container, stop it first, then run the docker rm command followed by the container ID.

docker rm 9bbb81d2db63

remove-a-docker-container

To remove an image, run the docker rmi command followed by the image name. But first, you need to remove the container associated with the image before removing the image. Otherwise, you’ll encounter a conflict error as shown.

error-when-removing-a-docker-image

Therefore, be sure to remove the container associated with the image by stopping it and removing it.

docker stop 41f7677d2a02 && docker rm 41f7677d2a02

You can now remove the image using the docker rmi command followed by the image ID or name.

docker rmi nginx

remove-a-docker-image

Also read: How to start Docker Daemon

Conclusion

In this tutorial, you learned how to install Docker on Ubuntu 24.04, the latest LTS release at the time of writing. You have also learned basic commands for working with Docker containers and images.

Cloud VPS - Cheaper Each Month

Start with $9.99 and pay $0.5 less until your price reaches $6 / month.

Share this article

Related Articles

Published on Apr 10, 2024 Updated on May 15, 2024

How to Install Nextcloud AIO on Docker | Step-by-Step

This tutorial will show you how to install Nextcloud using Nextcloud AIO Docker image to help significantly reduce configuration steps.

Read More
Published on May 15, 2024 Updated on May 15, 2024

Portainer Tutorial: How to Update [and Restart] Portainer

This tutorial demonstrates how to manage Portainer: how to update Portainer, how to restart Portainer, and update existing containers.

Read More
Published on Apr 14, 2020 Updated on Feb 6, 2024

Docker Swarm vs. Kubernetes – What are the Subtle Differences?

Kubernetes and Docker Swarms are essential tools used to deploy containers inside a cluster. Learn how they differ and when to use them

Read More
We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 06ac5732e.831