Imagine a box in which you can put all of your files, and it will maintain integrity. This is what Docker does, providing an easy way to manage containers for any application on-premise or cloud-hosted with ease.
What’s even better about this tool: It comes free (yes, really) as well as being open source so that everyone benefits from its use–not just those who have paid subscriptions like most big companies do these days.
Docker allows you to put your application in a container, ship it and preserve the environment, as well as make sure that all of those pesky updates don’t break it. You can access much smaller servers and reduce bandwidth since everything is self-contained.
The Docker team has been working very hard on this tool for a while now–and they have succeeded in making a very excellent and useful product. Those who have been working on or with Linux for a time can appreciate how amazing this is–finally an IT tool that works.
Docker has already been used by some large companies such as WordPress, Spotify and Reddit. It’s great to see the community using something that can be of benefit to all of us.
In this article, we will show you how to install Docker CE on your Rocky Linux system via the command line. In addition, these procedures are applicable for REHL and CentOS 8 as well.
Sudo privilege is required for installation.
Step 1: Update Rocky Linux 8 System
As usual, you should update/upgrade your system before installing anything new. This update will also prevent errors that may result from outdated dependencies.
sudo dnf -y update
It is essential to restart your system if you have any kernel-related updates.
sudo reboot now
Step 2: Adding the Docker Repo
AlamLinux 8 has a single command that allows you to add the official Docker CE repository, so installing Docker becomes much easier.
First, install the yum-utils utility
sudo dnf -y install yum-utils
Then use this command to add the Docker repo.
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Step 3: Installing Docker CE
You are now ready to install Docker CE since the repo has been added to your system.
Update the newly added repo and install the docker-ce package.
sudo dnf -y update
sudo dnf -y install docker-ce docker-ce-cli containerd.io
The command above will install Docker, but it will not allow it to start when you reboot your computer. To do this, use the following command to have the Docker service start automatically when the computer boots.
sudo systemctl enable --now docker
Restart your system, and you will see that Docker is now running when your Linux server starts up.
To check that the docker service is running, you can use the systemctl command as follows:
sudo systemctl status docker
It should return something like this:
Press q to exit back to the command prompt.
If the Docker service is not started and running, use the start subcommand to start it.
sudo systemctl start docker
Step 4: Adding New User
Once the installation is done, if we want to use Docker, we need to do it as a root user. To solve this problem, give the user access to the Docker group. Now they can run containers as a regular user.
To do this, you should add the regular user to the ‘docker’ group. This is done with the command usermod. In this example, we will add a vitux user with the usermod command as follows:
sudo usermod -aG docker vitux
If you want to add another user to the docker group, replace “vitux” with their username. Then log out and log back in to activate the docker group membership.
You have added a new user to the docker group. You can check it by the following command:
sudo id vitux
Step 5: Using the Docker Command
There are now 13 management commands and 41 general commands available to us, plus a few utility commands.
Most of these commands have the same syntax as they do on other Linux distributions.
The docker command consists of a set of functions and options and arguments.
docker [option] [command] [arguments]
To view all available options and commands, type:
Use the following command to learn more about Docker across the system.
Step 6: Testing Docker in Rocky Linux 8
To test Docker, we will do a very simple task. We want to pull the hello-world image. This image is very popular, and it will give you an idea about docker images. It’s really easy to do this task, just type:
docker run hello-world
The command searches the hello-world image on your computer. If it’s not found, the command will pull the hello-world image from Docker Hub, then automatically runs it. After that, you should see the Hello from Docker! message on your screen. This message confirms that your installation is up and running properly.
Step 7: Working with Docker Images
A container is a version of an image that can be executed. Docker gets these images from Docker Hub by default, which is a repository maintained by the organization that created Docker. Anyone can put their own pictures of their things on this site. Unlike virtual machines, which use emulation to run an operating system on the host computer (a copy of Windows or Linux), a container runs entirely within a single operating system on the host computer.
You can use the search function on the Docker Hub to look for images. To find an image, execute this command:
docker search imagename
We will be looking for the Ubuntu image in this case.
docker search ubuntu
As you can see below, there are many images available on the Docker Hub server.
If the word OK appears in the OFFICIAL column, it indicates that the image was created and is being supported by the business behind the project. You may download images for your project using the pull subcommand after you have discovered the images you want to use. In this example, we’ll use the Ubuntu operating system image.
docker pull ubuntu
It should return an output like this:
To see the images that are on your server, type:
It should return an output like this:
You can modify images and use them for building new images. This is a very efficient way to work with containers because you do not have to download the whole image every time you need it.
It’s pretty simple to modify an image, change something or add some code, then save this as a new image and share it with your friends, or whoever needs this new image by uploading it to the Docker Hub or any other Docker registry.
Step 8: Running a Docker Container
The hello-world container in the previous step was an example of a container that runs, emitting a Hello from Docker and then stops. Containers can be useful for more than just running one thing and stopping, though.
You can also run a container that will stay running, listening on a port and doing whatever you want it to do. To try this out, we’ll use the Ubuntu image and run a container.
docker run -it ubuntu
The combination of the -i and -t options tells docker to create a container and give you an interactive console to it. That sounds complicated, but it’s not.
The first time a container is started, the process that starts the container must be attached so that it can see any signals sent from bash. The -t option tells the docker which tty to open. Once the container has started, the -i option means you will get an interactive console, like this:
Now you can type any command inside the container. Suppose you want to update the list of packages inside the container. You don’t need to type sudo before each command because you are in charge of running commands as the root user of this container.
The container is effectively a microservice itself, and it has its own restrictions. Any changes you make inside the container are solely applicable to that specific container.
To quit the container shell, type exit at the prompt then hit Enter key.
In this tutorial, you’ve learned how to do some basic things with Docker. You know how to search for images on the Docker Hub and pull one of them down if it’s not already on your computer.
You know how to run a container, issue commands in that container. You’ve seen that containers are really useful for running processes in isolation.
This tutorial was pretty simple, but that’s because Docker hides a lot of complexity. At the end of the tutorial, you should be able to run an interactive container and know enough about what’s happening under the hood to try running containers with your apps inside them.