Explain it to me like I am a 5-year-old: What are Docker, Image, and Containers
You guys might have heard of docker a lot and a few months back even I was one of them. I heard about it so many times with words like images, containers that it felt daunting and never really understood what is the problem with the current system.
This happened to me when I was creating an application that would display reports on the analysis performed on different testing data. I created the application using Flask and it was up running but one day my friend asked me to send him my code so that even he can run it on his computer. Now came a question in mind, how do I send you this? I am afraid that sending you this will disturb the whole system and the application won’t run even if there is a single change in the configuration and code. That is when Docker comes into the picture. Docker is a great tool when it comes to deployment and running your application on different platforms. Using Docker, I can just take a screenshot of my application and put it up on dockerhub. Anyone having access to docker hub, can access my screenshot and run it the way I am on my local machine.
Thanks to Udita Gupta and her patience with answering my questions, I realized docker concepts are nothing but like a picnic where you go with a mat and a few lunch boxes with food in it. Confused, right? Let me explain to you how.
Docker is a service layer (like a picnic mat) on which you can run Docker Containers(lunch boxes) which consists of Docker Image (Food).
If you have an application that has three functionalities: Add, Subtract, Divide. You can create three different images for each functionality and run it inside three different containers. Let’s say there are more people using the ‘Add’ functionality, then you can create more containers with the same image. Each image is made up of different microservices (I know I know, this sentence is making it more confusing but I had to :P) You can imagine microservice as a service/functionality that can do only one tiny task but it can do it perfectly, for example ‘Add’ functionality is a very small task so we can consider it as a microservice. As we said image is just a screenshot of the service and the container is where the image runs. Spinning up multiple containers with the same image means you are distributing the load coming towards one specific service.
Getting into specifics…
What is Docker?
Removes the problem of application working in one platform and not working on another platform
- Docker is used heavily in the deployment phase of the application.
- There are different components to an application: Frontend, backend, databases, libraries, etc. and if you want the same application to run on different platforms then it becomes difficult.
- Docker makes it easier to deploy and run the applications using containers. So all the developer has to do is to package all the components of the application in the container and then give it to Docker. Docker makes your application portable.
- A developer creates a dockerfile where they specify the application dependencies and what is required to run the application.
- This dockerfile can be used to create a docker image. Consider the image as a xerox of the application.
- You can now duplicate the application by simply creating more instances(containers) of the image.
Docker has a client-server architecture where the command line is the client and the Docker Server(Daemon) is the server that consists of all the containers. The daemon receives a command from a docker client using CLI or REST API.
Advantages: Scalability, Portability, Simplicity and greater productivity
You can consider an image of being a screenshot of the application. When someone says that they have created a docker image of their application, that means they have taken a screenshot of their application and now that application can be run anywhere. You can find a list of images on dockerhub.com. So if I have created an application that does addition and subtraction and I want everyone to have it and able run in the same way that I am then all I have to do is to create an image of my application and put it on docker hub. So now anyone can pull my image from the docker hub, and spin up a container using it.
Consider docker images as food and now if you want to eat the food or make sure that the food is intact and you should be able to eat it elsewhere, where will you keep your food? In a tiffin/box. This tiffin/box is called a container. Every image needs a container to run in as you saw in the food and tiffin example.
- Pull docker image: docker pull <image name>:<tag> eg. docker pull ubuntu:19.02
- Gives you id numbers for all the images created: docker images -q
- Will remove the image → docker rmi <image name>
- Running a container: docker run -it <image name> [If the image is not currently on the machine then it will go to docker hub to get the image and run the image in the container] OR docker run — name <docker name> -it <image name> → This will create a container and run the docker image for eg. docker run — name NewContainer -it ubuntu will create a container named NewContainer with ubuntu running
- Display all running container: docker ps
- Starting container: docker start <container name or id>
- Stopping container: docker stop <container name or id>
- Kill the container: docker kill <container name or id>
- Remove the container: docker rm <container name or id>
That is all for this post!
Coming up posts on ‘How to create an image of a Flask App’, ‘Docker Swarm, Compose and Volume’, ‘Microservices’
Credits: Automation Step-by-Step: Raghav Pal