Docker For Beginners - Part 1

What is Docker?

It's a platform for building, running, and shipping applications in a consistent manner across different machines. So if your application runs well on your development machine, it should be working fine on other machines.

For example, when I was working with an "http-pdf" package, I noticed the pdf created on my windows system was different when it ran on a Linux server.

The reason why outputs can be different for different servers are as follows,

  • One or more files missing, or not included as part of your deployment.
  • If the target machine is running a different version of some software, that your application needs
  • Different environment variables.

This is where Docker comes to the rescue!

  • With Docker, we can easily package up our application with everything we need, and consistently run it on any machine. So basically we can take this package, and run it easily on any machine running docker, and end up with consistent results.

  • Another advantage is, as a new guy on a project, I don't have to spend an entire day setting up my machine to run a code. We simply tell docker to bring up our application, and docker itself with download and run its dependencies inside an isolated environment called Container.

  • Another beauty of this application is we can run our applications simultaneously in different containers, holding different versions, side by side, without messing with each other.

Docker helps us consistently build, run and ship our applications.

Virtual Machines vs Containers

A virtual machine is an abstraction of a machine, we can run several virtual machines on a single machine, using Hypervisor( a software we use to create/ manage virtual machines). Although you can run an application in Isolation on a virtual machine, the downside is

  • Each Virtual Machine needs a full-blown copy of an OS, thereby also being slow to start.
  • Another reason why is that each Virtual Machine is resource intensive. That means the virtual machine takes a slice of your actual system's resources.

A Container on the other hand gives us the same kind of isolation, i.e allows you to run multiple apps in isolation.

  • They are lightweight, which means they don't need a full OS to start and run. which means all containers share the operating system of the host.
  • Since the OS is already loaded, the container can start quickly.
  • And since they use a single OS, you don't have to allocate resources for each container.

Architecture of docker

Docker has a Client/Server architecture. The client talks to the server/Docker Engine using a rest API. The Docker Engine focuses on building and running docker containers.

image.png

Installing Docker

docker version check the version of docker // current working version > 20.10.17

Development Workflow

The overall workflow is summarized as below,

  • We take an application and dockerize it, basically making a small change in the program folder, so it can be run by docker. We just add a docker file to it. File Name: Dockerfile
    Side Note: The Dockerfile has no extension.

Dockerfile is basically a plain text file that includes instructions that package up the application into an image. This image basically holds all information that our application needs to run, such as:

  • A cut down OS,
  • A runtime environment (Node.js)
  • Application files
  • Third-party library
  • Environment variables

image.png

Once we have an image, we tell docker to create a container, where we can run this Image. based on the above line, we know that a container has its own file system.

So instead of running the application on a process, we tell docker to run it in a process inside a container, in an isolated environment. The beauty of having an image is we can push it to say a registry such as docker.hub (similar to Github). Then we can pull it into any machines and run it on any machines running docker.

Source: shorturl.at/jtvX8