Install Docker on Ubuntu Server 24.04: A Step-by-Step Guide

Docker has revolutionized the way applications are developed, deployed, and managed. This powerful platform enables you to package applications and their dependencies into containers, ensuring consistency across different environments. If you’re using Ubuntu Server 24.04, the latest Long-Term Support (LTS) release, installing Docker is a crucial first step to leveraging containerization.

This comprehensive guide will walk you through the process of installing Docker on Ubuntu 24.04, ensuring a smooth and efficient setup. We’ll cover everything from updating your system to verifying your installation and running your first containers. Whether you are a seasoned developer or just starting with Docker, this tutorial will provide you with the knowledge and steps to get Docker up and running on your Ubuntu server.

What is Docker?

Docker is an open-source platform that utilizes containerization to streamline the process of building, shipping, and running applications. Imagine containers as lightweight, standalone, and executable packages that include everything needed to run a piece of software: code, runtime, system tools, libraries, and settings. This containerization technology is built upon OS-level virtualization, making it efficient and resource-friendly.

The beauty of Docker lies in its ability to ensure consistency across different computing environments. Whether you’re developing on a local machine, testing in a staging environment, or deploying to a production server (be it Linux, Windows, or macOS), Docker guarantees that your applications will run reliably and predictably every time. This eliminates the common “it works on my machine” problem and significantly simplifies the deployment pipeline.

Key Benefits of Using Docker:

  • Consistency: Docker containers ensure applications run the same way regardless of the environment.
  • Isolation: Containers isolate applications from each other and the host system, enhancing security and stability.
  • Portability: Docker containers can be easily moved between different environments, from local development to cloud deployments.
  • Efficiency: Containers share the host OS kernel, making them lightweight and faster to start compared to virtual machines.
  • Scalability: Docker facilitates easy scaling of applications by allowing you to quickly spin up multiple containers.
  • Resource Optimization: Docker optimizes resource utilization by allowing multiple containers to run on a single host.

Docker is widely adopted for various use cases, including microservices architectures, continuous integration and continuous delivery (CI/CD) pipelines, and simplifying the deployment of complex applications. By installing Docker on your Ubuntu 24.04 server, you are unlocking a world of possibilities for modern application management.

Ready to explore the power of Docker? Let’s proceed with the installation process on your Ubuntu 24.04 server.

Prerequisites

Before you begin installing Docker, ensure you have the following prerequisites in place:

  • Ubuntu 24.04 Server: You will need a running instance of Ubuntu Server 24.04. For optimal performance, it’s recommended to have a server with at least 1GB of RAM and 1 CPU core.
  • SSH Access: You should have SSH access to your Ubuntu 24.04 server to execute commands remotely.
  • Sudo User: A non-root user with sudo privileges is required to perform administrative tasks, including installing software packages.
  • Internet Connection: An active internet connection is necessary to download Docker packages and dependencies from the official repositories.

Once you have these prerequisites in place, you are ready to proceed with the Docker installation steps.

Step-by-Step Installation Guide

The following steps will guide you through installing Docker on Ubuntu 24.04. We will be installing Docker Community Edition (CE) from Docker’s official repository to ensure you get the latest and most stable version.

Step 1. Update System Packages

It’s always a good practice to start by updating your system’s package index. This ensures that you have the latest package information and dependencies. Open your terminal and run the following command:

sudo apt update

This command will refresh the package lists for upgrades and new installations. Wait for the process to complete before moving to the next step.

Step 2. Install Required Dependencies

To facilitate the installation process and ensure proper functionality, you need to install a few dependency packages. These packages are essential for adding and using Docker’s official repository. Run the following command to install them:

sudo apt install curl apt-transport-https ca-certificates software-properties-common
  • curl: A tool for transferring data with URLs, used to download the Docker GPG key.
  • apt-transport-https: Allows APT to access repositories over HTTPS.
  • ca-certificates: Installs CA certificates, allowing HTTPS connections to be verified.
  • software-properties-common: Provides scripts to manage software repositories and distributions.

This command will install these utilities, which are crucial for adding the Docker repository in the subsequent steps.

Step 3. Add Docker GPG Key

To ensure the authenticity and integrity of the Docker packages, you need to add Docker’s GPG key to your system. This key is used to verify that the packages you download are indeed from Docker and haven’t been tampered with. Use the following curl command to download and add the GPG key:

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

