Introduction
Docker is a revolutionary platform that simplifies building, shipping, and running applications in lightweight containers. This guide provides clear, step-by-step instructions for installing Docker on Linux distributions (Ubuntu/Debian/CentOS) and running your first container. No prior Docker experience required!
🛠 Prerequisites
- Linux System: Ubuntu 20.04+, Debian 10+, or CentOS 7+
- Terminal Access: Use
Ctrl+Alt+T
(Ubuntu/Debian) or connect via SSH. - Admin Privileges: Run commands with
sudo
. - Internet Connection: Required for downloads.
🔄 Step 1: Remove Old Docker Versions
Ensure a clean installation:
sudo apt-get remove docker docker-engine docker.io containerd runc # Ubuntu/Debian
sudo yum remove docker docker-client docker-common docker-latest # CentOS
⬇️ Step 2: Install Docker
For Ubuntu/Debian:
- Update packages:
sudo apt-get update
- Install dependencies:
sudo apt-get install apt-transport-https ca-certificates curl gnupg
- Add Docker’s GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
- Set up the repository:
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
- Install Docker Engine:
sudo apt-get update && sudo apt-get install docker-ce docker-ce-cli containerd.io
For CentOS:
- Install dependencies:
sudo yum install -y yum-utils
- Add Docker repository:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- Install Docker Engine:
sudo yum install docker-ce docker-ce-cli containerd.io
▶️ Step 3: Start Docker
Enable and launch the Docker service:
sudo systemctl start docker # Start Docker
sudo systemctl enable docker # Launch on boot
sudo systemctl status docker # Verify it's running
✅ Step 4: Verify Installation
Run a test container:
sudo docker run hello-world
✅ Expected Output:
Hello from Docker!
This message shows your installation works correctly.
👥 Step 5: Run Docker as Non-Root User (Optional but Recommended)
Avoid using sudo
with Docker:
sudo usermod -aG docker $USER # Add your user to the "docker" group
newgrp docker # Apply group changes immediately
🔑 Important: Log out and back in for changes to take effect.
🐳 Step 6: Basic Docker Commands
Command | Description |
---|---|
docker pull nginx |
Download the Nginx image |
docker run -d -p 8080:80 nginx |
Run Nginx in background, map port 8080→80 |
docker ps |
List running containers |
docker stop |
Stop a container |
docker rm |
Delete a container |
docker rmi nginx |
Remove the Nginx image |
🚀 Example: Run a Web Server
- Start an Nginx container:
docker run -d --name my-web -p 8080:80 nginx
- Access in your browser:
http://:8080
→ Welcome to Nginx!
🔧 Troubleshooting
- Permission Denied? Ensure your user is in the
docker
group (Step 5). - “hello-world” not working? Check internet connectivity and Docker service status (
systemctl status docker
). - Firewall Issues: Allow Docker ports (e.g.,
sudo ufw allow 8080
on Ubuntu).
📌 Conclusion
You’ve successfully installed Docker, run your first container, and learned essential commands! Docker streamlines application deployment—explore Docker Hub for pre-built images (MySQL, Python, etc.).
> Next Steps:
> – Learn Dockerfile basics to create custom images.
> – Try Docker Compose for multi-container apps.
> – Check the official Docker docs for advanced topics!
💬 Questions? Share them in the comments below!