The Beginner’s Blueprint to Docker: Simplifying Containerization
It Was Confusing for Me Once, But You Must Know This Before K8s
Hi there, so you want to learn Docker, and if you are not finding the correct path to learn Docker or struggling to understand Docker, then you are at the right place. In this blog, I'll not only clear up the basics of Docker but also provide links to some of the best resources out there. So let's start by understanding why there is a need for Docker.
Need Of Docker
Suppose your friend is developing an application using some technologies, let's say JS and MongoDB, and it runs on their device. Now, they send it to you for review, and you're excited to give feedback. But wait, to run the application, you also need to install MongoDB and configure it. Isn't it annoying, as there are chances of errors in any step? Now comes Docker and says, "Don't worry, let's put your application in a container that has its own environment and can run on any machine without downloading the used technologies." It completely eliminates the "it works on my machine" problem. Now let me throw some more clearity on containers.
Containers
A container is essentially a layer of images that make up an application. More simply, it's a piece of software that contains all the libraries, configuration files, dependencies, and other necessary components to operate the application. Typically, it has a Linux base image due to its small size, with the application image on top. Between these layers, other images can be included as required.
Now, what exactly is an image? Basically, images are the blueprints of containers. They are sets of instructions that include packages containing all the files, libraries, and configurations needed to run a container. For more clarity, visit the Docker website.
You can find over 100,000 images on Docker Hub and use them to create containers. To clarify the distinction between images and containers in one line: an image is the actual package, while a container is the running instance of that package.
If you're familiar with Virtual Machines (VMs), you might wonder if we can't achieve the same thing with VMs.
Difference b/w VM And Docker
Docker runs much faster and is much smaller in size. As you can see in the image below, a VM creates a separate guest OS for each instance, which ultimately increases the boot time. In contrast, Docker uses a server and shares the host OS.
Now it's time for some docker commands and for this you need to install on Docker official website by clicking here.
Basic Commands
Docker pull IMAGE_NAME -> Used to pull an image from Docker Hub.You can pull any image by this command.
docker run IMAGE_NAME -> Used to create and start a new containerfrom a Docker image
docker ps -> It lists all the running containers.
You can also start and use containers using containers ID. Some of the commands used are :
docker stop CONTAINER_ID -> Stops the running container
docker start CONTAINER_ID -> Starts the stopped container
docker ps-a -> Lists running and stopped container
docker rm CONTAINER_NAME/ID -> Deletes a stopped container
Now comes the Dockerfile.
Dockerfile
You can create your own Docker Image and for this you need Dockerfile. Dockerfile is simply set of instructions which contains set of commands to build a Docker image. You'll be using Docker image to make deployment easy. You can also store these images on Docker Hub. Remember that the dockerfile should be stored with notation Dockerfile.
Run creates a layer ENV defines environment variables RUN execute linux command CMD tells what command to be run within container
Docker-Compose
Now if you more than one container so it becomes very complicated to run them on the CLI. For this create a YAML file and enter all the data in that file. It makes out task very simple. Basic commands of docker-compose are docker-compose -f name.yaml up -> starts the containers docker-compose -f name.yaml down -> stops the containers
Without docker-compose we have to write these much code and has chances of mistakes. Now, we write yaml file
And will use above commands to start and stop the containers respectively.
What next ?
Now you can manage 2 containers, 3 containers, 4 or 5 or even 10 containers but what will you do when you have large number of containers then and it's becoming very difficult to manage them. Then comes Kubernetes which is Container-Orchestration System.
At last I'm attaching some useful paid and unpaid resources so that you can have a strong grip on Docker. And I strongly suggest you to watch Tech With Nana Docker Crash Course video on YT.
At last, Thnx