This command does the following:

  • curl -fsSL https://download.docker.com/linux/ubuntu/gpg: Downloads the Docker GPG key from Docker’s official website.
  • sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg: Passes the downloaded key to gpg to dearmor it and saves it to /usr/share/keyrings/docker-archive-keyring.gpg. This location is a standard place to store keyring files for APT repositories.

Adding the GPG key is a critical security measure to ensure you are installing genuine Docker packages.

Step 4. Add Docker Repository

Now that you have added the GPG key, you need to add the official Docker APT repository to your system’s sources list. This tells APT where to find the Docker packages. Execute the following command to add the repository:

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

Let’s break down this command:

  • 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": This part constructs the repository entry.
    • deb: Indicates a Debian package repository.
    • [arch=$(dpkg --print-architecture)]: Specifies the architecture (e.g., amd64, arm64).
    • signed-by=/usr/share/keyrings/docker-archive-keyring.gpg: Specifies the GPG key file used to sign the repository.
    • https://download.docker.com/linux/ubuntu: The URL for the Docker Ubuntu repository.
    • $(lsb_release -cs): Detects your Ubuntu distribution codename (e.g., jammy for 24.04).
    • stable: Specifies the stable repository channel.
  • sudo tee /etc/apt/sources.list.d/docker.list > /dev/null: This uses tee to write the constructed repository entry to a new file named docker.list in the /etc/apt/sources.list.d/ directory. The > /dev/null part suppresses the output of the echo command to the terminal.

By adding the official Docker repository, you ensure that you are installing Docker packages directly from Docker, guaranteeing the latest stable versions and updates.

Step 5. Install Docker Engine

With the Docker repository added to your system, you can now install Docker Engine, which includes the Docker daemon (dockerd), the Docker CLI (docker), and other essential components. Update the package index again to include the newly added repository, and then install Docker Engine:

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
  • sudo apt update: Refreshes the package lists to include the Docker repository.
  • sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y: Installs the Docker packages:
    • docker-ce: Docker Community Edition Engine.
    • docker-ce-cli: Docker CLI (command-line interface).
    • containerd.io: Container runtime.
    • docker-buildx-plugin: Docker Buildx plugin.
    • docker-compose-plugin: Docker Compose plugin.
    • -y: Automatically answers “yes” to prompts during installation.

The Docker service should start automatically after installation.

Step 6. Verify Docker Installation

To ensure that Docker has been installed successfully, verify the status of the Docker service. Run the following command:

sudo systemctl status docker

This command will display the status of the Docker service. If Docker is installed and running correctly, you should see output similar to this, indicating that the service is active (running):

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2024-08-08 10:00:00 UTC; 5min ago
       Docs: https://docs.docker.com
   Main PID: 1234 (dockerd)
      Tasks: 8
     Memory: 50.0M
        CPU: 1.200s
     CGroup: /system.slice/docker.service
             └─1234 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Alt text: Docker service status showing as active and running on Ubuntu 24.04

A green active (running) status confirms that Docker Engine is up and running on your Ubuntu 24.04 server.

Step 7. Manage Docker as a Non-Root User

By default, Docker commands can only be run by the root user or a user with sudo privileges. Running Docker commands with sudo every time can be cumbersome. A more convenient approach is to add your user to the docker group. This allows you to run Docker commands without sudo.

To add your current user to the docker group, execute the following command:

sudo usermod -aG docker $USER
  • sudo usermod -aG docker $USER: This command modifies the user account.
    • usermod: Command for modifying user account information.
    • -aG docker: Adds the user to the docker group. -a ensures that you are adding to the group, not replacing the existing groups, and -G docker specifies the docker group.
    • $USER: A variable that represents your current username.

After adding your user to the docker group, you need to apply the changes to your current session. You can do this by running the newgrp command:

newgrp docker

Alternatively, you can log out and log back in to your server to apply the group changes.

To verify that your user is now a member of the docker group, use the groups command followed by your username:

groups $USER

You should see docker listed among the groups your user belongs to in the output:

cherry sudo docker

Alt text: Verifying user membership in the docker group on Ubuntu 24.04 using the groups command

Now, try running Docker commands without sudo. For example, check the Docker version:

docker version

You should see the Docker version information displayed without any permission errors:

