How to Install and Use Docker on Ubuntu 22.04

How to Install and Use Docker on Ubuntu 22.04

Docker is an open-source containerization platform that allows developers to easily package and deploy applications in a portable and lightweight manner. In this tutorial, we will provide a step-by-step guide on how to install and use Docker on Ubuntu 22.04.


Prerequisites

Before you proceed, make sure you have Ubuntu 22.04 installed on your system and have a user account with sudo privileges.

How to Install Docker on Ubuntu

To install Docker on Ubuntu 22.04, follow the steps below:

Update Package Manager

The first step is to update the package manager to ensure that you have the latest packages installed on your Ubuntu 22.04 machine. You can do this by running the following command:

sudo apt update && sudo apt upgrade

Install Dependencies

Docker requires a few dependencies to be installed on your system before it can be installed. You can install these dependencies using the following command:

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

Import GPG Key

Next, we need to import Docker’s GPG key to verify the integrity of the package we will download. Run the following curl command to import the key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Add Docker Repository

We need to add the Docker repository to APT sources so that we can install it using the package manager. Run the following command to add the repository:

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

Install Docker

Now that we have added the Docker repository, we can install Docker using the package manager. Run the following command to install Docker:

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io

Install a Specific Version of Docker

To install a specific version of Docker, first check the available versions by running the following command:

sudo apt update
apt list -a docker-ce

This will display a list of available Docker versions. Choose the version you want to install and run the following command, replacing VERSION_STRING with the version you want to install:

sudo apt install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

Verify Docker Installation

After installing Docker, you can verify the installation by running the following command:

sudo docker run hello-world

This command will download a test image and run it in a container. If everything is installed correctly, you will see a message that says “Hello from Docker!“.

After completing the installation of Docker on Ubuntu 20.04, the Docker service should start automatically. You can verify this by running the following command:

sudo systemctl status docker

If the Docker service is running, you should see output similar to the following:

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-09-16 15:48:14 UTC; 3s ago
       Docs: https://docs.docker.com
   Main PID: 1662 (dockerd)
      Tasks: 9
     Memory: 49.5M
     CGroup: /system.slice/docker.service
             └─1662 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

If the Docker service is not running, you can start it by running the following command:

sudo systemctl start docker

You can also enable the Docker service to start automatically at boot time by running the following command:

sudo systemctl enable docker

This will ensure that the Docker service starts automatically whenever your Ubuntu 20.04 machine is rebooted.

Check Version

After the installation is complete, you can check the version of Docker that is installed by running the following command:

docker -v

How to Execute Docker Command without Sudo

By default, the Docker command can only be executed with sudo privileges. However, you can add your user to the docker group to run the command without sudo. Run the following command to add your user to the docker group:

sudo usermod -aG docker $USER

After adding your user to the docker group, log out and log back in to apply the changes.


How to Use Docker Command

Now that you have installed Docker, you can start using it to run containers. The basic syntax for the Docker command is:

docker [option] [subcommand] [arguments]

Here’s a brief explanation of each component of the syntax:

  • docker: This is the command itself. You will use this to interact with the Docker daemon.
  • [option]: These are optional flags that modify the behavior of the Docker command. Some common options include -d to run a container in the background and -p to publish a container’s port to the host.
  • [subcommand]: These are specific actions that you can take with Docker. Some common subcommands include run to start a new container, ps to list running containers, and stop to stop a running container.
  • [arguments]: These are additional arguments that you can provide to the Docker command. The specific arguments that you provide will depend on the subcommand that you are using.

Here are a few examples of how you might use the Docker command:

To run a Docker container, use the following command:

docker run -d -p 80:80 nginx

This command will start a new container running the NGINX web server in the background (-d) and publish port 80 on the container to port 80 on the host (-p 80:80).

To list all of the running containers on your system, run the following command:

docker ps

This command will list all of the running containers on your system.

To stop the running container, use the following command:

docker stop CONTAINER_ID

This command will stop the container with the specified ID. You can find the ID by running docker ps.

Certainly! You can get a list of all available Docker subcommands by running the following command in your terminal:

docker

This will display a list of all the available subcommands that you can use with Docker.


How to Upgrade Docker

To upgrade Docker to the latest version, simply run the following commands:

sudo apt update
sudo apt upgrade docker-ce docker-ce-cli containerd.io

How to Uninstalling Docker

Before uninstalling Docker, it’s a good idea to remove all containers, images, volumes, and networks to avoid leaving behind any unused objects. To do this, you can run the following commands in your terminal:

docker container stop $(docker container ls -aq)

This will stop all running containers.

docker system prune -a --volumes

This will remove all unused objects including stopped containers, unused images, and volumes. The -a flag indicates that all unused objects should be removed, and the --volumes flag ensures that all unused volumes are also removed.

Now you can uninstall Docker by run the following command:

sudo apt purge docker-ce
sudo apt autoremove

Conclusion

Docker is a powerful tool for containerizing and deploying applications. With this guide, you should now be able to install and use Docker on Ubuntu 22.04. By following the steps outlined above, you can get up and running with Docker in no time. Remember to always check the Docker documentation for more information on how to use Docker effectively. Happy containerizing!

Got questions or suggestions on the article? Don’t hesitate to leave a comment below. Your feedback is valuable to us and helps improve the content quality. And don’t forget to share this article with others who might find it useful. Looking forward to hearing from you!

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top