DEV Community

Cover image for Get Started with Docker: How to Install Docker on Ubuntu 22.04
Aditi Bindal for NodeShift

Posted on

Get Started with Docker: How to Install Docker on Ubuntu 22.04

Docker has revolutionized how we build, ship, and run applications, making it a must-have tool for developers and organizations. By leveraging the concept of Containerization, Docker ensures that your apps run consistently in different environments, eliminating the "it works on my machine" problem. As an open-source tool, it has become an industry standard for containerization, with its ever-increasing adoption in cloud-native, DevOps, and microservices.

Image-intro

That said, we have created this comprehensive guide to walk you through the process of installing Docker on Ubuntu 22.04. Whether you're already experienced or just starting your way into containerized environments, this article will help you set up Docker so that you can unleash its full potential. By the end, you'll be ready to start effortlessly creating, managing, and deploying containers.

Prerequisites

  • A Virtual Machine (such as the ones provided by NodeShift) with:

    • 2 vCPUs
    • 2 GB RAM
    • 10 GB SSD
  • Ubuntu 22.04 VM

Note: The above prerequisites are highly variable across use cases. One could use a high-end configuration for a large-scale deployment.

Step-by-step process to install Docker on Ubuntu 22.04

For this tutorial, we'll use a CPU-powered Virtual Machine by NodeShift, which provides high-compute Virtual Machines at a very affordable cost on a scale that meets GDPR, SOC2, and ISO27001 requirements. Also, it offers an intuitive and user-friendly interface, making it easier for beginners to get started with Cloud deployments. However, feel free to use any cloud provider you choose and follow the same steps for the rest of the tutorial.

Step 1: Setting up a NodeShift Account

Visit app.nodeshift.com and create an account by filling in basic details, or continue signing up with your Google/GitHub account.

If you already have an account, login straight to your dashboard.

Image1

Step 2: Create a Compute Node (CPU Virtual Machine)

After accessing your account, you should see a dashboard (see image), now:

  1. Navigate to the menu on the left side.

  2. Click on the Compute Nodes option.

    Image2

  3. Click on Start to start creating your very first compute node.

    Image3

These Compute nodes are CPU powered virtual machines by NodeShift. These nodes are highly customizable, and let you control different environmental configurations, such as CPUs, RAM and storage, according to your needs.

Step 3: Select configuration for VM

  1. The first option you see is the Reliability dropdown, which lets you choose the type of uptime guarantee level you're seeking for your VM (e.g., 99%).

    illustration4

  2. Next, select a geographical region from the Region dropdown, where you want to launch your VM (e.g. United States).

    illustration5

  3. Now, most importantly, select the right specifications for your VM according to your use-case by sliding the bars for each option.
    illustration6

Step 4: Choose VM Configuration and Image

  1. After you select your required configuration options, you'll see the available VMs in your region and as per (or very close to) your configuration. In our case, we'll choose a '4 vCPUs/4GB/80GB SSD' Compute node as the best possible match.

  2. Next, you'll need to choose an image for your Virtual Machine. For the scope of this tutorial, we'll select Ubuntu, as we will deploy Docker on Ubuntu 22.04 LTS.

    illustration of step 4

Step 5: Choose the Billing cycle and Authentication Method

  1. For the billing cycle, two options are available: Hourly being ideal for short-term usage, offering pay-as-you-go flexibility, and Monthly being best for long-term projects with consistent usage rate and potentially lower cost.

    illustration for step 5

  2. Next, you'll need to select an authentication method. There are two methods available: Password and SSH Key. We recommend using SSH keys as they are more secure option. In order to create one, head over to our official documentation.

    illustration for step 5

Step 6: Finalize Details and Create Deployment

Finally, you can also add a VPC (Virtual Private Cloud), which provides an isolated section to launch your cloud resources (Virtual machine, storage, etc.) in a secure, private environment. We're keeping this option as default for now, but feel free to create a VPC as per your needs.

Also, you can deploy multiple nodes at once by clicking + in the Quantity option.

illustration for step 6

That's it! Now, you are ready to deploy the node. Finalize the configuration summary, if it looks good, go ahead and click Create to deploy the node.

illustration for step 6

Step 7: Connect to active Compute Node using SSH

As soon as you create the node, it will be deployed in seconds or minutes. Once deployed, you will see a status Running in green, meaning that our Compute node is ready to use!

illustration for step 7

Once your node shows this status, follow the below steps to connect to the VM via SSH:
1) Open your terminal and run the below SSH command:
(replace root with your username and paste the IP of your VM in place of ip after copying it from the NodeShift dashboard)

