funwithlinux guide

The Basics of Docker on Linux: Installation and Getting Started

In today’s fast-paced development world, consistency across environments—from local machines to production servers—is critical. Enter **Docker**, a platform that simplifies building, shipping, and running applications in lightweight, portable containers. Unlike traditional virtual machines (VMs), Docker containers share the host system’s operating system kernel, making them more efficient, faster to start, and easier to scale. If you’re a Linux user, Docker feels right at home—Linux is the native environment for Docker, and it’s where you’ll get the best performance and feature support. This blog will guide you through the fundamentals of Docker on Linux: from understanding what Docker is and how it works, to installing it on major Linux distributions, and finally, getting hands-on with essential Docker commands and workflows.

Table of Contents

  1. What is Docker?
  2. Containers vs. Virtual Machines: What’s the Difference?
  3. Prerequisites for Installing Docker on Linux
  4. Installing Docker on Linux
  5. Verifying Your Docker Installation
  6. Docker Basics: Key Terminology
  7. Essential Docker Commands
  8. Hands-On: Running Your First Container
  9. Building Your First Docker Image with a Dockerfile
  10. Conclusion
  11. References

What is Docker?

Docker is an open-source platform designed to automate the deployment of applications inside containers—standardized units that package code, runtime, libraries, and dependencies required to run an application. This ensures the app works seamlessly across any environment that supports Docker, eliminating the “it works on my machine” problem.

Key Components of Docker:

  • Docker Engine: The core runtime that builds and runs containers (installed on your Linux host).
  • Docker CLI: The command-line tool to interact with Docker Engine (e.g., docker run, docker build).
  • Docker Hub: A public registry (like GitHub for Docker images) where you can find pre-built images for popular software (e.g., Nginx, Python, MySQL).

Containers vs. Virtual Machines: What’s the Difference?

You might be wondering: How are containers different from virtual machines (VMs)? Let’s break it down:

ContainersVirtual Machines
Share the host OS kernel.Run a full guest OS on top of a hypervisor.
Lightweight (MBs in size).Heavyweight (GBs in size).
Seconds to start.Minutes to start.
Limited isolation (shares kernel).Strong isolation (full OS).

Why does this matter? Containers are ideal for microservices, CI/CD pipelines, and scenarios where you need to run multiple isolated applications efficiently on a single host. VMs, by contrast, are better for full OS isolation (e.g., running Windows on Linux).

Prerequisites for Installing Docker on Linux

Before installing Docker, ensure your Linux system meets these requirements:

  • 64-bit architecture: Docker only supports 64-bit systems.
  • Kernel version: Minimum 3.10 (recommended 4.15+ for better stability). Check with uname -r.
  • sudo access: You’ll need administrative privileges to install packages.
  • Internet connection: To download Docker packages and container images.

Installing Docker on Linux

Docker provides official packages for most major Linux distributions. Below are step-by-step guides for the most popular ones.

Ubuntu/Debian

Ubuntu and Debian use the apt package manager. Follow these steps:

Step 1: Update your package index and install dependencies

First, update your system and install tools required to add Docker’s official repository:

sudo apt update && sudo apt upgrade -y  
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common  

Step 2: Add Docker’s official GPG key

Docker signs its packages with a GPG key to ensure authenticity. Add it with:

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

Step 3: Add the Docker repository

Add Docker’s stable repository to your system:

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 4: Install Docker Engine

Update the package index again, then install Docker Community Edition (CE):

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

Step 5: Start and enable Docker

Ensure Docker runs on boot and start the service immediately:

sudo systemctl start docker  
sudo systemctl enable docker  

Step 6: (Optional) Run Docker without sudo

By default, only root or users in the docker group can run Docker commands. To avoid typing sudo every time, add your user to the docker group:

sudo usermod -aG docker $USER  

Note: Log out and back in for this change to take effect.

Fedora

Fedora uses the dnf package manager. Here’s how to install Docker:

Step 1: Add the Docker repository

Docker doesn’t ship with Fedora’s default repos. Add the official repo:

sudo dnf -y install dnf-plugins-core  
sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo  

Step 2: Install Docker Engine

sudo dnf install -y docker-ce docker-ce-cli containerd.io  

Step 3: Start and enable Docker

sudo systemctl start docker  
sudo systemctl enable docker  

Step 4: (Optional) Add user to docker group

sudo usermod -aG docker $USER  

CentOS (Legacy)

CentOS is now EOL, but if you’re using it, here’s how to install Docker (via the yum package manager):

sudo yum install -y yum-utils  
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo  
sudo yum install -y docker-ce docker-ce-cli containerd.io  
sudo systemctl start docker  
sudo systemctl enable docker  
sudo usermod -aG docker $USER  

Verifying Your Docker Installation

Once installed, verify Docker is working correctly with these checks:

Check Docker version

docker --version  
# Output: Docker version 24.0.6, build ed223bc (version may vary)  

Check Docker Engine status

sudo systemctl status docker  
# Should show "active (running)"  

Run the “Hello World” test container

