
Get Started with Docker on Linux: A Step-by-Step Installation Guide
Containerization has fundamentally changed how developers build, ship, and run applications. At the forefront of this revolution is Docker, a powerful platform that packages applications and their dependencies into lightweight, portable units called containers. For Linux users, Docker is a particularly powerful tool, as it runs natively on the Linux kernel, offering superior performance and stability.
This comprehensive guide will walk you through the entire process of installing Docker on a Linux system, configuring it for secure use, and running your very first containers.
Why Use Docker on Linux?
Before diving into the installation, it’s important to understand the benefits. Docker provides a standardized environment for your applications, eliminating the classic “it works on my machine” problem.
- Consistent Environments: Ensure your application runs the same way in development, testing, and production.
- Application Isolation: Containers run in isolated environments, preventing conflicts between dependencies and improving security.
- Portability: A container built on your laptop can run on any other Linux machine, on-premises server, or cloud provider that has Docker installed.
- Resource Efficiency: Containers are much lighter than traditional virtual machines, as they share the host system’s kernel. This means you can run more containers on a single host, saving resources and costs.
Prerequisites
To follow this guide, you will need:
- A 64-bit version of a Debian-based Linux distribution, such as Ubuntu 22.04, 20.04, or Debian 11. While Docker supports many distributions, these instructions are tailored for
apt-based systems. - Access to a user account with sudo privileges.
- A stable internet connection.
Step-by-Step Guide to Installing Docker
We will be installing Docker Engine from Docker’s official repository. This is the recommended method as it ensures you always have the latest, most secure version.
Step 1: Update Your System
First, open your terminal and update your package list to ensure you have the latest information about available software.
sudo apt update && sudo apt upgrade -y
Step 2: Install Required Packages
Next, install a few prerequisite packages that allow apt to use a repository over HTTPS. This is a crucial step for securely downloading software.
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
Step 3: Add Docker’s Official GPG Key
To ensure the Docker packages you download are authentic, you need to add Docker’s official GPG (GNU Privacy Guard) key to your system.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Step 4: Set Up the Docker Repository
Now, add the official Docker repository to your system’s APT sources. This tells your package manager where to find the Docker packages.
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
Step 5: Install Docker Engine
With the repository set up, update your package list one more time and then install Docker Engine.
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -y
Once the installation is complete, you can verify that the Docker service is running.
sudo systemctl status docker
If it’s active, you’re all set!
Post-Installation: Running Docker Without Sudo
By default, the Docker daemon binds to a Unix socket owned by the root user. This means you have to use sudo every time you run a Docker command. For security and convenience, it’s best to allow your user to run Docker commands without sudo.
To do this, you need to add your user to the docker group.
sudo usermod -aG docker ${USER}
Important: For this change to take effect, you must log out of your current session and log back in, or run the following command:
newgrp docker
Running Your First Docker Containers
Now for the exciting part. Let’s test your installation by running a couple of simple containers.
The Classic “Hello World”
This container is designed to test that your installation is working correctly. It prints a confirmation message and then exits.
docker run hello-world
When you run this command, Docker will first check if the hello-world image is available locally. If not, it will automatically pull the image from Docker Hub, create a new container from that image, run it, and display the output.
A Practical Example: Running an NGINX Web Server
Let’s run something more useful: a fully functional NGINX web server. This one command will download the official NGINX image and start a web server accessible from your machine.
docker run --name my-web-server -p 8080:80 -d nginx
Let’s break down this command:
docker run: The command to create and start a new container.--name my-web-server: Assigns a human-readable name to your container for easy management.-p 8080:80: This is the port mapping. It maps port8080on your host machine to port80inside the container (which is the default port for NGINX).-d: Runs the container in “detached” mode, meaning it runs in the background.nginx: The name of the image to use.
To verify it’s working, open a web browser and navigate to http://localhost:8080. You should see the default NGINX welcome page!
Essential Docker Commands to Get You Started
Here are a few basic commands to help you manage your new containers:
docker ps: Lists all currently running containers.docker ps -a: Lists all containers, including those that have stopped.docker images: Shows all the Docker images you have downloaded to your machine.docker stop my-web-server: Stops the container namedmy-web-server.docker rm my-web-server: Removes the stopped container.
Congratulations! You have successfully installed Docker on your Linux system, configured it for secure, non-root access, and run both a test container and a functional web server. You are now ready to explore the vast ecosystem of containerized applications and begin building your own.
Source: https://linuxblog.io/install-docker-linux-run-container/


