Docker Installation Guide for Linux: Complete Tutorial for Beginners 2025
Docker has revolutionized how we develop, ship, and run applications. This comprehensive guide covers everything you need to know about installing and getting started with Docker on Linux in 2025.
📑 Table of Contents
- What is Docker?
- Why Use Docker?
- Prerequisites
- Installing Docker on Ubuntu/Debian
- Step 1: Update System Packages
- Step 2: Install Required Dependencies
- Step 3: Add Docker’s Official GPG Key
- Step 4: Set Up Docker Repository
- Step 5: Install Docker Engine
- Step 6: Verify Installation
- Installing Docker on Rocky Linux/AlmaLinux/CentOS
- Step 1: Remove Old Docker Versions
- Step 2: Install Required Packages
- Step 3: Install Docker
- Step 4: Start and Enable Docker
- Post-Installation Steps
- Run Docker Without Sudo
- Verify Docker Installation
- Essential Docker Commands
- Container Management
- Image Management
- Docker Compose Commands
- Creating Your First Dockerfile
- Docker Compose Example
- Best Practices
- Troubleshooting Common Issues
- Permission Denied Error
- Docker Daemon Not Running
- Disk Space Issues
- Conclusion
What is Docker?
Docker is a containerization platform that packages applications and their dependencies into lightweight, portable containers. Unlike virtual machines, containers share the host operating system kernel, making them faster and more resource-efficient.
Why Use Docker?
- Consistency: Same environment from development to production
- Isolation: Applications run independently without conflicts
- Portability: Run anywhere Docker is installed
- Efficiency: Lightweight compared to traditional VMs
- Scalability: Easy horizontal scaling with orchestration tools
- Version Control: Track changes to your application environment
Prerequisites
Before installing Docker, ensure your system meets these requirements:
- 64-bit Linux operating system
- Kernel version 3.10 or higher (check with
uname -r) - Sudo or root access
- At least 4GB RAM recommended
ubuntu-debian">Installing Docker on Ubuntu/Debian
Step 1: Update System Packages
sudo apt update
sudo apt upgrade -y
Step 2: Install Required Dependencies
sudo apt install -y \
ca-certificates \
curl \
gnupg \
lsb-release
Step 3: Add Docker’s Official GPG Key
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
Step 4: Set Up Docker Repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Step 5: Install Docker Engine
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Step 6: Verify Installation
sudo docker run hello-world
You should see a message confirming Docker is installed correctly.
centos">Installing Docker on Rocky Linux/AlmaLinux/CentOS
Step 1: Remove Old Docker Versions
sudo dnf remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
Step 2: Install Required Packages
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Step 3: Install Docker
sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Step 4: Start and Enable Docker
sudo systemctl start docker
sudo systemctl enable docker
Post-Installation Steps
Run Docker Without Sudo
Add your user to the docker group to run Docker commands without sudo:
sudo usermod -aG docker $USER
newgrp docker
Log out and back in for changes to take effect.
Verify Docker Installation
# Check Docker version
docker --version
# Check Docker Compose version
docker compose version
# Run test container
docker run hello-world
Essential Docker Commands
Container Management
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Start a container
docker start container_name
# Stop a container
docker stop container_name
# Remove a container
docker rm container_name
# Run a container interactively
docker run -it ubuntu bash
Image Management
# List images
docker images
# Pull an image
docker pull nginx
# Remove an image
docker rmi image_name
# Build an image from Dockerfile
docker build -t myapp:latest .
Docker Compose Commands
# Start services
docker compose up -d
# Stop services
docker compose down
# View logs
docker compose logs -f
# Rebuild and start
docker compose up -d --build
Creating Your First Dockerfile
A Dockerfile defines how to build your container image. Here is a simple example for a Node.js application:
# Use official Node.js image
FROM node:20-alpine
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy application code
COPY . .
# Expose port
EXPOSE 3000
# Start command
CMD ["npm", "start"]
Build and run your container:
# Build the image
docker build -t mynode-app .
# Run the container
docker run -d -p 3000:3000 mynode-app
Docker Compose Example
Docker Compose simplifies multi-container applications. Here is an example for a web application with a database:
# docker-compose.yml
version: '3.8'
services:
web:
build: .
ports:
- "80:80"
depends_on:
- db
environment:
- DB_HOST=db
- DB_USER=myuser
- DB_PASSWORD=mypassword
db:
image: mysql:8.0
volumes:
- db_data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=rootpassword
- MYSQL_DATABASE=myapp
- MYSQL_USER=myuser
- MYSQL_PASSWORD=mypassword
volumes:
db_data:
Best Practices
- Use Official Images: Start with official base images from Docker Hub
- Minimize Layers: Combine RUN commands to reduce image size
- Use .dockerignore: Exclude unnecessary files from builds
- Don’t Run as Root: Create and use non-root users in containers
- Use Multi-stage Builds: Reduce final image size
- Tag Images Properly: Use semantic versioning for image tags
- Scan for Vulnerabilities: Use
docker scoutto check images
Troubleshooting Common Issues
Permission Denied Error
# Add user to docker group
sudo usermod -aG docker $USER
# Restart Docker daemon
sudo systemctl restart docker
Docker Daemon Not Running
# Check status
sudo systemctl status docker
# Start Docker
sudo systemctl start docker
Disk Space Issues
# Remove unused containers, networks, images
docker system prune -a
# Check disk usage
docker system df
Conclusion
Docker is an essential tool for modern software development. By containerizing your applications, you ensure consistent environments across development, testing, and production. Start with simple containers and gradually explore advanced features like Docker Compose, networking, and orchestration with Kubernetes.
Practice with the commands and examples in this guide, and you will be comfortable with Docker in no time. The containerization skills you develop will be valuable throughout your career in DevOps and software development.
Was this article helpful?
About Ramesh Sundararamaiah
Red Hat Certified Architect
Expert in Linux system administration, DevOps automation, and cloud infrastructure. Specializing in Red Hat Enterprise Linux, CentOS, Ubuntu, Docker, Ansible, and enterprise IT solutions.