Docker is a containerization platform that enables you to create, deploy, and run applications conveniently with the help of containers. It is basically concerned with the packaging of applications with all their required libraries and other dependencies in a lightweight containers by the developer. As containers has its own isolated operating system layer with Linux based image that means everything packed in one isolated environment.
Why to use ?
- Provides Isolated Environment: Docker takes the responsibility of isolating and segregating your apps and resources in such a way that each container becomes able to access all the required resources in an isolated manner i.e., without disturbing or depending on another container. It eventually allows you to run multiple containers simultaneously on the same host. Moreover, as each container is only allowed to access the assigned resources – it helps in reducing the risk of several potential issues such as downtime, etc. Also, any app can easily be removed by deleting its container, and it will not leave behind any temporary files, etc. on the same system.
- Helps in Rapid Application deployment: Docker indeed fastens the application deployment process to a greater extent. It creates a container for every individual process and subsequently the Docker apps do not boot into an OS – that saves a lot of time. The docker containers come up with the minimal runtime requirements of the application that allows them to deploy faster.
- Having Portable in Nature: The applications created with Docker containers are immensely portable. The Docker containers can run on any platform whether it be Amazon EC2, Google Cloud Platform, VirtualBox, Rackspace server, or any other – though the host OS should support Docker. The Docker containers can swiftly move from the cloud environment to localhost and vice-versa. It subsequently results in various benefits such as no wastage of time & resources in setting up environments, debugging issues in environments, etc.
- Cost-Effective: It can help Organization to reduce overall cost without compromising with the standard workflow or product quality. As Docker reduces the need for more infrastructure resources for development and the container created for individual processes can be shared with other apps with instances of these containerized apps using less memory compared to virtual machines.
- In-Built Version Control System: The Docker containers allow you to commit changes to the Docker images and version control them conveniently. For instance – if you are having some issues with the current or upgraded version of the image – you can quickly roll back to a previous stable version of the Docker image. Docker enables you to easily track successive versions of a container and inspect the differences before rolling back to the previous versions.
- Security: As name suggests, the most common issue of any Organization to secure its applications – is the Security. As Docker takes the responsibility of complete isolation and segregation of applications running within the Docker containers with each other – the developers have complete control over the traffic course. A particular container cannot access the data of another container without having authorized access.
Requirements
To install docker on your host machine, you will have following requirements:
For Ubuntu:
- OS: Linux Ubuntu
- Memory: 512MB RAM (2GB Recommended)
- Disk: Sufficient amount to run the Docker containers you wish to use
- CPU: Dependent on the applications you wish to run in the containers
For Windows:
- WSL 2 & Hyper-V and Containers Windows features must be enabled. Requirements to run these two:
- 64-bit processor with Second Level Address Translation (SLAT)
- 4GB system RAM
- BIOS-level hardware virtualization support must be enabled in the BIOS settings.
- Download and install the Linux kernel update package.
For MacOS:
- Version must be 10.15 or newer
- Minimum 4GB RAM
- VirtualBox prior to version 4.3.30 must not be installed as it is not compatible with Docker Desktop.
How It Works
Docker packages an application and all its dependencies in a virtual container that can run on any Linux server. This is why it is called as container. As it has all the necessary dependencies contained in a single piece of software. Docker has following elements:
- A Daemon, which is used to build, run, and manage the containers
- A high-level API which allows the user to communicate with the Daemon,
- and a CLI, the interface which is used to make this all available.
Docker Containers:
Containers are abstractions of the app layer. They package all the code, libraries, and dependencies together. Each container runs as an isolated process in the user space and take up less space than regular VMs due to their layered architecture. Also, all layers are hashed, which means Docker can cache those layers and optimize build times for layers that didn’t change across builds. In the end of the build process, Docker creates a new empty layer on top of all layers called thin writable layer. This layer is the one which is accessed when using docker exec -it <container> <command>.
This way you can perform interactive changes in the image and commit those using docker commit, just like a Git tracked file. You can check containers running on your machine using docker ps. Even, when container is not in use we can stop that container also using docker stop <container>
Docker Images:
Docker images are instructions written in a special file called a Dockerfile (no extension). It has its own syntax and defines what steps Docker will take to build your container. Since containers are only layers upon layers of changes, each new command you create in a Docker image will create a new layer in the container.
An example of a simple image for a Node.js application:
FROM node:stable
COPY . /usr/src/app
/RUN npm install && npm run build
EXPOSE 3000
ENTRYPOINT [“npm”, “start”]
These images are stored in the Docker Hub or the local Registry and can be removed if not in use using docker image rm <imagename>
How to build Images and run Containers:
- When run docker build .on the same directory as the Dockerfile, Docker daemon will start building the image and packaging it.
- Then, run docker run <image-name>to start a new container.
Also, you can execute commands inside containers with docker exec.
This way, an application can run on any platform irrespective of the software installed on the host machine.