Install Docker and Learn Basic Container Manipulation in CentOS and RHEL 8/7

Docker is a containerization platform that packages applications and their dependencies into lightweight, portable containers. This allows applications to run consistently across different environments, from development to production. In this article, we will guide you through installing Docker on CentOS and RHEL 8/7 and demonstrate basic container operations.

Installing Docker on CentOS/RHEL 8

Preparing the System

Before installing Docker, ensure your system is up to date and install required dependencies ?

$ sudo yum update
$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Adding Docker Repository

Add the official Docker repository to your system ?

$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Installing Docker Engine

Install Docker Community Edition and its components ?

$ sudo yum install -y docker-ce docker-ce-cli containerd.io

Starting Docker Service

Start Docker and enable it to start automatically at boot ?

$ sudo systemctl start docker
$ sudo systemctl enable docker

Verifying Installation

Verify that Docker is installed correctly by checking the version ?

$ docker --version
Docker version 24.0.5, build ced0996

Installing Docker on CentOS/RHEL 7

The installation process for RHEL/CentOS 7 is similar, with the same repository and package names ?

$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum install -y docker-ce docker-ce-cli containerd.io
$ sudo systemctl start docker
$ sudo systemctl enable docker

Basic Container Operations

Running Your First Container

Run an Nginx web server container in detached mode, mapping port 80 ?

$ docker run -d -p 80:80 nginx

Managing Containers

View running containers ?

$ docker ps

Stop a container using its ID or name ?

$ docker stop <container_id_or_name>

Remove a stopped container ?

$ docker rm <container_id_or_name>

Working with Images

Pull an image from Docker Hub ?

$ docker pull ubuntu:20.04

List available images ?

$ docker images

Remove an image ?

$ docker rmi <image_id_or_name>

Interactive Containers

Run an interactive Ubuntu container with a shell ?

$ docker run -it ubuntu:20.04 /bin/bash

Docker Networking

Network Types

Network Type Description Use Case
Bridge Default network for container communication Single-host applications
Host Containers share host network namespace High-performance networking
Overlay Multi-host container communication Docker Swarm clusters

Creating Custom Networks

Create a custom bridge network ?

$ docker network create mynetwork

Run containers on the custom network ?

$ docker run -d --network=mynetwork --name web1 nginx
$ docker run -d --network=mynetwork --name web2 nginx

Building Custom Images

Creating a Dockerfile

Create a simple Dockerfile for a custom web server ?

FROM nginx:latest
COPY index.html /usr/share/nginx/html/
EXPOSE 80

Build the custom image ?

$ docker build -t custom-nginx .

Run a container from the custom image ?

$ docker run -d -p 8080:80 custom-nginx

Container Management Commands

Copy files between host and container ?

$ docker cp file.txt container_name:/path/to/destination/

View container logs ?

$ docker logs <container_name>

Inspect container details ?

$ docker inspect <container_name>

Limit container resources ?

$ docker run --memory=512m --cpus=1.0 nginx

Conclusion

Docker provides a powerful platform for containerizing applications on CentOS and RHEL systems. We covered the installation process, basic container operations, networking concepts, and custom image creation. With these fundamentals, you can begin leveraging Docker to create consistent, portable application deployments across different environments.

Updated on: 2026-03-17T09:01:39+05:30

385 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements