Containerization is the packaging of software in its isolated environment together with its dependencies, libraries, and configuration files. The software packages are called containers. Using this method ensures that software is packaged in a container and can run on any operating system without using the host’s resources to function. This makes containers portable and efficient to use.

Docker

Docker is a tool that is used to deploy applications in lightweight containers so that they can function efficiently in different environments. The service is hosted by Docker Engine which is the base engine installed in the host machine to run Docker containers. Docker Engine is available in both free (Docker CE) and premium (Docker EE) options. Both options are fairly good with not much of a difference between the two. The main differences between the two are the premium features on Docker EE that includes Better customer support, improved security, and certified docker images and plugins.

  • Docker CE which stands for Docker Community Edition is the free option available to use with Docker.
  • Docker EE which stands for Docker Enterprise Edition is the paid option with aim of offering premium services to enterprises looking to develop premium applications with docker.

Docker is preferred due to its favorable features that include;

  • Scalability
  • Easy to use with many tutorials available.
  • Secure by applying updates and security fixtures.
  • Setup and maintenance are seamless.

Other tools can be used to run containers without installing docker which includes Podman, Buidah, and Skopeo. These tools are command-line utilities that are used to run and operate containers without installing them on your system.

This guide will show you how to install Docker CE on CentOS 9|AlmaLinux 9.

Install Docker CE on CentOS 9 / AlmaLinux 9

Update your system packages.

sudo yum -y update

Uninstall older versions if you have any from your system. It is okay if you did not have any docker components installed.

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

Install the yum-utils package for the yum-config-manager.

sudo yum install -y yum-utils

Then set up the repository.

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

Install the latest version of docker-engine together with containerd and docker-compose plugin.

sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

PS. If you get an error while trying to install the docker packages, most specifically one with Buildah, Uninstall Buildah and run the install command once more.

Start the Docker service.

sudo systemctl start docker

Then run a hello-world image to verify docker is installed correctly.

$ docker run hello-world

Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Configure Docker CE on CentOS 9 / AlmaLinux 9

Configure Docker to start on boot

sudo systemctl enable docker.service
sudo systemctl enable containerd.service

Then add a user to the docker group to run the docker commands without using sudo.

sudo usermod -aG docker $USER

Run the following command to activate changes to the group.

newgrp docker 

To confirm the user has been added to the group, run the following command.

$ id -nG
docker wheel technixleo

Verify that you can run the docker commands without sudo using the following command.

docker run hello-world

Using Docker CE on CentOS 9 / AlmaLinux 9

Let us try to build an application. First, create a project directory and change to work on that directory.

mkdir dockerproj
cd dockerproj

Create a file called app.py and paste the following code. This file defines the application services.

$ vi app.py
import time

import redis
from flask import Flask

app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)

def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)

@app.route('/')
def hello():
    count = get_hit_count()
    return 'Hello World! I have been seen {} times.\n'.format(count)

Save and exit the file.

Create another file called requirements.txt and paste the following in.

$ vi requirements.txt
flask
redis

Save and exit the file.

Let’s create a Dockerfile that builds the docker image with the dependencies the application requires. We will be using Python.

$ vi Dockerfile
# syntax=docker/dockerfile:1
FROM python:3.9-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]

Create a file named docker-compose.yml and paste the following code which we will later use to run the container.

$ vi docker-compose.yml
version: "3.9"
services:
  web:
    build: .
    ports:
      - "8000:5000"
  redis:
    image: "redis:alpine"

Save and exit the file.

Run the following command to start up your application.

$ docker compose up

[+] Running 7/7
 ⠿ redis Pulled                                                            3.5s
   ⠿ 2408cc74d12b Pull complete                                            0.7s
   ⠿ e90389148883 Pull complete                                            0.7s
   ⠿ c6c08b6ea4d5 Pull complete                                            1.0s
   ⠿ 665852243e1b Pull complete                                            1.3s
   ⠿ be2f822ab555 Pull complete                                            1.4s
   ⠿ f663420da992 Pull complete                                            1.4s
[+] Building 2.4s (6/6) FINISHED                                                
 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 428B                                       0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => resolve image config for docker.io/docker/dockerfile:1                 1.4s
 => docker-image://docker.io/docker/dockerfile:[email protected]:443aab4ca21183e06  0.6s
 => => resolve docker.io/docker/dockerfile:[email protected]:443aab4ca21183e069e7d  0.0s
 => => sha256:443aab4ca21183e069e7d8b2dc68006594f40bddf1b 2.00kB / 2.00kB  0.0s
 => => sha256:24d064a369eda7bc7839b6c1c227eac7212d06ca09a8235 528B / 528B  0.0s

Enter http://localhost:8000/ in your browser to see the application running.

You can stop the application by using the following command on a new terminal or pressing CTRL+C on the same terminal.

$ docker compose down
^CGracefully stopping... (press Ctrl+C again to force)
[+] Running 2/2
 ⠿ Container dockerproj-web-1    Stopped                                  10.2s
 ⠿ Container dockerproj-redis-1  Stopped                                   0.2s
canceled

Uninstall Docker on CentOS 9 / AlmaLinux 9

To uninstall docker use the following command.

$ sudo yum remove docker-ce docker-ce-cli containerd.io docker-compose-plugin
Removed:
  containerd.io-1.6.6-3.1.el9.x86_64                                            
  docker-ce-3:20.10.17-3.el9.x86_64                                             
  docker-ce-cli-1:20.10.17-3.el9.x86_64                                         
  docker-ce-rootless-extras-20.10.17-3.el9.x86_64                               
  docker-compose-plugin-2.6.0-3.el9.x86_64                                      
  docker-scan-plugin-0.17.0-3.el9.x86_64                                        

Complete!

Container images and other configuration files can be removed using the following command.

sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

Conclusion

From this guide, we have installed Docker CE on CentOS 9|AlmaLinux9 systems. Docker has simplified work for developers by enabling them to package their developed software in a container together with its libraries and dependencies. This makes it easy to run, test, and upgrade the software on any platform.

Similar guides:

LEAVE A REPLY

Please enter your comment!
Please enter your name here