Client: Docker Engine - Community
 Version:           26.1.3
 API version:       1.46
 Go version:        go1.22.3
 Git commit:        29cf465
 Built:             Wed Jun 19 21:55:27 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          26.1.3
  API version:      1.46 (minimum version 1.24)
  Go version:       go1.22.3
  Git commit:       a7b1d33
  Built:            Wed Jun 19 21:55:00 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.30
  GitCommit:        5df44654410b5df3efc3edbc3834c5b951f6b09b
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Alt text: Output of docker version command confirming successful Docker installation on Ubuntu 24.04

This confirms that you can now run Docker commands as a non-root user, making your Docker workflow more convenient.

Step 8. Test Docker Installation with Hello-World

To further validate your Docker installation, run a simple test container using the hello-world image. This image is designed to verify that Docker is working correctly and can pull images from Docker Hub, the default public registry for Docker images. Execute the following command:

docker run hello-world

If your Docker installation is working correctly, you will see output similar to this in your terminal:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
abcd
Digest: sha256:e07ee1baac5fae6a26f2c3dc3577f1969bb549c4ef87ecbea646353ed53ba658
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can deploy a web application by following the steps in the documentation:
 https://docs.docker.com/get-started-web-apps/

Alt text: Output of docker run hello-world command confirming successful Docker installation and image pull from Docker Hub on Ubuntu 24.04

This output indicates that:

  1. Your Docker client successfully communicated with the Docker daemon.
  2. The Docker daemon pulled the hello-world image from Docker Hub.
  3. Docker created and ran a container from the hello-world image.
  4. The container printed the “Hello from Docker!” message to your terminal.

Congratulations! You have successfully installed and verified Docker on your Ubuntu 24.04 server.

Basic Docker Commands

Now that Docker is installed, let’s explore some fundamental Docker commands to get you started with managing images and containers. Docker commands follow this basic syntax:

docker [OPTIONS] COMMAND [ARG...]

To see a list of available Docker commands, simply type docker in your terminal without any options:

docker

This will display a comprehensive list of Docker commands along with their descriptions. Here are some of the most commonly used commands:

  • docker search <image_name>: Searches for Docker images on Docker Hub. For example, to search for Nginx images:

    docker search nginx

    Alt text: Output of docker search nginx command showing search results for Nginx images on Docker Hub

  • docker pull <image_name>: Downloads a Docker image from Docker Hub to your local machine. To pull the official Nginx image:

    docker pull nginx
  • docker images: Lists Docker images that are currently stored on your local machine.

    docker images

    Alt text: Output of docker images command listing locally available Docker images on Ubuntu 24.04

  • docker run [OPTIONS] <image_name>: Creates and starts a container from a Docker image. To run an Nginx container, mapping port 8080 on the host to port 80 in the container and running it in detached mode:

    docker run -d -p 8080:80 nginx
  • docker ps: Lists currently running Docker containers.

    docker ps
  • docker ps -a: Lists all Docker containers, including stopped ones.

    docker ps -a

    Alt text: Output of docker ps and docker ps -a commands listing running and all Docker containers on Ubuntu 24.04

  • docker stop <container_id> or docker stop <container_name>: Stops a running Docker container.

    docker stop <container_id>
  • docker rm <container_id> or docker rm <container_name>: Removes a stopped Docker container.

    docker rm <container_id>

    Alt text: Output of docker rm command after removing a Docker container on Ubuntu 24.04

  • docker rmi <image_id> or docker rmi <image_name>: Removes a Docker image from your local machine.

    docker rmi <image_name>

    Alt text: Output of docker rmi command after removing a Docker image on Ubuntu 24.04

These are just a few of the essential Docker commands. As you delve deeper into Docker, you will discover a wide range of commands and options to manage your containers and images effectively.

Conclusion

This tutorial provided a detailed guide on how to install Docker on Ubuntu 24.04. By following these step-by-step instructions, you have successfully set up Docker Engine on your server and learned how to verify your installation. You are now equipped with the foundation to start containerizing your applications and leveraging the benefits of Docker’s powerful platform.

From here, you can explore more advanced Docker concepts such as Docker Compose for multi-container applications, Docker Swarm or Kubernetes for container orchestration, and Dockerfiles for building custom images. Docker opens up a vast ecosystem of tools and technologies to streamline your development and deployment workflows. Start experimenting with Docker and unlock the potential of containerization for your projects on Ubuntu 24.04!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *