This Step By Step Docker Tutorial Explains What Is Docker, It’s Installation And Terminology Used With Like Docker Container, Docker Build, Docker Hub, etc.:
Docker is a containerization open-source platform that automates the deployment of applications inside containers.
Let’s look at it from the point of view of loading and unloading cargo vessels in a port. Docker helps to package the entire application with dependencies into a unit that enables better usage of resources and systems.
What You Will Learn:
What Is Docker?
Software industries today have been using virtual machines to deploy and run applications. We are well aware that the VM’s run application inside a guest operating system which again runs on hardware that is virtualized based on the server operating system.
So the VM’s package the virtual hardware & the kernel and there is a computational overhead cost involved in virtualizing the hardware. With Docker, there is no need for extra hardware for the guest operating system. All apps run as Docker containers.
Docker containers are quick to start than the VM’s as it does not contain a guest operating system. In short, Docker containers run on a single machine and virtualize the operating system while VM’s virtualize the hardware.
The below diagram shows the difference between VM’s and Container.
Terminologies Used In Docker
The following are the most common terminologies that are used in Docker.
- Docker Client: This is a command-line utility that is used when any docker commands are run.
- Docker Daemon or Engine: This is the background service that manages the containers in the operating system to which the clients talk.
- DockerHub: This is a registry of all available Docker images.
Docker Installation On Red Hat Linux 7.5
Docker is available in 2 editions as shown below.
- Community Edition (CE) is free to use. It can be installed on various platforms like Linux, Windows, and Mac
- Enterprise Edition (EE) is priced.
We will install the Community Edition using the below steps:
# yum update –y # yum install wget –y
Optional: Remove the older versions of Docker. If not installed then proceed to the next step.
# yum -y remove docker-common docker container-selinux docker-selinuxdocker-engine
Add the Docker CE repo
# wget https://download.docker.com/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo # yum -y --enablerepo=rhui-REGION-rhel-server-extras install container-selinux # yum -y install docker-ce
Start Docker service
# service docker start
Run the docker version command to check for the success of installation and version details.
# docker version
Login to the Dockerhub which is a global repository for storing the Docker Images. More about the Dockerhub is explained in the next section.
# docker login
If the following error comes up, while running the above docker login command, then PROXY settings would be required. Normally every organization would have proxy servers to connect to the internet.
Error response from daemon: Get Https://registry-1.docker.io/v2/: net/Http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Follow the instructions in this URL to set up the proxy for HTTP or HTTPS protocols.
Docker Hub is a registry service on the Cloud that stores all the images which are manually built and also helps to download the images built by other users or organizations.
Login with your ID or Create Account @ SignUp
Once you have signed up, you will be logged in to the Docker Hub.
Creating A Dockerhub Repository
Once you have logged in to the Dockerhub you can create a public repository to store the Docker images (Images are explained in detail in the next section). Some of the pre-defined images like Jenkins, MongoDB, etc. are already available and we can create and upload our images.
To create a Repository click the menu “Create -> Create Repository” and create a Repository with the name demorepo. Make sure that the visibility of the Repository is public.
Click on Create.
We will use this Repository further in our article to push and pull images.
A Docker image is a template containing the application details plus all the necessary libraries and binaries that are required to create a Docker container. These images are stored in a public repository or registry called Docker Hub. The Docker image is built using the Dockerfile. A Dockerfile consists of instructions that are required to build a Docker image.
Example of Dockerfile:
RUN apt-get y <install some-package>: To install a software package
EXPOSE 8000: to expose a port
Once the Docker file is set up, you can use the Docker build command to build the image. We will see more about building images and Dockerfile in our upcoming sections.
Running First Docker Image
Let’s run a pre-defined image which will create an instance of the image called a Container.
# docker run hello-world
The Docker client contacts the Docker daemon and it pulls the pre-defined “hello-world” image from the Docker Hub and creates a new container from the image from which the executable is run thereby showing the output.
Docker Image Commands
#1) To list all the Docker images installed on the system that is created by the BUILD command and also the ones downloaded from Docker Hub.
# docker images
Every image contains a TAG (Label), Image ID to uniquely identify each image, the number of days since the image was created and the size.
#2) To remove the Docker images use the following command.
# docker rmi<ImageID>
Use –f to force Remove the image if it is in use.
#3) To view the details of the Docker images run the following command.
# docker inspect <Name of Image>
The above command will help to get the metadata of the image.
#4) To view all the commands that were run within an image use the Docker history command.
# docker history <ImageID/ImageName>
Containers are instances of the Docker images that we saw earlier. It is run using the Docker run command. The purpose of Docker is to run containers.
In the below screenshot we have an image called ‘hello-world’ and will issue a ‘run’ command as below.
# docker run –it hello-world
The -i and -t options that are used together, create the bash shell terminal inside the newly launched container. Docker Containers are uniquely identified with an ID and can be found by running the command as given below.
# docker ps –a
The –a switch will show all the containers in use and the stopped ones as well.
The above screen shows the name of the containers that are created randomly like trusting_morse, jovial_lichterman, etc. In case you need to name the container based on the project needs then you can do that using the –-name switch while we run the container.
# docker run –-name my-java-app –it java-app
# docker ps –a
(This command will show the name assigned to the container.)
Deleting Docker Container
Use the below command to delete the Docker container.
# docker rm <Container ID>
The Dockerfile is used to build the images. It is a simple text file consisting of instructions or commands.
As you are aware that the Docker Hub consists of a repository of images both pre-defined and custom ones uploaded. A Dockerfile is required to create a custom image.
Just follow the below 3 steps to create a custom Docker image.
- Create a file named “Dockerfile” using the vi editor or any other editor in Linux. Note that ‘D’ is in uppercase.
- Build the image using the file.
- Upload the image to the Docker Hub.
#1) Dockerfiles start with a FROM instruction that helps to use a BASE image. If we need to use Java for running our application, then we can use the official image that is available i.e. java:8. The first line of the one that uses Java 8 version will be as follows.
#2) Typically in development, the files to be run or deployed could be placed in a target location.
For Example, if a developer is working on a file called HelloWorld.java and the file is saved in the location /root/javaproject/target then we will need to use the Docker ADD instruction to copy the files from the above source location and include it into the image.
There is a similar instruction called COPY which also has the same behavior as that of ADD. The only difference to use ADD instruction is in case you want to extract any TAR files into the image in addition to copying the files and directories to the destination folder.
An example of this could be as follows.
If the source is a TAR file then it will be extracted as the directory into the image.
ADD projectsource.tar /home/niranjan
If the source is URL, then the file will be downloaded and copied to the destination into the image.
#3) The next instruction that can be set anywhere is a non-executing one called MAINTAINER. It just declares an author. It should be defined after the FROM instruction.
#4) If there is a need to set the working directory before executing any command or executable then you can do it using the WORKDIR instruction.
ADD . /home/niranjan
In the above when the pwd is run it will show the path as /home/niranjan
#5) The next instruction that is mainly used to install a new application or package is RUN. It can also be used to execute any command. Usually, there will be multiple RUN instructions in the Dockerfile.
RUN [“executable”,”param1”, “paramN”]
RUN apt-get –y update
RUN apt-get install –y httpd
When an image is built and launched, the container uses CMD instruction to execute. Thus there is a default command that is set with the parameters and can be changed by passing the parameters from the command line when the Docker run command is issued.
Unlike the RUN instruction, if there are multiple CMD instructions then only the last CMD instruction is executed.
CMD [“executable”,”param1”, “paramN”]
CMD [“echo”,”Hello World”]
#6) The next instruction is ENV that helps to set environment variables in the container.
In our previous section, we saw a few instructions that need to be put into the Dockerfile to build an image. In the format and example shown below the following command will help to build the image and we will see how to push the same to the Dockerhub.
# docker build –t Imagename:Tagname DIR
-t : To mention TAG name for the image
Imagename: A name for your image
Tagname: A Tag to be given for your image
DIR : The location of Dockerfile
MAINTAINER Niranjan V
add . /home/niranjan
RUN javac HelloWorld.java
CMD [“java” , “HelloWorld”]
# docker build -t vn-helloworld:V1 .
# docker images
Docker RUN Image
# docker run vn-helloworld:V1
Docker Push Image to Dockerhub
#1) Firstly TAG the imageID to the repository on Dockerhub.
Format: docker tag imageIDreponame
# docker tag 9c7db40e0d6c vniranjan1972/demorepo:V1
- imageID can be taken from the ‘Docker images’ command.
- reponame is the Dockerhub Repository name.
#2) Issue the Push command. Make sure you log in to the Dockerhub using the ‘Docker login’ command before the command is run.
# docker push vniranjan1972/demorepo:V1
Login to the Dockerhub to view the pushed image.
As the images are all uploaded and stored in the Dockerhub there will be a need to pull the images on to your environments which are usually your DevOps environments like Dev, QA, Staging, etc. Now, let’s see how to pull the image which was uploaded in the previous section from the Dockerhub.
Firstly we will run the Docker images command to list and see whether the image is existing or not.
# docker images
The image vniranjan1972/demorepo:V1 is not existing locally on this machine or environment.
Now give the pull command and run the image.
# docker pull vniranjan1972/demorepo:V1
Run the Image
# docker run vniranjan1972/demorepo:V1
If we look at the list of containers (docker ps –a) then the above run image will be listed under the Exited containers. This is because the program has run and finished the execution.
Whereas the command docker ps will show only the running containers. An example of such running containers could be when Tomcat, Jenkins, JBoss has to be running. We will see the examples of running pre-defined images that will be used in the DevOps scenarios of CI and CD.
In this article, we have seen the fundamentals of Docker and how it is compared with VM’s. This part also concentrated on the installation and usage of docker especially on how to build images and run them in the containers.
Suggested reading =>> MySQL Docker Container
Further reading =>> Docker vs Kubernetes