Docker is a very popular tool in this fast-growing IT world. Many large and small-scale organizations, as well as startups, are continuously using docker in the production environment in their projects.
It is a tool designed to ease the process of development, deployment, and running of applications by using containers. This blog is an introductory guide to docker and covers the following concepts-
- Virtual Machines or VMs
- What is Docker
- Why we use Containers
- Docker Client & Server (Daemon)
- Docker Images & Container
Virtual Machines or VMs:
Before using containerization concept in the applications, we were using virtual machines to isolate and organize applications and their dependencies into VMs. So, virtual machines run many multiple applications in the same physical hardware and this process is called Virtualization.
But we know that virtual machines are ponderous in size and as we need to run multiple VMs, it leads towards terrible performance. Also, it creates several other problems, such as portability, software updates, or continuous delivery. These drawbacks of Virtualization were later ruled out by the introduction of a new concept, called Containerization.
What is Docker?
Docker is a tool or a set of platform-as-a-service (PaaS) products that use the concept of OS-level virtualization to deliver the software in packages called Containers. We create containers that contain the software and its resources or dependencies after that we dockerize our application and can use it anywhere like in Mac, Linux, or windows.
As you see in the above figure, all the application libraries run inside separate containers. This shows that each application is independent of the other and no application can interfere in the execution of other applications.
Why Do We Use Containers?
Talking about containers, let me tell you a thing about them. Containers have the host operating system instead of the guest operating system. So, it can share relevant libraries and resources whenever needed.
Containers are lightweight in comparison to VMs as they share OS kernel and starts up faster and use a fraction of sections to run. So, yes, we can say that this is a better approach than using Virtual Machines in more than one way.
Docker Client & Server:
Here are two key concepts of docker-
- Docker Client: It is a tool that we issue commands to reach out to Daemon(server).
- Daemon: It is a service that runs on your host operating system and responsible for running Containers.
Docker Image & Container:
- A docker image is basically a template containing all the instructions for creating a container. It carries all the data which is required to build a container.
- A container is the running instance of docker image that holds the required dependencies and packages needed to run the application in a particular environment.
As shown in the above figure, a docker file is created and when we run the build command, it converts to an image and after running it, it finally converted into a container.
A Dockerfile is a text document that contains all the information required for running your software in a base container. It contains all the commands that an end-user can call in the command line to build the docker-image.
Steps to create a custom image –
- Choose a Base Image.
- Download and Install dependencies.
- Setup the Startup command
Let us take an example of creating a Dockerfile as shown in the below image –
- Firstly, we inherited the official Node.js image instead of creating our own base image because it has all the packages and dependencies to run our Node.js application.
- After that, specify the node.js environment like development or production.
- Let’s create a working directory. This instructs docker to use that particular path as the default location for all subsequent commands.
- Then, we need to get our
package-lock.jsonfiles from our image by using the
- Now, we run
RUNcommand so that it runs the corresponding command like
install node modulesin the .3. directory of our image.
- For adding source code, we have to Run
Copy . .command and that command add our source code into the image.
- And finally, we run
CMDcommand which tells docker that this command will be run whenever our image is run inside the container.
- Here is the complete explanation of Dockerfile.
- After that, you have to save it and run
docker buildcommand inside the terminal window.
- By this command, you can give a name to the image
docker build --tag node-dockerwhere,
node-dockeris the image name.
Docker has several reasons that differentiate it from others and make it popular among the programming community. Firstly, it boosts up your software delivery cycles. It is easier to put new versions of software into production quickly and we can also roll back to a previous version if needed. Furthermore, it also solves the problem of application portability.
Hope you liked this blog and it helped you get the basic knowledge about Docker so that you can consider using it in your next project.