Operations 11 min read

How to Install Docker on CentOS 8 and Deploy Nginx Containers – A Complete Step‑by‑Step Guide

This tutorial walks through preparing a CentOS 8 server, uninstalling any existing Docker packages, adding a Tencent Cloud mirror, installing and starting Docker, configuring a non‑root Docker user group, and finally pulling, configuring, and running an Nginx container with essential HTTPS settings.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
How to Install Docker on CentOS 8 and Deploy Nginx Containers – A Complete Step‑by‑Step Guide

Introduction

The author needed a fresh Linux server environment based on Docker and compiled a verified operation manual for others facing similar requirements.

Docker Installation on Linux (CentOS 8 / TencentOS Server)

Remove Existing Docker Packages

If Docker is already installed, run the following command to remove old versions:

yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

Add Docker Repository Mirror

Network issues often affect the official Docker mirror, so a Tencent Cloud mirror is used for servers on Tencent Cloud:

dnf config-manager --add-repo=https://mirrors.cloud.tencent.com/docker-ce/linux/centos/docker-ce.repo

Use dnf (the Dandified Yum) for package management; yum can still be used on older systems.

Install Docker Packages

Install required dependencies first:

yum install yum-utils device-mapper-persistent-data lvm2

Optionally list available Docker versions: yum list docker-ce --showduplicates | sort -r Install Docker CE, CLI, and containerd:

yum install docker-ce docker-ce-cli containerd.io

Verify the installation:

docker version

Start Docker Service

# Enable Docker to start on boot
systemctl enable docker
# Start Docker now
systemctl start docker

Confirm the daemon is running with a test container:

docker run hello-world

Docker User Group Management

To avoid using root for Docker commands, create a dedicated docker group and add the target user:

sudo groupadd docker
sudo usermod -aG docker username
newgrp docker

Verify the user belongs to the group: groups username After re‑login, the user can run Docker commands without sudo.

Deploying Nginx with Docker

Pull and Prepare the Image

docker search nginx
docker pull nginx:latest

Extract the default configuration from a temporary container:

# Run a temporary container
docker run -d --name nginx nginx
# List containers to get the ID if needed
docker container ls
# Create local directories
mkdir conf html
# Copy config files from the container
docker cp nginx:/etc/nginx/nginx.conf $PWD/conf
docker cp nginx:/etc/nginx/conf.d $PWD/conf
# Stop and remove the temporary container
docker container stop nginx
docker container rm nginx

Run the Official Nginx Container with Volumes

docker run -d -p 80:80 \
       -p 443:443 \
       --name nginx \
       -v /home/worker/nginx/html:/usr/share/nginx/html \
       -v /home/worker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
       -v /home/worker/nginx/conf/conf.d:/etc/nginx/conf.d \
       -v /home/worker/nginx/logs:/var/log/nginx \
       nginx
-d

: run container in background -p 80:80 and -p 443:443: map host ports to container ports --name: assign a name to the container -v: bind‑mount host directories/files into the container

Both HTTP (80) and HTTPS (443) ports should be mapped to avoid service interruptions when switching protocols.

Simple Nginx Configuration Example

server {
    listen 443 ssl;
    server_name www.xxx.com; # domain with a valid certificate
    ssl_certificate     /etc/nginx/conf.d/certs/xxxx.pem;
    ssl_certificate_key /etc/nginx/conf.d/certs/xxxx.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    ssl_prefer_server_ciphers on;
    location / {
        root  /usr/share/nginx/html/admin;
        index  index.html index.htm;
    }
    location /admin {
        alias   /usr/share/nginx/html/admin;
        index  index.html index.htm;
    }
    location /s {
        proxy_pass http://127.0.0.1:8080/;
    }
}

The ssl_certificate and ssl_certificate_key files are typically placed under /etc/nginx/conf.d on the host, allowing easy replacement without rebuilding the image.

Conclusion

The guide covered changing the repository source on CentOS 8, installing and starting Docker, configuring a dedicated Docker user group, and deploying an Nginx container with a basic HTTPS configuration, providing a ready‑to‑use reference for similar server setups.

DockercontainerCentOS
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.