Docker gains popularity in recent years as it solves many software development issues by containerizing the application. It allows us to ship the application together with all the dependencies that it needs in the machine in one container resolving the issue that it only works on certain machines. This guide will help you to install Docker on CentOS or in any RHEL based Linux machine. You can also follow our guide here on how to install CentOS 8 in a VirtualBox.
First, check if centos-extras repo is enabled in your machine. Run the command below and you should see centos-extras in your repo list.
yum repolist -v
If you have an older version of Docker, uninstall it first. Otherwise, you can skip this step. To uninstall old docker, run the command below.
sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
Add the Docker repository in your CentOS machine. To add, install the yum-utils first and then add the repo.
sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Now that we have our prerequisites installed, we are now ready to setup Docker. First, we update the repository.
sudo yum -y update
In case that you are getting an error that containerd.io conflicts with runc, or similar below,
Then, please try this command. This will remove conflicting podman and buildah:
sudo yum erase podman buildah
Next, install Docker by running the command:
sudo yum install docker-ce docker-ce-cli containerd.io
If you have successfully installed Docker, then you can skip this step and proceed to configure Docker section. Otherwise, if it fails that the docker-ce requires containerd.io version to be higher than a specific version, then you can fix this by installing the containerd.io manually. This happens when that specific version of containerd.io is not yet added to the CentOS repository. You might be seeing something like this:
To fix this, visit https://download.docker.com/linux/centos/7/x86_64/stable/Packages/ and check for the latest version of containerd.io. Get the link for containerd.io rpm file and install it using yum install. For example:
sudo yum install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
Then, install Docker again but this time, without containerd.io since we already installed it manually.
sudo yum install docker-ce docker-cli
Docker is now installed but we still need to do some configurations. First, we need to add our user to the docker group. To check who is included in the docker group, run the below command:
cat /etc/group | grep 'docker'
If your user is not included, then we can add it using sudo usermod -a -G docker <username>. For example, using javapointers as username:
sudo usermod -a -G docker javapointers
Then, let’s verify again if our user was now added in docker group.
Next, we will add the docker as a service to automatically starts at boot. We can add them using:
sudo systemctl enable docker
Restart your VM and finally, test it by running hello-world image. Open a terminal again and type:
docker run hello-world
And if you see something similar below, then you have successfully installed Docker on your CentOS or RHEL based machine.
That’s it! Please let us know in the comments if this guide helps you or if there are issues you’ve encountered while following this guide.