Lets talk about the most common problem the industries were facing before the terms containerization and docker.
Here we can see a developer has built an application that works fine in his environment but when it reaches production, there were certain issues with that application which happens because of the difference in the computing environments between Dev and prod.
This is where containerization and Docker come in. Docker is a containerization platform that solves the libraries and dependency issues once and for all. As the tester and developer now have the same system running on Docker container, they both are able to run the application in the Docker environment without having to face differences in dependencies issues as before.
In simple terms, you can now wrap up all the pieces your software needs in a single unit called a docker image, then ship or share this image with anyone. And, as long as the recipient has Docker, they will be able to run or test your project. Gone are the days of, “But it worked on my machine!”
Container refers to a lightweight, stand-alone, executable package of a piece of software that allows a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that the machine might have that could differ from the machine used for writing and testing the code.
In a way, Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications to be shipped with things not already running on the host computer. Also, containers are created and booted in seconds, use fewer resources while a VM takes time to boot and use more resources.
- The server is the physical server that is used to host multiple virtual machines. So this layer remains the same.
- The Host OS is the base machine such as Linux or Windows. So this layer remains the same.
- Now comes the new generation which is the Docker engine. This is used to run the operating system which earlier used to be virtual machines as Docker containers.
- All of the Apps now run as Docker containers.
The clear advantage in this architecture is that you don’t need to have extra hardware for Guest OS. Everything works as Docker containers.
Now, A developer can create a code, without having to worry about the packing of the application and uploading it to other environments within a short duration, and finally, delivering a properly working application that works fine on all the machines.
Docker Registry :
Is a repository for Docker images. Using the Docker registry, you can build and share images with your team. A registry can be public or private. Docker Inc provides a hosted registry service called Docker Hub. It allows you to upload and download images from a central location. If your repository is public, all your images can be accessed by other Docker hub users. You can also create a private registry in Docker Hub, Google Cloud Registry, Amazon Registry, etc. Docker hub acts like git, where you can build your images locally on your laptop, commit it, and then can be pushed to the Docker hub.
Some Basic Docker Commands :
- docker version: Shows docker version
- docker images: list all images
- docker ps: shows all running containers
- docker ps -a: shows all containers, running + exited
- docker ps -aq: shows the id of all containers
- docker search <image_id> : search images
- docker rm <container_id> : removes a container
- docker rmi <id> : removes an image
- docker pull ubuntu:20.04: pulls an ubuntu image from docker hub of version 20.04
- docker login: login to docker hub
- docker push: pushes images from local to docker hub
- docker stats <id> : displays resource utilization of container
- docker logs <id> : Generate logs of container
- docker run <image>: pulls an image from docker hub and runs as a container
- docker tag: provide a tag to the docker image
Creating and building images using a Dockerfile :
A Dockerfile is a text file that defines a Docker image.
We can use a Dockerfile to create our own custom Docker image, in other words, to define our custom environment to be used in a Docker container. The Dockerfile acts like a recipe for your image, what OS you need, what libraries to install, what variables to use, and so on.
The workflow to follow using Dockerfile :
- Create the Dockerfile and define the steps that build up your images
- Execute the
docker buildcommand which will build a Docker image from your Dockerfile.
- Use this image to start containers with the
How to Write a Dockerfile?
Every Dockerfile must start with the
The idea behind this is that you need a starting point to build your image. The image you start from is called the base image.
The next step is to build an image using the Dockerfile using
docker buildand then running a container using
Wrapping Up :
I hope you’re now equipped with the knowledge you need to learn about Docker and maybe even use it in a project one day.
As always, drop me a line in the comments if I’ve made any mistakes or can be helpful in any way! :)