Docker Installation and Essential Commands: Complete Beginner's Guide
Learn to install Docker on CentOS/RHEL and master essential Docker commands. From installation to running containers, managing images, and basic container operations.
Docker revolutionizes application deployment and development by packaging applications into lightweight, portable containers. This comprehensive guide walks you through installing Docker on CentOS/RHEL and mastering the essential commands every Docker user needs to know.
๐ฏ What You'll Learn: In this hands-on tutorial, you'll discover:
- How to properly install Docker CE on CentOS/RHEL systems
- Essential Docker commands for container and image management
- Running your first containers and understanding basic operations
- Managing container lifecycle with start, stop, and remove commands
- Working with Docker images and the Docker registry
- Setting up Docker for non-root user access
Time to read: ~12 minutes | Difficulty: Beginner
๐ Why Docker Matters
Docker containers solve the "it works on my machine" problem by creating consistent, isolated environments that run anywhere. Whether you're developing applications, deploying services, or learning new technologies, Docker simplifies the process significantly.
Prerequisites
Before we begin, make sure you have:
- A CentOS, RHEL, or Fedora system
- Root or sudo privileges
- Internet connection for downloading packages
- Basic familiarity with terminal commands
๐งน Step 1: Remove Old Docker Versions
Before installing the latest Docker, remove any existing Docker installations to avoid conflicts:
sudo dnf remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
This command removes all old Docker packages and their dependencies. Don't worry if some packages aren't found - this just ensures a clean installation.
๐ก Why Remove Old Versions: Old Docker installations can conflict with the new Docker CE (Community Edition). This step ensures you start with a clean slate.
๐ฆ Step 2: Install Required Dependencies
Install the necessary plugins for DNF package manager:
sudo dnf -y install dnf-plugins-core
This package provides additional functionality for DNF, including the ability to manage repositories.
๐ Step 3: Add Docker Repository
Add the official Docker repository to your system:
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
This adds the Docker CE repository, which contains the latest stable versions of Docker and its components.
๐ฅ Step 4: Install Docker CE
Install Docker Community Edition and its essential components:
sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
This installs:
- docker-ce: The Docker engine
- docker-ce-cli: Command line interface
- containerd.io: Container runtime
- docker-buildx-plugin: Extended build capabilities
- docker-compose-plugin: Multi-container application management
๐ Step 5: Enable and Start Docker
Enable Docker to start automatically at boot and start it immediately:
sudo systemctl enable --now docker
The --now
flag both enables the service for automatic startup and starts it immediately.
โ Step 6: Verify Installation
Test your Docker installation by running the hello-world container:
sudo docker run hello-world
You should see output similar to:
Hello from Docker!
This message shows that your installation appears to be working correctly.
๐ฅ Step 7: Configure User Access
Add your user to the docker group to run Docker commands without sudo:
sudo usermod -aG docker $USER
newgrp docker
- Line 1: Adds current user to the docker group
- Line 2: Activates the new group membership immediately
โ
Important: After running these commands, you can use Docker without sudo
. The newgrp
command applies the changes immediately in your current session.
๐งช Step 8: Test User Access
Verify that Docker works without sudo privileges:
docker --version
docker run hello-world
Both commands should work without requiring sudo or password prompts.
๐ Step 9: Essential Container Management Commands
Now let's explore the fundamental Docker commands for managing containers:
View Running Containers
docker ps
This shows currently running containers. Initially, this will likely be empty.
View All Containers
docker ps -a
This displays all containers, including stopped ones. You should see the hello-world container from earlier.
View Downloaded Images
docker images
This shows all Docker images stored locally on your system.
Display System Information
docker info
This provides comprehensive information about your Docker installation and system resources.
๐ง Step 10: Interactive Container Session
Run an interactive Ubuntu container to explore inside a container:
docker run -it ubuntu:latest /bin/bash
This command:
-i
: Keeps STDIN open (interactive)-t
: Allocates a pseudo-TTY (terminal)ubuntu:latest
: Uses the latest Ubuntu image/bin/bash
: Runs bash shell inside the container
Explore Inside the Container
Once inside the Ubuntu container, try these commands:
ls
pwd
cat /etc/os-release
- Line 1: List files in the current directory
- Line 2: Print working directory
- Line 3: Display Ubuntu version information
Exit the Container
exit
This terminates the container and returns you to your host system.
๐ Step 11: Background Container Management
Run a container in the background (detached mode):
docker run -d --name my-nginx nginx:latest
This command:
-d
: Runs in detached mode (background)--name my-nginx
: Assigns a custom namenginx:latest
: Uses the latest Nginx web server image
Check Running Containers
docker ps
You should see the my-nginx container running.
๐ Step 12: Container Lifecycle Management
Stop a Running Container
docker stop my-nginx
This gracefully stops the named container.
Remove a Stopped Container
docker rm my-nginx
This permanently removes the container from your system.
Remove an Image
docker rmi hello-world
This removes the hello-world image from local storage to free up space.
โ ๏ธ Note: You can only remove images that aren't being used by any containers. Stop and remove containers first if needed.
๐ Understanding the Commands
Let's break down what each command accomplishes:
Command | Purpose | Example |
---|---|---|
docker --version | Show Docker version | Docker version 24.0.6 |
docker run | Create and start container | docker run hello-world |
docker ps | List running containers | Shows active containers |
docker ps -a | List all containers | Shows all containers |
docker images | List local images | Shows downloaded images |
docker info | System information | Detailed Docker info |
docker stop | Stop running container | docker stop my-nginx |
docker rm | Remove container | docker rm my-nginx |
docker rmi | Remove image | docker rmi hello-world |
๐ Container States and Lifecycle
Understanding container states helps you manage them effectively:
# Create and run container
docker run -d --name test-container nginx:latest
# Container is now in "running" state
docker ps
# Stop the container (moves to "stopped" state)
docker stop test-container
# Container still exists but stopped
docker ps -a
# Remove the container permanently
docker rm test-container
# Container no longer exists
docker ps -a
๐ฏ Command Flags and Options
Here are the key flags you'll use frequently:
Docker Run Options
Flag | Description | Usage |
---|---|---|
-d | Detached mode (background) | docker run -d nginx |
-it | Interactive terminal | docker run -it ubuntu bash |
--name | Assign container name | docker run --name web nginx |
-p | Port mapping | docker run -p 80:80 nginx |
--rm | Auto-remove when stopped | docker run --rm hello-world |
Docker PS Options
Flag | Description | Usage |
---|---|---|
-a | Show all containers | docker ps -a |
-l | Show latest container | docker ps -l |
-q | Show only container IDs | docker ps -q |
๐งช Practice Session
Try this complete workflow to reinforce your learning:
# Check Docker version
docker --version
# Run a quick test
docker run hello-world
# List all containers
docker ps -a
# Run interactive Alpine Linux
docker run -it alpine:latest sh
# Inside Alpine container:
# ls
# whoami
# exit
# Run Nginx in background
docker run -d --name web-server nginx:latest
# Check running containers
docker ps
# Stop the web server
docker stop web-server
# Remove the container
docker rm web-server
# Clean up images
docker images
docker rmi hello-world alpine nginx
๐ฏ Key Takeaways
โ Remember These Points
- Clean Installation: Always remove old Docker versions before installing
- User Access: Add your user to docker group to avoid using sudo
- Container Lifecycle: Understand run โ stop โ remove workflow
- Image Management: Remove unused images to save disk space
- Essential Commands: Master
run
,ps
,stop
,rm
, andrmi
๐ Further Reading
Official Resources
- Docker Documentation
- Docker Hub - Official image registry
- Docker Get Started Guide
๐ Excellent Work! You've successfully installed Docker and learned the essential commands for container management. You now have the foundation to start containerizing applications and exploring the Docker ecosystem.
๐ฌ Discussion
I'd love to hear about your Docker journey:
- What applications are you planning to containerize?
- Have you encountered any installation challenges?
- Which Docker commands do you find most useful?
- What development workflows are you hoping to improve with Docker?
Connect with me: