How to Install Docker on Ubuntu 24.04: Step-by-Step Guide
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
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.
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
Now run the groups
command to verify that the user has been added to the docker group.
groups cherry
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
#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.
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
#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.
To download or pull the image, run the docker pull
command:
docker pull nginx
To list existing Docker images, run:
docker images
The output includes the image name, tag, Image ID, creation date, and size.
#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
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.
You can view currently running containers on the terminal using the docker ps
command.
docker ps
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
.
To stop a running container, issue the docker stop
command followed by the container ID as shown.
docker stop 9bbb81d2db63
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
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.
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
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.