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.

11 min read

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 name
  • nginx: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:

CommandPurposeExample
docker --versionShow Docker versionDocker version 24.0.6
docker runCreate and start containerdocker run hello-world
docker psList running containersShows active containers
docker ps -aList all containersShows all containers
docker imagesList local imagesShows downloaded images
docker infoSystem informationDetailed Docker info
docker stopStop running containerdocker stop my-nginx
docker rmRemove containerdocker rm my-nginx
docker rmiRemove imagedocker 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

FlagDescriptionUsage
-dDetached mode (background)docker run -d nginx
-itInteractive terminaldocker run -it ubuntu bash
--nameAssign container namedocker run --name web nginx
-pPort mappingdocker run -p 80:80 nginx
--rmAuto-remove when stoppeddocker run --rm hello-world

Docker PS Options

FlagDescriptionUsage
-aShow all containersdocker ps -a
-lShow latest containerdocker ps -l
-qShow only container IDsdocker 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

  1. Clean Installation: Always remove old Docker versions before installing
  2. User Access: Add your user to docker group to avoid using sudo
  3. Container Lifecycle: Understand run โ†’ stop โ†’ remove workflow
  4. Image Management: Remove unused images to save disk space
  5. Essential Commands: Master run, ps, stop, rm, and rmi

๐Ÿ“– Further Reading

Official Resources


โœ…

๐ŸŽ‰ 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:

  • ๐Ÿ™ GitHub - Docker examples and configurations
  • ๐Ÿ“ง Contact - Docker questions and project discussions
O

Written by Owais

Passionate developer sharing insights on technology, development, and the art of building great software.

Related Articles

Continue exploring with these handpicked articles that complement what you just read

More Reading

One more article you might find interesting