Install Docker Engine On Centos

admin14 April 2024Last Update :

Prerequisites for Docker Installation on CentOS

Before diving into the installation process, it’s essential to ensure that your system meets the necessary requirements. Here are the prerequisites for installing Docker Engine on CentOS:

  • A CentOS 7 or CentOS 8 system with a non-root user with sudo privileges.
  • An internet connection for downloading Docker packages.
  • Ensure that the yum package manager is up-to-date by running sudo yum update.
  • 64-bit version of CentOS as Docker does not support 32-bit versions.

Uninstalling Old Versions of Docker

Older versions of Docker were called docker or docker-engine. If these are installed, they need to be removed before we install the newer version known as Docker Engine.

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

Setting Up the Docker Repository

To install Docker Engine, you first need to set up the Docker repository on your CentOS system. This will allow you to install and update Docker from the repository using the yum package manager.

Installing Required Packages

Install the required packages for managing the repositories.

sudo yum install -y yum-utils

Adding the Docker Repository

Use the following command to add the Docker CE repository to your system.

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

Installing Docker Engine

With the repository in place, you can now proceed to install Docker Engine.

Installing the Latest Version of Docker Engine and Containerd

Run the following command to install the latest version of Docker Engine and containerd.

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

Starting and Enabling Docker

Once the installation is complete, start the Docker service and enable it to launch at boot.

sudo systemctl start docker
sudo systemctl enable docker

Verifying the Installation

Verify that Docker Engine is installed correctly by running the hello-world image.

sudo docker run hello-world

This command downloads a test image and runs it in a container. If the container runs successfully, it indicates that Docker has been installed correctly and is functioning as expected.

Configuring Docker to Start on Boot

It’s generally a good practice to have Docker start automatically when the system boots. The earlier step of enabling Docker should suffice, but here’s how to confirm or change this setting.

sudo systemctl enable docker

To disable this behavior, simply use:

sudo systemctl disable docker

Working with Docker as a Non-Root User

By default, the Docker daemon binds to a Unix socket instead of a TCP port. By default, that Unix socket is owned by the user root, and so, by default, you need to access it with sudo. To avoid typing sudo whenever you run the docker command, add your username to the docker group.

sudo usermod -aG docker ${USER}

Log out and log back in so that your group membership is re-evaluated.

Updating Docker Engine

To update Docker Engine, simply update the package index and then install the newer version.

sudo yum makecache fast
sudo yum -y install docker-ce

FAQ Section

How do I uninstall Docker Engine?

To uninstall Docker Engine, along with all containers, images, and volumes, use the following commands:

sudo systemctl stop docker
sudo yum remove docker-ce docker-ce-cli containerd.io
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

Is it possible to install a specific version of Docker Engine?

Yes, you can list available versions in the repo and select a specific one to install.

yum list docker-ce --showduplicates | sort -r
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

Replace <VERSION_STRING> with the desired version.

What if I encounter a ‘Cannot connect to the Docker daemon’ error?

This error usually means that the Docker daemon is not running. Start it with:

sudo systemctl start docker

If the problem persists, check the status of the Docker service for more information.

sudo systemctl status docker

Can I install Docker on CentOS 6?

Docker no longer supports CentOS 6. It’s recommended to upgrade to at least CentOS 7 to use Docker.

Do I need to configure a firewall for Docker on CentOS?

Docker manages iptables rules to provide network isolation between containers by default. However, if you have an existing iptables-based firewall, you may need to manually open ports or configure it to work with Docker.

References

Leave a Comment

Your email address will not be published. Required fields are marked *


Comments Rules :

Breaking News