In this article we will cover the topic of “How to install & Uninstall Docker on Ubuntu 20.04/22.04 LTS?” in step-by-step.

How to install Docker on Ubuntu 20.04/22.04 LTS?

Let’s start the installation of docker on ubuntu 20.04/22.04 LTS clearly.

Introduction:

At its core, Docker is an open-source platform that automates the process of developing, shipping, and running applications in containers. But what exactly are containers, and why are they so significant?

Containers are lightweight, standalone, and executable packages that contain everything needed to run a piece of software, including the code, runtime, libraries, and system tools. Unlike traditional virtual machines (VMs), containers share the host operating system’s kernel, making them incredibly efficient and fast. Docker uses containerization technology to create and manage these containers.

Prerequisites

Before we begin, ensure that you have the following:

  1. A system running Ubuntu 20.04/22.04 LTS.
  2. A user account with sudo privileges (to execute administrative commands).

Steps of to install Docker on Ubuntu 20.04/22.04 LTS

Let’s start the guide for “How to install Docker on Ubuntu 20.04/22.04 LTS?”

Step #1: Update Package Lists

Open a terminal window and update the package lists to ensure you have the latest information about available packages:

ubuntu@Rushi-InfoTech:~$ sudo apt-get update

Step #2: Install docker on Ubuntu using curl

  • Download the script.
ubuntu@Rushi-InfoTech:~$ curl -fsSL https://get.docker.com -o install-docker.sh
  • Run the script with –dry-run to verify the steps it executes.
ubuntu@Rushi-InfoTech:~$ sh install-docker.sh --dry-run
  • Run the script either as root, or using sudo to perform the installation.
ubuntu@Rushi-InfoTech:~$ sudo sh install-docker.sh
  • Verify the installation of Docker on Ubuntu server.
ubuntu@Rushi-InfoTech:~$ docker --version
  • Once installed verify Docker Service status.
ubuntu@Rushi-InfoTech:~$ sudo systemctl status docker

Running Docker commands without sudo

By default, Docker requires administrator privileges, Docker group is created when during the installation of Docker packages.

  • Add your system user to docker group.
ubuntu@Rushi-InfoTech:~$ sudo usermod -aG docker $USER

Step #3: Docker Command-Line

Before moving to Docker Images, Let’s see Docker commands. Before are docker command syntax.

ubuntu@Rushi-InfoTech:~$ docker [option] [command] [arguments]
  • Run docker command to view all available subcommads.
ubuntu@Rushi-InfoTech:~$ docker

Commands:

ubuntu@Rushi-InfoTech:~$ docker

Usage:  docker [OPTIONS] COMMAND

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes


Run 'docker COMMAND --help' for more information on a command.

For more help on how to use Docker, head to https://docs.docker.com/go/guides/

Step #4: Working with Docker Images

Once docker is installed, Lets test docker by testing docker images

Docker images are required for establishing Docker containers. These images can be used for linking to any of the Docking environment.

Before running docker images, check by accessing hello-world image from Docker Hub

ubuntu@Rushi-InfoTech:~$ docker run hello-world

Error:

docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/create: dial unix /var/run/docker.sock: connect: permission denied.

Error

Change the docker.sock permission

ubuntu@Rushi-InfoTech:~$ sudo chmod 666 /var/run/docker.sock
  • Download the base image to your server.
ubuntu@Rushi-InfoTech:~$ docker pull ubuntu
  • This command downloads an image to your server from docker registry/DockerHub.
ubuntu@Rushi-InfoTech:~$ docker images
  • You will see Ubuntu images is downloaded.

Remove Docker Images

Syntax:

ubuntu@Rushi-InfoTech:~$ docker rmi <REPOSITRY Name /IMAGE ID >

Example:

ubuntu@Rushi-InfoTech:~$ docker rmi ubuntu

Step #5: Working with Docker Container

Docker Containers are lightweight and it uses the host OS and it included software package that consists of all the dependencies required to run an application.

Lets launch container with Ubuntu docker image.

ubuntu@Rushi-InfoTech:~$ docker run -it ubuntu

-i  –                                  To start an interactive session.
-t  –                                 Allocates a tty and attaches stdin and stdout.
ubuntu–                       Docker image that is used to create the container.

Note- The container will stop when you leave it with the command exit.
OR

To exit from docker container type CTRL + P + Q. This will leave container running in background an provide you host system console.

  • To verify Docker Container running in background.
ubuntu@Rushi-InfoTech:~$ docker ps
  • Now Run Docker Container in background. If you like to have a container that is running in the background, you just need to add the -d option in the command
ubuntu@Rushi-InfoTech:~$ docker run -itd ubuntu
  • To list all Docker Containers including stopped.
ubuntu@Rushi-InfoTech:~$ docker ps -a
  • To rename Docker Container.
ubuntu@Rushi-InfoTech:~$ docker rename <Old_Name> <New_Name>
  • To Start/Stop Container.
ubuntu@Rushi-InfoTech:~$ docker stop <CONTAINER ID>
ubuntu@Rushi-InfoTech:~$ docker start <CONTAINER ID>
  • To remove the Docker Container, stop it first and then remove it.
ubuntu@Rushi-InfoTech:~$ docker rm <CONTAINER ID>
  • Run below command to remove all stopped containers.
ubuntu@Rushi-InfoTech:~$ sudo docker rm -f $(sudo docker ps -a -q)

Uninstall Docker on Ubuntu 20.04/22.04 LTS

Let’s see the steps to uninstall docker on ubuntu 20.04/22.04 LTS.

Introduction:

To uninstall Docker from your Ubuntu system, you’ll need to remove the Docker packages and associated configurations. Here are the steps to uninstall Docker:

Note: These instructions will remove Docker completely, including all containers and images. Be sure to backup any data you want to keep from your Docker containers before proceeding.

Steps to Uninstall Docker on ubuntu 20.04/22.04 LTS

Step #1: Stop and Disable Docker

First, stop the Docker service and disable it from starting on boot:

ubuntu@Rushi-InfoTech:~$ sudo systemctl stop docker
ubuntu@Rushi-InfoTech:~$ sudo systemctl disable docker

Step #2: Remove Docker Packages

To remove Docker packages, including docker-ce, docker-ce-cli, and containerd.io, you can use the following command:

ubuntu@Rushi-InfoTech:~$ sudo apt purge -y docker-ce docker-ce-cli containerd.io

Step #3: Remove Docker Files and Directories

To remove any Docker-related files and directories (including containers and images), you can use the following commands:

ubuntu@Rushi-InfoTech:~$ sudo rm -rf /var/lib/docker
ubuntu@Rushi-InfoTech:~$ sudo rm -rf /etc/docker

Step #4: Remove Docker Group (Optional)

If you previously added your user to the docker group, and you no longer plan to use Docker, you can remove the group:

ubuntu@Rushi-InfoTech:~$ sudo deluser <your-username> docker

Replace <your-username> with your actual username.

Step #5: Verify the Docker on server

Please verify the presence of Docker on Ubuntu 20.04/22.04 LTS.

ubuntu@Rushi-InfoTech:~$ docker --version

Conclusion

In this blog article provided a comprehensive guide on how to both install and uninstall Docker on Ubuntu 20.04 and 22.04 LTS, two popular long-term support versions of the Ubuntu operating system. Docker is a powerful containerization platform that simplifies application management and deployment.

From this article we have learnt “How to install & Uninstall Docker on Ubuntu 20.04/22.04 LTS?” in detail.

For Reference:

Please visit the official website of Docker for reference of installation.

Any queries pls contact us
https://rushiinfotech.in/contact/

Similar Posts

Leave a Reply

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