How to Install Docker and Pull Images from Docker Hub
Introduction
Whenever you build a project it works fine on your local machine but when you move this project to other machine the performance of your project decreases. This is the place where docker comes into the picture. Docker allow applications to be shuttled easily between environments.
Docker is a software platform to create, test and deploy applications in an isolated environment. Docker uses container to package up an application with all of the parts it needs including, libraries and dependencies. It allows applications to use the kernel and other resources of the host operating system this will boost the performance and reduce the size of the application.
Docker Hub is a centralized repository service that allows you to store container images and share them with your team. You can use Pull and Push command to upload and download images to and from the Docker Hub.
In this tutorial, we will show you how to install Docker CE and install several images from the Docker Hub on CentOS 8.
Prerequisites
- A server running CentOS 8 with a minimum 4 GB RAM.
- A root password is setup on your server.
Disable SELinux
By default, SELinux is enabled in CentOS 8. So you will need to disable the SELinux to allow Docker containers to work internally. You can disable it by editing /etc/selinux/config file:
nano /etc/selinux/config
Change the following line:
SELINUX=disabled
Save and close the file then restart your system to apply the changes.
Install Docker CE
By default, Docker CE is not available in the CentOS 8 default repository so you will need to add Docker CE official repository in your system.
You can enable the Docker CE repository using the following command:
dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
Once the repository is enabled, install the latest version of Docker CE using the following command:
dnf install docker-ce --nobest -y
Once the installation has been completed, start the Docker CE service and enable it to start at reboot with the following command:
systemctl start docker
systemctl enable docker
You can also verify the installed version of Docker CE using the following command:
docker -v
You should get the following output:
Docker version 19.03.13, build 4484c46d9d
I have written a detailed tutorial on Docker installation. Checkout https://www.usessionbuddy.com/post/How-To-Install-Docker-On-Centos-8/
Docker Hub Nginx
In this section, we will show you how to download Nginx image from the Docker Hub and run it inside the container.
First, download the Nginx image from the Docker Hub registry using the following command:
docker pull nginx
Once the Nginx image is downloaded, you should get the following output:
Next, create a new Nginx container from the downloaded image and expose it on port 80 using the following command:
docker run --name docker-nginx -p 80:80 -d nginx
You can also verify your Nginx container with the following command:
docker ps
You should get the following output:
You can connect to the running container with the following command:
docker exec -it docker-nginx /bin/bash
You should get the container shell as shown below:
You can now run any command inside the container.
Run the following command to exit from the container shell
exit
Docker Hub Redis
First, download the Redis image from the Docker Hub with the following command:
docker pull redis
You should get the following output:
Next, create a Redis container from the downloaded image with the following command:
docker run --name docker-redis -d redis
Next, verify the Redis container with the following command:
docker ps
You should see the following output:
Next, connect to the Redis container using the following command:
docker exec -it docker-redis /bin/bash
Now, run the redis-cli command to verify the Redis:
redis-cli
You should get the Redis shell as shown below:
Now, exit from the Redis shell and container with the following command:
exit
Docker Hub MongoDB
First, download the latest version of MongoDB image from the Docker Hub with the following command:
docker pull mongo
You should get the following output:
Next, create a container from the Downloaded image and expose the port 27017 for external access with the following command:
docker run --name docker-mongo -p 27017:27017 -d mongo
Next, verify the running container with the following command:
docker ps
You should get the following output:
As you can see, MongoDB container is listening on port 27017 and accessible from remote machine.
Next, connect to the MongoDB container with the following command:
docker exec -it docker-mongo /bin/bash
Next, verify the MongoDB container with the following command:
root@335c83c0033c:/# mongo
You should see the Mongo Shell in the following output:
Now, exit from the MongoDB shell with the following command:
exit
Docker Hub Alpine
First, download the Alpine image from the Docker Hub registry with the following command:
docker pull alpine
You should get the following output:
Next, create a new Alpine container from the downloaded image with the following command:
docker run --name docker-alpine -dit alpine sh
Next, verify the running container using the following command:
docker ps
You should get the following output:
Next, connect to the Alpine container using the following command:
docker exec -it docker-alpine sh
Now, run the following command to check the version of Alpine:
/ # cat /etc/os-release
You should get the following output:
Conclusion
In the above guide, we learned how to install Docker CE CentOS 8. We also learned how to download Nginx, MongoDB, Alpine and Redis image from the Docker Hub and create a container for each. I hope this will help you to download your desired Docker image and create a container.