How to Install Docker on Linux?

Hello there, budding tech enthusiasts! Today, we're going to embark on an exciting journey into the world of Docker, focusing on how to install it on Linux systems. As your friendly neighborhood computer teacher, I'm here to guide you through this process step-by-step. Don't worry if you're new to programming – we'll start from the basics and work our way up. So, grab a cup of your favorite beverage, and let's dive in!

Docker - Installing on Linux

Prerequisites to Install Docker on Linux

Before we jump into the installation process, let's make sure we have everything we need. Think of this as packing for a camping trip – you wouldn't want to forget the tent, would you?

  1. A 64-bit version of one of these Linux distributions:

    • Ubuntu
    • Debian
    • Fedora
    • CentOS
    • RHEL
  2. A user account with sudo privileges

  3. A stable internet connection

  4. Basic familiarity with the terminal (Don't worry, we'll walk through the commands together!)

Now that we've got our virtual backpack ready, let's move on to the different ways we can install Docker.

Installing Docker using APT Repository

This method is like ordering your favorite pizza – it's quick, easy, and gets the job done efficiently. Let's break it down into steps:

Step 1: Update the package index

sudo apt-get update

This command refreshes the list of available packages. It's like checking the menu before ordering.

Step 2: Install packages to allow apt to use a repository over HTTPS

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

These packages are like the delivery guy's GPS – they help apt navigate securely to the Docker repository.

Step 3: Add Docker's official GPG key

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

This step is like checking the delivery guy's ID – it ensures we're getting the genuine Docker package.

Step 4: Set up the stable repository

echo \
  "deb [arch=amd64 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

Here, we're telling apt where to find Docker, like giving the delivery guy your exact address.

Step 5: Install Docker Engine

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

Finally, we install Docker. It's like receiving and unpacking your pizza!

Installing Docker From a Package

Sometimes, you might want to install Docker manually, perhaps in an environment without internet access. This is like making your own pizza from scratch – a bit more work, but you have more control.

  1. Go to https://download.docker.com/linux/ubuntu/dists/
  2. Choose your Ubuntu version
  3. Navigate to pool/stable/ and select the appropriate architecture (amd64, armhf, arm64, or s390x)
  4. Download the .deb files for the Docker Engine, CLI, containerd, and Docker Compose packages
  5. Install the packages using the following command:
sudo dpkg -i ./containerd.io_<version>_<arch>.deb \
  ./docker-ce_<version>_<arch>.deb \
  ./docker-ce-cli_<version>_<arch>.deb

Replace <version> and <arch> with the actual version and architecture of your downloaded files.

Installing Docker using Convenience Scripts

Docker provides a convenience script that automates the installation process. It's like having a personal chef who comes to your house and prepares the pizza for you!

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

This script detects your Linux distribution and sets up Docker for you. However, it's important to note that using convenience scripts in production environments is not recommended for security reasons.

How to Uninstall Docker Engine on Linux?

Sometimes, you might need to uninstall Docker. Don't worry, it's not as heartbreaking as throwing away a pizza – you can always install it again later!

  1. Uninstall the Docker Engine, CLI, and Containerd packages:
sudo apt-get purge docker-ce docker-ce-cli containerd.io
  1. Delete all images, containers, and volumes:
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

Conclusion

Congratulations! You've now learned multiple ways to install (and uninstall) Docker on Linux. Remember, like learning to make the perfect pizza, mastering Docker takes practice. Don't be afraid to experiment and try different methods to see what works best for you.

Here's a quick reference table of the methods we've covered:

Method Pros Cons
APT Repository Easy, automatic updates Requires internet connection
Package Installation Works offline, specific versions Manual updates
Convenience Script Quick, easy Not recommended for production

As we wrap up, I'd like to share a little story from my teaching experience. I once had a student who was terrified of using the terminal. He'd avoid it like a cat avoids water. But after walking him through Docker installation, he realized it wasn't so scary after all. In fact, he became so comfortable that he started joking about 'containing' his fear in a Docker image!

Remember, every expert was once a beginner. Keep practicing, stay curious, and before you know it, you'll be containerizing applications like a pro. Happy Dockering!

Credits: Image by storyset