Docker is a tool that allows you to easily build, test and deploy applications smoothly and quickly using containers. It has gained widespread popularity in recent times due to the portability to run applications anywhere irrespective of the host operating system.
Docker provides a more efficient and lightweight environment to deploy the application. Docker uses Kernel’s features such as cgroups and namespace to run a container on a single os instance.
Docker is now available in two editions,
- Community Edition (CE)
- Enterprise Edition (EE)
Here, we will install Docker Comunity Edition (CE).
Uninstall Older Version
Uninstall older versions of Dockers, named docker or docker-engine along with associated dependencies.
dnf remove -y docker-common docker container-selinux docker-selinux docker-engine
Do not worry about the contents inside /var/lib/docker/, all will be preserved.
Install Dependent Packages
Then, install the required packages.
dnf install -y lvm2 device-mapper device-mapper-persistent-data device-mapper-event device-mapper-libs device-mapper-event-libs
Add Docker Repository
Let’s add the Docker community edition repository for the Docker installation.
dnf install -y dnf-utils dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Install Docker on Rocky Linux 8 / CentOS 8 / RHEL 8
Install Docker packages using the below command.
dnf install -y docker-ce docker-ce-cli containerd.io
E.g. yum install docker-ce-19.03.5-3.el7
You can list the available Docker versions with yum list docker-ce –showduplicates | sort -r
Now you have Docker installed onto your machine, start the Docker service in case if it is not started automatically after the installation
systemctl start docker systemctl enable docker
Check the Docker service.
systemctl status docker
Verify Docker Installation
Once you start the Docker service, you can run a simple “Hello World” container to verify the installation.
docker run -it rockylinux/rockylinux echo Hello-World
When you run the docker run command, the Docker creates and starts the container with the base image of Rocky Linux.
Since we are running centos container for the first time, the output will look like below.
Unable to find image 'rockylinux/rockylinux:latest' locally latest: Pulling from rockylinux/rockylinux 1b474f8e669e: Pull complete Digest: sha256:8122f31fbdd5c1368c6b7d5b9ae99fec2eb5966a5c967339d71e95c4a3ab7846 Status: Downloaded newer image for rockylinux/rockylinux:latest Hello-World
At first, the Docker looks for Rocky Linux image locally, and if it is not found, it starts downloading the Rocky Linux image from the Docker registry (one time). Otherwise, it uses the already downloaded Rocky image.
Once the image has been downloaded (in our case), it will start the container and echo the command Hello-World in the console which you can see at the end of the output.
Allow Non-root access
By default, only users with root or sudo (root) privilege can run Docker containers. To allow non-root users to run Docker containers, you can follow the below steps.
Create a group called docker if it does not exist, run the following commands with root privileges.
Add a user that is to be a part of the docker group. Replace raj with your username.
Add a user to the docker group.
usermod -aG docker raj
Now you can run a Docker with a non-root user.