Docker For Beginners

What is docker?

 
Docker is one of the most famous and commonly used Open Source containerization technologies. This docker technology is used to build by some features of Linux kernel which Cgroups and namespaces, these features are used to isolate the containers between one another, and that’s why containers can run more processes efficiently.
 

What is the image or images?

 
Let me explain the legacy operation and what we do for operating system installation. We should download the image file of the operating system and then we do the installation. If you want to run ubuntu, centos or any other Linux based OS environment you can also download the official predefined and installed application images like WordPress, Nginx, MySQL, Oracle, MongoDB and run that image as a container.
 

What is Docker Hub?

 
Docker Hub is a Docker community centralized repository, we can download/pull the image from docker, and also you can save your customized images from your local docker engine to your docker hub account, by this you can download your image from anywhere.
 

Action 1 - Readiness setup of the Host machine

 
Prerequisites to install docker in the Redhat Linux.
  • If you are a Windows user, you can install docker directly, but we are installing docker in Redhat Linux so you may install VMplayer, Microsoft HyperV or Oracle Virtual box to install RHEL in your windows system.
  • You must have at least  4 GB ram of the system to see better performance.
  • Subscribe your RHEL OS to install Redhat packages from the RedHat repo using yum or configure yum. Without yum installation, it may take more time to install because of the dependencies.
  • You should run the following prerequisite commands if you are not a root user, use sudo before the command to run as superuser.

    yum install -y yum-utils \ device-mapper-persistent-data \ lvm2

    Docker For Beginners
  • To download docker engine from the docker repo, you should configure Docker repo by using below command

    yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
  • The below command is used to enable more rpm dependencies to install from RHELl repo and docker repo. Without enabling you can get an error to download the docker engine.

     subscription-manager repos --enable=rhel-7-server-extras-rpms

    Docker For Beginners

Action 2 - Docker setup and download the image and run as a container

 
After completing the pre-requisites, to install docker by running the following command
 
yum install docker-ce docker-ce-cli containerd.io
 
Docker For Beginners 
 
After completing the installation you can see the below completion snapshot.
 
Docker For Beginners 
 
To Run the Docker, run the command systemctl start docker

After starting the docker service you need to download the docker image, so to pull the ubuntu image run the command docker pull ubuntu
 
Docker For Beginners 
 
To check image downloaded or not run the command → docker images
 
Docker For Beginners 
 
To run the downloaded image as container run the command docker run -it -d ubuntu
 
Docker For Beginners 
 
To list the container run the command docker ps
 
Docker For Beginners 
 
To login to the image run the command with container ID docker exec -it container ID bash
 
Docker For Beginners 
 

Action 3 - How to clone container

 
From this step, we are going to login to another container and create a folder and make that container clone as an image, and then run the clone image then we are going to check whether that folder is available or not.
 
Let’s start!!
 
Login to container and list out the folder using the command ls
 
Docker For Beginners 
 
Create a new folder mkdir corner and check the folder is created or not.
 
Docker For Beginners 
 
To take container as a clone image and use at any time in future purpose.
 
Exit the container and run the command docker commit container_ID and name what you want to save the container as the image
 
I have given the command docker commit c7e807794fe3 corner
 
Docker For Beginners 
 
Let’s check the image was created or not.
 
Docker For Beginners 
 
Oh, great, the image has been created successfully!!
 
Anyway we need to assure that the image contains that created folder, and to check that we should run the container. using the following command docker run -it -d corner
 
Docker For Beginners 
 
Run the command for log-in to the container docker exec -it cde5154dda2e(container ID) bash and list out the folder using the command ls
 
Docker For Beginners 
 
Now you can see the folder which we created in the clone container.
 

Action 4 - How to install a service and allow ports to the container to access the service from outside

 
Before you start installation in a newly created container you should run the update for any type of OS container for ubuntu --> apt-get update
 
After update --> Run the command to install the apache2 service apt-get install apache2 to run the apache 2 services using the command --> service apche2 start
 
Docker For Beginners 
 
I have given the command docker commit cde5154dda2e apachegokul to convert apache installed container to an image.
 
Docker For Beginners 
 
To access the apache2 service map container port 80 to host port 8080 or any other free port and run the image as a container.
 
Docker For Beginners 
 
Login to the container using the command docker exec -it 4e948138a213 bash
 
After login run the apache2 service using the below command service apache2 start 
 
To check if the Apache server is accessible or not--> go to the browser and run the docker installed host IP address: 8080 
 
It's working fine!! 
 
Docker For Beginners 
 

Action 5 - How to Push docker image to docker HUB 

 
Next, we are going to see how to push your docker image to docker hub
 
First, you need a docker ID, if you don’t have to go to link create new account
 
To List out the images --> docker images
 
Docker For Beginners 
 
Choose and Tag which image you want to upload to your docker hub, I want to upload apache installed image docker hub.
 
So I am going to tag my apchegokul container image as flyahead/apachegokul using the command --> docker tag apachegokul flyahead/apachegokul
 
Docker For Beginners 
 
After tag you should login to docker hub using the command -->docker login --> give docker ID and password 
 
After logging in to docker hub run the command --> docker push flyahead/apachegokul
 
Docker For Beginners 
 
Now you can see in the below image the Docker image is uploaded to my account successfully.
 
Docker For Beginners
 
Thanks for reading this article.
 
I hope this article was very easy for to you understood about the docker and its basic operations.
 
If you like this article, please comment with your feedback and share it!