What are Docker containers? Why should I use docker containers?
Updated: Sep 5, 2021
What are Containers?
A Container in general is an isolated execution environment for applications. Containers are lighter versions of execution environments compared to Virtual Machines(VMs) because they don't require a dedicated guest operating system to run. When we host an application in a Host OS or in a VM only very few parts of the full environment are used. Other parts of the OS are not needed. Base containers are created by stripping off the unnecessary parts of the OS Kernel, it only contains the runtime of the OS needed to host an application. The base containers are used to load application executable and dependencies. This makes the size and weight of a container very light. When we install a VM in an Operating System(OS) the VM also comes with its OS that runs on top of the OS in the hardware or physical machine. In a physical machine with its own OS, we can run multiple VMs each with its own guest OS. Containers do not need their own OS to run, we can directly run multiple containers on top of the OS on a physical machine. Between the OS of the machine and the containers we need a thin layer of container runtime. Just like a Java runtime that can run multiple java applications. That is why containers can also be run on VMs. The below picture shows a comparison between VM and container.
What is a Docker Container?
Docker enables us to create such containers. An application, its dependencies and the environment it requires can be bundled into a container and saved as a docker image. A Docker image can be seen as an Class where a docker container can be seen as an object of that class. Many containers can be created from an image. An Image is a skeleton or blueprint and a container is a living thing. Everytime a the application is launched the container provides a standard and consistent runtime environment. We need to instal Docker runtime to run docker containers. Docker provides CLI commands to create and manage containers. The container runtime is called Docker Engine. Docker has become the defacto standard when it comes to containerization. A Docker container can run on any machine where the docker engine is installed.
Why should I use Docker Containers?
Docker containers provide few advantages when it comes to application packaging, shipment, deployment and version control. When these aspects can be handled efficiently the productivity of developers increases. The containers make the application portable, the lifecycle management of the application becomes very easy.
Developers can put all the dependent packages and can set up the environment, for example setting environment variables and putting the application runnable into an image. Then the command to launch the application also can be specified, this will allow the application to start automatically when a container is created. The preparation for the application runtime along with the application itself is only a one time task. Each time the application needs to be launched we will have all the requirements in one place. The images can be uploaded in a central place and can be downloaded whenever needed.
We need zero effort to ship the application from development space to staging and staging to production space. We can run the same application without any discripencies If we have multiple production spaces. There is no manual effort to set up the environment in each deployment, also it reduces manual error. We just need to ship the image from one stage to another.
Application deployment and life cycle management is super easy. We just need to spin up the container. No manual steps or upfront tasks needed. As it's dead simple, we just need a single command to run the container using a CLI command and it can be automated. If the application crashes, the container will stop. We can spin up a fresh container in no time. We just need to make sure that we don’t save data within the container. We can save them in another volumn or database. The volume can be mounted in the container when the container is started. The database has to be in another container and attached with the container where the application is running. Anyone can deploy the application in as many deployment environments.
Application version control
We can bundle the new version of the application in a new image. We can save the runtime along with the version of the application in a new version of image. This way several versions of the same application can coexist. Any version can be deployed whenever needed.
Read the Docker documentation to read more about Docker Containers.