ssh root@ip
Enter fullscreen mode Exit fullscreen mode

2) The terminal will authenticate automatically if SSH keys are set up.
3) In some cases, the terminal may take your consent before connecting; enter 'yes', and you should be connected.

illustration for step 7

Step 8: Install Dependencies

Before we install Docker, we need to install some required dependencies.

1) Let's start by updating the Ubuntu package source list for the latest version and security updates

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step8-1

2) Install the dependency packages

sudo apt install apt-transport-https ca-certificates curl software-properties-common 
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step8-2

  • apt-transport-https: Allows apt to download packages over HTTPS, ensuring secure communication when fetching Docker packages.
  • ca-certificates: Provides trusted CA certificates to validate the authenticity of Docker's HTTPS endpoints.
  • curl: A command-line tool used to fetch Docker's GPG keys and verify repository integrity during the installation process.
  • software-properties-common: Provides tools to manage apt repositories, enabling you to add Docker's official repository to the package manager.

Step 9: Install Docker

1) Use curl to add the GPG key for the Docker repository

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

Output:

Image-step9-1

2) Add Docker's APT repository to the system's source list

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
Enter fullscreen mode Exit fullscreen mode

3) Update the package source list again

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step9-2

4) Install Docker

sudo apt install docker-ce -y
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step9-3

5) Verify if the Docker is correctly installed and the service is running

sudo systemctl status docker
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step9-4

Step 10: Working with Docker Images

Docker images are like templates for creating containers. They contain everything needed to run an application, including the code, runtime, libraries, and dependencies. You can pull images from Docker Hub or create your own for specific applications.

Docker Hub is an online library containing pre-built docker images you can use or upload your own for others to use.

1) Let's run a simple test container named hello-world as a warm-up and see if Docker is running correctly

docker run hello-world
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step10-1

As you can see above, once we hit the run command, Docker tries to look for hello-world image locally; if it can't find the image, it pulls the image from the Docker Hub and creates the container from that image.

After displaying the output to the terminal, the container exits and stops running.

2) To see the current downloaded images on the system

docker images
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step10-2

3) Let's see how to download a new image in the system
a) Search the image on Docker Hub

(replace kubernetes with the image you want to search)

docker search kubernetes
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step10-3

b) Download an image
Enter fullscreen mode Exit fullscreen mode

Now, when we search kubernetes, the hub shows all the available docker images related to Kubernetes. Let's go ahead and download one of them, e.g. gofunky/kubernetes

docker pull gofunky/kubernetes
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step10-4

c) Verify the downloaded image
Enter fullscreen mode Exit fullscreen mode

Log the image list again to check the updated list with our newly downloaded image.

docker images
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step10-5

Our Docker image has been successfully downloaded to the system! Feel free to try pulling new images by following the same steps.

Step 11: Working with Docker Containers

Now, let's try running some basic commands that can get you started working with Docker containers.

1) Create a container
By now, you may have already understood that Docker creates containers using locally downloaded Docker images.
So, let's try creating a container using our newly downloaded gofunky/kubernetes image.

docker run -d  -p 8080:80 gofunky/kubernetes
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step11-1

The output is the container ID in alphanumeric form, confirming that our container has successfully been created and assigned an ID.

2) To log the details of the last created container

docker ps -l
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step11-2

3) To view the list of current active/inactive containers

docker ps -a
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step11-3

4) In case you want to remove a container

(replace <container_id> with the ID of the container you want to remove, as shown in the list in the previous step)

docker rm <container_id>
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step11-4

4) Verify if the container is deleted

Log the container list again to check if the container has been removed.

docker ps -a
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step11-5

Note: If you want to remove a Docker image, you must remove the container built with that image before removing the image itself. Since we have already removed gofunky/kubernetes container, let's try removing the image as well.

To remove an image:

(replace <image_id> with the ID of the respective image from the image list)

docker rmi <image_id>
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step11-6

Verify if the image has been deleted by logging the image list:

docker images
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step11-7

As you can see, the image has been successfully removed from the system.

Conclusion

This guide covers the step-by-step approach for setting up Docker on Ubuntu 22.04. We also covered basic instructions on working with Docker images and containers. Following the steps outlined, you have a robust environment ready for your operations. Furthermore, we deployed our VM on NodeShift, which helped streamline the deployment process and made the set-up easier with its developer-friendly interface. Whether you're building applications for your projects or deploying at scale in the cloud, NodeShift offers customized and affordable solutions to optimize your workflows and enhance resource efficiency for your development pipelines.

For more information about NodeShift:

Top comments (0)