How to Install and Use Docker on Linux in 2025: Complete DevOps Tutorial
Meta Description: Learn how to install and use Docker on Linux in 2025. Complete tutorial covering containerization, Docker Compose, images, and containers for DevOps professionals.
Introduction
Docker has revolutionized the way developers and DevOps professionals deploy applications through containerization technology. As we enter 2025, Docker remains the leading platform for creating, distributing, and running containerized applications on Linux systems. This comprehensive Linux Docker tutorial will guide you through everything from installation to advanced usage, making it perfect for both beginners and experienced professionals looking to master containerization.
Containerization has become essential in modern DevOps workflows, enabling consistent application deployment across different environments while maximizing resource efficiency. Whether you're developing microservices, setting up CI/CD pipelines, or managing complex applications, understanding Docker on Linux is crucial for success in today's tech landscape.
What is Docker and Why Use It on Linux?
Docker is a containerization platform that packages applications and their dependencies into lightweight, portable containers. Unlike traditional virtual machines, Docker containers share the host operating system's kernel, making them more efficient and faster to start.
Key Benefits of Docker on Linux:
- Resource Efficiency: Containers use fewer resources than virtual machines - Portability: Applications run consistently across different Linux distributions - Scalability: Easy horizontal scaling of applications - Isolation: Applications run in isolated environments - DevOps Integration: Seamless integration with CI/CD pipelines - Version Control: Easy application versioning and rollbacks
System Requirements and Prerequisites
Before installing Docker on your Linux system, ensure you meet these requirements:
Minimum System Requirements:
- 64-bit Linux distribution - Kernel version 3.10 or higher - At least 2GB RAM (4GB recommended) - 10GB free disk space - Root or sudo privilegesSupported Linux Distributions (2025):
- Ubuntu 20.04 LTS, 22.04 LTS, 24.04 LTS - CentOS 7, 8, 9 - RHEL 7, 8, 9 - Fedora 36, 37, 38, 39 - Debian 10, 11, 12 - SUSE Linux Enterprise Server - Amazon Linux 2Installing Docker on Linux
Method 1: Installing Docker on Ubuntu/Debian
First, update your package index and install prerequisites:
`bash
sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release
`
Add Docker's official GPG key:
`bash
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
`
Add the Docker repository:
`bash
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
`
Install Docker Engine:
`bash
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
`
Method 2: Installing Docker on CentOS/RHEL/Fedora
Install required packages:
`bash
sudo dnf install -y dnf-plugins-core
`
Add Docker repository:
`bash
sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
`
Install Docker:
`bash
sudo dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin
`
Method 3: Universal Installation Script
Docker provides a convenience script for quick installation:
`bash
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
`
Post-Installation Setup
Start and Enable Docker Service
`bash
sudo systemctl start docker
sudo systemctl enable docker
`
Add User to Docker Group
To run Docker commands without sudo:
`bash
sudo usermod -aG docker $USER
newgrp docker
`
Verify Installation
`bash
docker --version
docker run hello-world
`
Understanding Docker Images
Docker images are read-only templates used to create containers. They contain the application code, runtime, libraries, and dependencies needed to run an application.
Working with Docker Images
List available images:
`bash
docker images
`
Search for images on Docker Hub:
`bash
docker search nginx
`
Pull an image from Docker Hub:
`bash
docker pull nginx:latest
docker pull ubuntu:22.04
`
Remove an image:
`bash
docker rmi nginx:latest
`
Building Custom Images
Create a Dockerfile:
`dockerfile
Use official Python runtime as base image
FROM python:3.11-slimSet working directory
WORKDIR /appCopy requirements file
COPY requirements.txt .Install dependencies
RUN pip install --no-cache-dir -r requirements.txtCopy application code
COPY . .Expose port
EXPOSE 8000Define command to run application
CMD ["python", "app.py"]`Build the image:
`bash
docker build -t myapp:1.0 .
`
Working with Docker Containers
Containers are running instances of Docker images. They provide isolated environments for applications.
Basic Container Operations
Run a container:
`bash
docker run -d --name webserver -p 8080:80 nginx
`
List running containers:
`bash
docker ps
`
List all containers (including stopped):
`bash
docker ps -a
`
Stop a container:
`bash
docker stop webserver
`
Start a stopped container:
`bash
docker start webserver
`
Remove a container:
`bash
docker rm webserver
`
Interactive Container Usage
Run container interactively:
`bash
docker run -it ubuntu:22.04 /bin/bash
`
Execute commands in running container:
`bash
docker exec -it webserver /bin/bash
`
Container Logs and Monitoring
View container logs:
`bash
docker logs webserver
docker logs -f webserver # Follow logs in real-time
`
Monitor container resource usage:
`bash
docker stats webserver
`
Docker Compose for Multi-Container Applications
Docker Compose simplifies managing multi-container applications using YAML configuration files.
Installing Docker Compose
Docker Compose is now included as a plugin with Docker Engine. Verify installation:
`bash
docker compose version
`
Creating a Docker Compose File
Create docker-compose.yml:
`yaml
version: '3.8'
services: web: build: . ports: - "8000:8000" volumes: - .:/app environment: - DEBUG=1 depends_on: - db - redis
db: image: postgres:15 environment: POSTGRES_DB: myapp POSTGRES_USER: user POSTGRES_PASSWORD: password volumes: - postgres_data:/var/lib/postgresql/data ports: - "5432:5432"
redis: image: redis:7-alpine ports: - "6379:6379"
volumes:
postgres_data:
`
Docker Compose Commands
Start services:
`bash
docker compose up -d
`
View running services:
`bash
docker compose ps
`
Stop services:
`bash
docker compose down
`
View logs:
`bash
docker compose logs web
`
Scale services:
`bash
docker compose up --scale web=3
`
Advanced Docker Usage
Docker Networking
Create a custom network:
`bash
docker network create mynetwork
`
Run containers on custom network:
`bash
docker run -d --name app1 --network mynetwork nginx
docker run -d --name app2 --network mynetwork nginx
`
Volume Management
Create a named volume:
`bash
docker volume create mydata
`
Use volume in container:
`bash
docker run -d -v mydata:/data nginx
`
Bind mount host directory:
`bash
docker run -d -v /host/path:/container/path nginx
`
Environment Variables and Secrets
Pass environment variables:
`bash
docker run -e ENV_VAR=value -e DB_PASSWORD=secret nginx
`
Use environment file:
`bash
docker run --env-file .env nginx
`
Best Practices for Docker on Linux
Security Best Practices
1. Use official base images: Start with official, minimal base images
2. Run as non-root user: Create and use non-root users in containers
3. Keep images updated: Regularly update base images and dependencies
4. Scan for vulnerabilities: Use tools like Docker Scout or Trivy
5. Limit container privileges: Use --user flag and avoid --privileged
Performance Optimization
1. Multi-stage builds: Reduce image size with multi-stage Dockerfiles 2. Layer caching: Optimize Dockerfile layer ordering 3. Resource limits: Set memory and CPU limits for containers 4. Use .dockerignore: Exclude unnecessary files from build context
Example optimized Dockerfile:
`dockerfile
Multi-stage build
FROM node:18-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm ci --only=productionFROM node:18-alpine AS runtime
RUN addgroup -g 1001 -S nodejs && \
adduser -S nextjs -u 1001
WORKDIR /app
COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules
COPY --chown=nextjs:nodejs . .
USER nextjs
EXPOSE 3000
CMD ["npm", "start"]
`
Troubleshooting Common Issues
Docker Daemon Issues
If Docker daemon isn't running:
`bash
sudo systemctl status docker
sudo systemctl restart docker
`
Permission Denied Errors
Ensure user is in docker group:
`bash
groups $USER
sudo usermod -aG docker $USER
`
Storage Issues
Clean up unused resources:
`bash
docker system prune -a
docker volume prune
`
Frequently Asked Questions (FAQ)
Q: What's the difference between Docker containers and virtual machines? A: Containers share the host OS kernel and are more lightweight, while VMs include a full OS. Containers start faster and use fewer resources.
Q: Can I run Docker on older Linux distributions? A: Docker requires kernel version 3.10 or higher. Older distributions may need kernel updates or use older Docker versions.
Q: How do I persist data in Docker containers? A: Use Docker volumes or bind mounts to persist data outside the container filesystem.
Q: Is Docker free for commercial use in 2025? A: Docker Engine remains free and open-source. Docker Desktop has licensing restrictions for large organizations.
Q: How do I update Docker on Linux?
A: Use your distribution's package manager: sudo apt update && sudo apt upgrade docker-ce for Ubuntu/Debian.
Q: Can I run GUI applications in Docker containers? A: Yes, but it requires additional configuration for X11 forwarding or using tools like VNC.
Conclusion
Docker on Linux provides a powerful platform for containerization and modern DevOps practices. This tutorial covered everything from basic installation to advanced usage with Docker Compose, giving you the foundation needed to leverage containerization effectively in 2025.
As containerization continues to evolve, Docker remains an essential tool for developers and DevOps professionals. The combination of Docker's flexibility with Linux's stability creates an ideal environment for deploying scalable, efficient applications.
Start with simple containers and gradually explore advanced features like orchestration, networking, and security. With practice, you'll master Docker and unlock the full potential of containerization for your projects and infrastructure.
Remember to stay updated with Docker's latest features and security practices as the technology continues to advance throughout 2025 and beyond.