Docker provides a minimal hello-world image to validate your setup. Run:

docker run hello-world  

If successful, you’ll see a message like:

Hello from Docker!  
This message shows that your installation appears to be working correctly...  

Docker Basics: Key Terminology

Before diving into commands, let’s define core Docker concepts:

  • Docker Image: A read-only template containing instructions to create a container. Images are built from Dockerfiles and stored in registries (e.g., Docker Hub).
  • Docker Container: A runnable instance of an image. Containers are isolated environments where your application runs.
  • Dockerfile: A text file with a series of instructions (e.g., FROM, COPY, RUN) to build a Docker image.
  • Docker Registry: A storage and distribution system for Docker images. Docker Hub is the default public registry.

Essential Docker Commands

Let’s explore the most common Docker commands to manage images and containers.

Image Management

Images are the building blocks of containers. Here’s how to work with them:

docker pull: Download an image from a registry

Pull the official Nginx (web server) image from Docker Hub:

docker pull nginx:latest  
# "latest" is the image tag (version). Omit to use the default tag.  

docker images: List all local images

docker images  
# Output: REPOSITORY   TAG       IMAGE ID       CREATED        SIZE  
#         nginx        latest    605c77e624dd   2 weeks ago    142MB  
#         hello-world  latest    d2c94e258dcb   5 months ago   13.3kB  

docker search: Search for images on Docker Hub

Find images for Python:

docker search python  

docker rmi: Remove an image

Delete the hello-world image (replace IMAGE_ID with the ID from docker images):

docker rmi d2c94e258dcb  

Container Management

Containers are running instances of images. Here’s how to manage them:

docker run: Create and start a container

Run an Nginx container in detached mode (background) and map port 8080 on the host to port 80 in the container:

docker run -d -p 8080:80 --name my-nginx nginx:latest  
  • -d: Run in detached mode (background).
  • -p 8080:80: Map host port 8080 to container port 80.
  • --name my-nginx: Assign a custom name to the container.

docker ps: List running containers

docker ps  
# Output: CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                  NAMES  
#         a1b2c3d4e5f6   nginx:latest   "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   0.0.0.0:8080->80/tcp   my-nginx  

docker ps -a: List all containers (including stopped ones)

docker ps -a  

docker stop: Stop a running container

docker stop my-nginx  

docker start: Restart a stopped container

docker start my-nginx  

docker rm: Delete a stopped container

docker rm my-nginx  

docker logs: View logs from a container

Check logs for the running Nginx container:

docker logs my-nginx  

docker exec: Run a command inside a running container

Open a shell inside the Nginx container:

docker exec -it my-nginx /bin/bash  
# "-it" enables interactive mode with a terminal.  

Hands-On: Running Your First Container

Let’s walk through a practical example: running an Nginx web server in a container and accessing it from your Linux host.

Step 1: Pull the Nginx image

docker pull nginx:latest  

Step 2: Run the Nginx container

Start Nginx in detached mode, mapping host port 8080 to container port 80:

docker run -d -p 8080:80 --name my-first-nginx nginx:latest  

Step 3: Access the web server

Open a browser and navigate to http://localhost:8080, or use curl in the terminal:

curl http://localhost:8080  
# Output: <!DOCTYPE html>... (Nginx welcome page HTML)  

Step 4: Clean up

Stop and delete the container when done:

docker stop my-first-nginx  
docker rm my-first-nginx  

Building Your First Docker Image with a Dockerfile

So far, we’ve used pre-built images from Docker Hub. Now, let’s create a custom image using a Dockerfile. We’ll build a simple Python app that prints “Hello, Docker!“.

Step 1: Create the project files

Create a new directory and add two files:

app.py (your Python app)

print("Hello, Docker!")  

Dockerfile (instructions to build the image)

# Use an official Python runtime as the base image  
FROM python:3.9-slim  

# Set the working directory inside the container  
WORKDIR /app  

# Copy the current directory contents into the container at /app  
COPY . /app  

# Command to run when the container starts  
CMD ["python", "app.py"]  

Step 2: Build the image

Run this command in the directory with your Dockerfile:

docker build -t my-python-app:1.0 .  
# "-t" tags the image with a name ("my-python-app") and version ("1.0").  
# "." specifies the build context (current directory).  

Step 3: Run the custom image

docker run my-python-app:1.0  
# Output: Hello, Docker!  

Congratulations! You’ve built and run your first custom Docker image.

Conclusion

Docker has revolutionized how we develop, ship, and run applications, and Linux is the perfect platform to leverage its power. In this blog, we covered:

  • What Docker is and how it differs from virtual machines.
  • Installing Docker on Ubuntu, Fedora, and CentOS.
  • Verifying your installation with the hello-world container.
  • Key Docker terminology (images, containers, Dockerfiles).
  • Essential commands to manage images and containers.
  • Building your first custom image with a Dockerfile.

This is just the beginning—Docker offers advanced features like Docker Compose (for multi-container apps), Docker Swarm (for orchestration), and integration with CI/CD pipelines. Explore the references below to dive deeper!

References