Docker is an application that delivers software in certain packages called containers, which are highly convenient and resource-friendly units dependent on the host OS. Docker uses operating-system-level virtualization to run software applications in a container.
In order to install Docker on CentOS, there are 2 methods to choose from:
- The first method is to install it on an existing application of CentOS, and
- The second method is to spin up a server using the Docker Machine tool and then run the auto installation.
We will only discuss the first method here – and save the second method for some other article – with a detailed step-by-step guide. It will help anyone looking to install Docker on CentOS. The process of using Docker will also be mentioned in detail.
Before moving on to the tutorial, there are certain things that you need to install Docker on CentOS successfully. These are:
- A 64-bit version of CentOS 7 Droplet or CentOS 8. Keep in mind that archived versions may not work because they are not supported or tested.
- Usually, the ‘centos-extras’ repository is enabled by default, but in case you have disabled it, you have to make sure that it is re-enabled.
- The ‘overlay2’ storage driver.
- You need to uninstall older versions of Docker, if any. The old versions were generally called ‘docker’ or ‘docker-engine’.
Note: Use the following code to uninstall old versions of Docker:
$ sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
How to Install Docker on CentOS?
In this section, you will be guided step-by-step on how to install the latest version of Docker from the official Docker repository.
Step 1: Docker Installation
Update the package database by running the following command:
$ sudo yum check-update
The following command will download and install the latest version of Docker from the official Docker repository:
$ curl -fsSL https://get.docker.com/ | sh
When the installation is completed, you need to run the following command to start the Docker daemon:
$ sudo systemctl start docker
Run this command to verify that Docker is running:
$ sudo docker run hello-world
The above command will download a test image and will run the same in a container. The container will print a message “Hello World”, and exit when it runs.
When you pulled the image from Docker Hub, you must have received the following output:
Hello from Docker. This message shows that your installation appears to be working correctly. ...
Now, you need to run the following command so that Docker starts after every reboot:
$ sudo systemctl enable docker
Now since you have the Docker service (daemon), you also get the docker command-line utility. Using the same will be further explained in detail.
Step 2: Run Docker Command without Sudo
For running the ‘docker’ command, it requires the prefix ‘sudo’ as it requires root privileges. However, you can let go of the ‘sudo’ prefix if you run the command in the docker group. The installation of Docker also creates the Docker group by itself. To be able to do that, you need to follow the given steps:
Add your username to the docker group by running the following command:
$ sudo usermod -aG docker $(whoami)
Now, in order to allow this change, you have to log out of the Droplet and then log in as the same user. You can also add another user to the Docker group even if you are not logged in as that user by running this command:
$ sudo usermod -aG docker username
Therefore, now you can easily run the ‘docker’ command as a user in the Docker group without the use of the prefix ‘sudo’. The rest of this tutorial will assume that you have made this change. If you have chosen not to, then add ‘sudo’ to every command.
Step 3: Running the Docker Command
Since Docker is now installed and working fine, you can start running the docker command-line utility. Let’s take a look at how you can do it:
Start with running the following command:
$ docker [option] [command] [arguments]
You can view all the available subcommands by typing:
The full list of subcommands available in Docker 1.11.1 is given below:
attach Attach to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on a container or image kill Kill a running container load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container network Manage Docker networks pause Pause all processes within a container port List port mappings or a specific mapping for the CONTAINER ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart a container rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop a running container tag Tag an image into a repository top Display the running processes of a container unpause Unpause all processes within a container update Update configuration of one or more containers version Show the Docker version information volume Manage Docker volumes wait Block until a container stops, then print its exit code
You can view the available switches to a certain command by running this command:
$ docker docker-subcommand --help
If you want to see the system-wide information on Docker, run this command:
$ docker info
Step 4: Docker Images
Docker Images pull images from Docker Hub by default, which, in turn, runs Docker containers. Any user is allowed to create and host their own Docker images on Docker Hub.
In this article, we discussed how to pull a Docker image when we verified that Docker is running. We pulled an image titled ‘hello-world’ from Docker Hub, which is a part of the Docker project.
Docker also allows the users to search for images among the Docker Hub collection and a user needs to type the following command to search, for example, for the CentOS image:
$ docker search centos
Then you will observe a listing of all of the images whose name matches with your search string. The output will look somewhat like this:
NAME DESCRIPTION centos The official build of CentOS. jdeathe/centos-ssh CentOS-6 6 . 7 x86_64 / CentOS-7 jdeathe/centos-ssh-apache-php CentOS-6 6.7 x86_64 / Apache / PHP / P milion12/centos-supervisor Base CentOS-7 with supervisord launche nimmis/java-centos This is docker images of CentOS 7 with torusware/speedus-centos Always updated official CentOS docker nickistre/centos-lamp LAMP on centos setup
After choosing the image you want to use, you can use the ‘pull’ subcommand and download it on your system by typing this:
$ docker pull centos
Then you can run the image that you have downloaded by using the ‘run’ subcommand like this:
$ docker run centos
If you wish to view the images that you have downloaded on your computer, then you can run the following command:
$ docker images
The output should look something like this:
[secondary_lable Output] REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 778a53015523 5 weeks ago 196.7 MB hello-world latest 94df4f0ce8a4 2 weeks ago 967 B
Step 5: Run a Docker Container
We ran the ‘hello-world’ container earlier in this tutorial. That container was the type of container that first runs and then exits after giving an output message.
Containers can do so much more than that as they are similar to virtual machines. Now, let us take a look at how to run a container with the CentOS image that we just downloaded on our computer. You can use the combination of the -i and -t switches for having interactive shell access into the container:
$ docker run -it centos
The output that you will receive will be like this:
After this, you are enabled to run any command you want in the container.
This step-by-step guide will help you get started with Docker by providing you the basic knowledge of the same so that you may develop further on that by putting your knowledge to practical use.
That sums up this brief introductory tutorial on installing and using Docker on CentOS. The containerization platform can be used for much more useful purposes as it is very resource-friendly. You are welcome to read more about Docker on the official website to get more out of the powerful containerization platform.