Skip to main content

How to install docker in Ubuntu 24.04

7 minutes


Introduction

Docker has risen to become a powerful tool for packaging containers. By encapsulating applications and their dependencies into containers, Docker enables seamless deployment across different environments. 

In this blog post, we'll guide you through the steps to install Docker on Ubuntu, so you can take your development projects to the next level. We'll also explore the crucial Docker commands that every developer needs to be familiar with. From creating and operating containers to handling networks and volumes, we'll discuss few essential commands that will simplify your Docker workflow. 

You can also use the steps described below to install docker on Digitalocean or any other cloud service provider.

Prerequisites

To install Docker on Ubuntu, ensure you meet the following requirements.

  • A running local or cloud instance of Ubuntu 22.04/24.04
  • SSH access to the server with sudo privilege. 

 

Update package index

To start with, update the package index on your Ubuntu 24.04. The Update process will refresh the catalog of available packages and make sure you're installing the newest version of Docker.

$ sudo apt update

Add Docker repository

Following the update of package indexes, the subsequent step involves incorporating the Docker repository into the system. To add the Docker repository, you should have a few pre-requisite packages in the system. Get these packages using apt. 

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

Add Docker’s official GPG key followed by adding the Docker repository to your system's software sources.

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Update the package index again.

$ sudo apt update

Configure installation source

Set up the Docker installation source to the official Docker repository instead of the Ubuntu repository. This will guarantee that you update the most recent Docker version on your system. To complete this step, execute the following command through the terminal. 

$ sudo apt-cache policy docker-ce

Install Docker

To install Docker community edition on Ubuntu,  use the following apt command. Check the status of docker and start it if it has not been started yet.

$ sudo apt install docker-ce
$ sudo systemctl status docker
$ sudo systemctl enable docker

Test the installation

To check if the Docker has been installed correctly, run the following command.

$ docker --version
Docker version 25.0.3, build 4debf41

Finally, run 'hello-world' Docker container to ensure everything is working correctly.

$ sudo docker run hello-world
Verify Docker installation
 

The command mentioned above will retrieve the 'hello-world' image from the Docker hub, initiate or run the container, and display the message 'Hello from Docker!'

Manage Docker as a Non-root User 

To run Docker without root privileges, add your non root user to the docker group using the following command:

$ sudo usermod -aG docker ${USER}

You can also substitute the current user name from ${USER} to any other user name in the above command with-

$ sudo usermod -aG docker user-name

Log out and log back in for this change to take effect.

Now verify if the user belongs to the Docker group.

$ id ${USER} 
Verify Docker installation
 

Your Ubuntu 24.04 system should now have Docker up and running.

Essential Docker commands

The Docker command set is extensive, requiring some time and effort to get the hang of. Therefore, let's start exploring and maximize Docker's capabilities with these essential commands.

Docker Help

To obtain assistance with Docker commands, run the docker help command.

$ docker --help

Additionally, you can also get help on Docker sub-commands with --help switch.

$ docker run --help

Docker Version

Display the installed docker version that you are working on -

$ docker version

Docker search

This command searches for specific images in the Docker hub and returns detailed information about the image name, description, stars, and whether official or non-official. Here is how you will use it –

$ docker search php

Docker Pull

As the name implies, this command pulls a specific image from the Docker hub. To pull an image, enter the image's name, either with or without a tag, using the docker pull command. You can omit a tag while pulling a docker image. In that case, the most recent version of the tag will be taken as the default tag.

$ docker pull nginx:1.21.0

Docker push

The docker push command is used to upload a Docker image to a specified Docker registry and allows you to share Docker images with others or make them available for deployment on other systems.

In order to push a Docker image, it's necessary to tag it with the right registry identifier. For example, if you want to push an image named "app1" to the Docker Hub, you would tag it as follows:

$ docker tag app1 your_registry_username/app1

Now push the image with the following docker command.

$ docker push your_registry_username/app1

Docker run

The docker run command is utilized to initiate and launch a fresh container using a Docker image.

$ docker run --env MYSQL_ROOT_PASSWORD=password --detach mysql

The above docker command will pull the MySQL image if it is not available on the local machine, create and run a container in detached mode after setting the environmental variable MYSQL_ROOT_PASSWORD.

Docker ps

The docker ps command is utilized to display a list of containers that are presently active on your system.
For instance, the docker ps command can show all the active containers, along with their information such as Container ID, Image, Command, Creation time, Status, Ports, and Names.

$ docker ps

To view the list of containers, including those that are both active and inactive, run the docker ps command with -a option.

$ docker ps -a

Docker stop

The docker stop command is used to stop one or more running containers by passing either the container name or ID. Here is how to do it -

$ docker stop container_name_or_id

Docker restart

The docker restart command is used to restart one or more running containers by passing either the container name or ID.

$ docker restart  container_name_or_id

Docker Kill

The docker kill command is used to terminate one or more active containers by force. Unlike the docker stop command where the container exits smoothly by leveraging the SIGTERM signal to the container's main process, the docker kill command uses the SIGKILL signal thereby terminating the container's process without allowing it to perform any cleanup.

$ docker restart  container_name_or_id

Docker exec

The Docker exec command runs an ad-hoc command in the container's shell under the hood and presents the results back to you in the terminal.

$ docker exec -it container_name_or_id bash
$ docker exec -d container_name_or_id ls -l /var/www

Docker login

This command is used to authenticate with a Docker registry or with any other registry to push and pull docker images. 

$ docker login

The above command will prompt you to provide user name and password interactively.

Docker logout

Once you complete your task after logging to the Docker registry, Use the docker logout command to logout from the docker registry.

$ docker logout

Docker history

The docker history command is used to view the history of a Docker image. This command displays information about each layer in the image, including the commands used to create each layer, the size of each layer, and the timestamp of creation.

$ docker history image_name

Docker logs

The Docker logs command allows you to retrieve and display logs in the standard output (stdout) and standard error (stderr) streams from a container.

$ docker logs container_name_or_id

Docker rmi

The docker rmi command is used to remove one or more Docker images from your local machine and lets you clean up the system by deleting images those are no longer needed or outdated.

$ docker rmi image_1 image_2

Docker copy

To copy files or directories to and from between a running container and the local filesystem,you can use the docker cp command. 

$ docker cp /local/path/from/file.txt container_name:/path/to/

The manual pages of the docker commands are an excellent source to learn docker commands by doing. For example, to invoke the help pages of the docker ps command use the --help switch along with the command.

$ docker ps --help

You may want to try out all the docker commands listed in the above section before delving into more advance docker commands.

Install Docker Compose(Optional)

Docker Compose is a tool that simplifies creation and running of multi-container applications in a Docker environment. Instead of running multiple and long Docker commands separately, docker compose facilitates creation of containers using a config file(yaml). It thus simplifies the process of creating, managing and deploying a complex application in a single config file. 

$ sudo apt install docker-compose-plugin -y

Verify the installation of docker compose with the following command.

$ docker-compose --version
docker-compose version 1.29.2, build unknown


 

 

Conclusion

In conclusion, installing Docker in an Ubuntu system is an easy procedure. Moreover, the docker commands listed in this post will enable you to get started with containerization features from CLI quickly.

fivestar_rating
Average: 4 (1 vote)
Comments