DEV Community

Chandrashekhar Kachawa
Chandrashekhar Kachawa

Posted on

A Developer's Guide to Setting Up Docker on Linux

Docker has revolutionized the way we build, ship, and run applications. By containerizing applications, it provides a consistent environment from development to production, eliminating the classic "it works on my machine" problem. For developers, especially on Linux, Docker is an indispensable tool. This guide will walk you through the process of installing and setting up Docker on a Linux system, focusing on Ubuntu/Debian-based distributions.

Why Docker?

Before we dive in, let's quickly recap why Docker is so powerful:

  • Consistency: Ensures your application runs the same way everywhere.
  • Isolation: Containers run in isolated environments, preventing conflicts between dependencies.
  • Portability: Docker containers can run on any machine that has Docker installed, regardless of the underlying OS.
  • Efficiency: Containers are lightweight and start up quickly compared to traditional virtual machines.

Step 1: Preparing Your System

First, it's always a good practice to update your package list to ensure you're getting the latest versions of software.

sudo apt update
sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Next, install a few prerequisite packages that allow apt to use a repository over HTTPS. These packages are crucial for securely fetching Docker packages:

  • apt-transport-https: Enables apt to retrieve packages over the HTTPS protocol, ensuring secure communication.
  • ca-certificates: Allows the system to verify security certificates from remote hosts, crucial for trust when downloading packages.
  • curl: A command-line tool for transferring data with URLs, used here to download Docker's GPG key.
  • gnupg: GNU Privacy Guard, a tool for secure communication and data storage, used to manage and verify the GPG key.
  • lsb-release: Provides information about the Linux Standard Base (LSB) distribution, which helps in identifying the correct Docker repository for your system.
sudo apt install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
Enter fullscreen mode Exit fullscreen mode

Step 2: Adding Docker’s Official GPG Key

To ensure the Docker packages you download are authentic, 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
Enter fullscreen mode Exit fullscreen mode

This command downloads the key and saves it in a location where apt can find it.

Step 3: Setting Up the Stable Repository

Now, set up the stable Docker repository so you can install Docker from it. This ensures you get the official, stable release.

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

Step 4: Installing Docker Engine

With the repository set up, you can now install Docker Engine.

First, update the apt package index again, then install the latest version of Docker Engine, containerd, and Docker Compose.

sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
Enter fullscreen mode Exit fullscreen mode

docker-ce is the Community Edition of Docker, which is free to use.

Step 5: Verifying the Installation

To verify that Docker has been installed correctly, you can run the classic hello-world image.

sudo docker run hello-world
Enter fullscreen mode Exit fullscreen mode

If the installation was successful, you'll see a message starting with "Hello from Docker!". This confirms that your Docker setup is working.

Step 6: Managing Docker as a Non-Root User (Optional but Recommended)

By default, the Docker daemon binds to a Unix socket instead of a TCP port. This socket is owned by the root user, and other users can only access it using sudo.

To run Docker commands without sudo, you need to add your user to the docker group.

  1. Create the docker group (if it doesn't already exist):

    sudo groupadd docker
    
  2. Add your user to the docker group:

    sudo usermod -aG docker ${USER}
    
  3. Apply the new group membership:

    For the changes to take effect, you need to log out and log back in, or you can run the following command:

    newgrp docker
    

    After this, you should be able to run Docker commands without sudo.

    docker run hello-world
    

Conclusion

You now have a fully functional Docker setup on your Linux machine. This opens up a world of possibilities for streamlining your development workflow, from creating isolated development environments to deploying complex applications with ease. Happy containerizing!

Top comments (0)