Docker Architecture and Development WorkFlow
In this blog post, I will dive a little deeper into the architecture of Docker and will discuss some of the ways it can make a developer's job easier.
Architecture of Docker
- Docker works on a client-server architecture.
- The Docker engine is the server and is responsible to pass on the tasks to the host operating system kernel.
- The Containers / Processes represent the client.
Docker Installation
For Windows & MacOS
One can head to the Official Docker Website and install the latest version of Docker Desktop for their respective operating system. This will install the Docker Engine and other necessary tools to work with Docker.
For Linux
One can head here and select their distribution. The webpage will provide one with the necessary commands to install Docker Engine.
DockerFile, Image and Containers
So let's discuss Dockerfile, Docker Image & Docker Containers. Also, lets us try to understand how all three are related to one another and what various roles do they play.
Dockerfile
A Dockerfile contains a bunch of instructions that are used by Docker to build an image. Let's look at a simple Dockerfile and try to understand what it does.
FROM node:alpine
COPY . /app
WORKDIR /app
CMD node app.js
In the above Dockerfile, the base layer is the 'node:alpine' which is the environment on top of which we will run our JavaScript code. Next, we instruct Docker to copy all the files and directories present in the current directory into the 'app' directory. Then we set the 'app' directory as our working directory. Finally, we run the 'app.js' file.
Docker Images and Containers
A Docker image is an immutable file. It's a snapshot of a container. Images can even be stored in a Docker registry like Docker Hub. As these images can get quite large, they are made up of different layers of different images to reduce the size when sharing images over a network.
On the other hand, a container is an isolated, lightweight and virtualised run-time environment. They can be used to run processes and applications without relying upon the software installed on the host machine. They are created using the various Docker images available on the local machine or any public or private Docker registry.
There is sometimes a confusion between Docker Images and Containers. From an object oriented programming perspective, a Docker image is like a class and a container is like the instance of that class.
The following command can be used to create an image from a Dockerfile:
docker build -t name-of-image .
The docker build command tells Docker to build an image. The -t flag adds a tag to our image and . tells Docker the path for the Dockerfile.
The following command creates an application container:
docker run -dp 3000:3000 name-of-image
The docker run commands runs a container using the specified image. The -dp flags signify that we are running the container in detached mode (in background) and creating a mapping between the port 3000 of host and port 3000 of the container.
Development Workflow
Containerisation can be really useful when it comes to maintaining and quickly deploying various applications. Docker can make a developer's job easier in a lot of ways some of which are listed below.
It works everywhere
There are times when some products work in the development environment but cause problems in the test/production environment. Using docker can eliminated all these environment specific issues.
It makes it easier to implement CI/CD pipelines
Docker eases the entire process of setting up a CI/CD pipeline. It makes it really easy to automate development tasks and maintains consistency.
Increases productivity
Since Docker can be used to automate most of the repetitive tasks, the developer can focus on the more important stuff therefore increasing the level of productivity.
Easy cloud migration
All major cloud platforms have a Docker offering therefore it is really easy for development teams to deploy and scale up their products.
Easy to fix mistakes
Since Docker container images are versioned, development teams can roll back to a previous stable version in case of issues with the latest iteration of the product.