Probably all the developers must have heard about Docker. Docker has now become a standard for developing and deploying applications. It also has impacted DevOps a lot as mentioned in this Forbes article. In this post, we are going to look at what Docker is, what Docker containers and images are, what are Docker registries, and where they are used.
Containerization is not a new concept. This concept has been around since the 1970s since the Linux Containers. Containers add an abstraction layer between the host operating system and the applications running on the container. This gives an impression that only the guest applications are running, thus adding abstraction and security to the applications.
The difference that Docker made was that it made this whole process very easy and independent of the operating system. Hence Linux containers could run perfectly seamlessly on Windows OS as well. Once development teams were free of the restriction of any particular operating system, a whole new door opened in software development.
But this is not all. Just like code has code repositories like Github, where developers can host their repositories; Docker also introduced something called Docker registry such as the Docker Registry. Now companies could host their entire software images directly on these registries and developers can fetch these. Docker also provided its registry called Docker Hub. All the prominent platforms host their platform images there. Developers can use these reusable packages to build applications on top of them.
When you want to build an application on Docker, there are a few simple steps that you need to perform.
- First, install Docker on your machine. Docker is available for every popular operating system including major distributions of Linux.
- Then you need to identify where you want to host the application. For instance, suppose I have an application that runs on IIS. So I will need to fetch the image of IIS for Docker. If my application runs on Node JS, I will need to fetch an image of Node JS from Docker Hub.
- Once the image has been fetched, we move our code inside the above-downloaded container.
- If your application needs to persist the data or need to talk to other applications within Docker you will need to do a few more steps. You will need to configure the Docker network and Docker persistence to achieve communication and data storage respectively.
- Once you have followed all the above steps, you have your Dockerfile. This file can be used to distribute your application as you wish, run the code, deploy and test the application, and so on. You are now no more platform-dependent.
Working of Docker
In Docker, three components work together.
- The first is the Docker Daemon. This service takes the commands and then and then executes them. Docker Daemon is used to build, run, and manage the containers.
- Docker interface that is used to communicate with the Daemon.
- A CLI to work these together.
Creating a Dockerized Container
A minimal Docker file looks like:
COPY . /usr/src/app/
ENTRYPOINT [“npm”, “start”]
In this file, we are setting up a node application. Let me summarize it:
- First, we fetch the node image from their repository. We specify which image to fetch by mentioning the tag.
- Next, we copy our source files. The “.” specifies that the files are present in the current directory. We install them to /usr/src/app/. We need to copy these files since we want to have these files for running the application.
- We also have to specify the working directory. The working directory tells the Docker daemon which directory to execute the commands on inside the Docker image.
- We expose port 8089. This exposure enables applications outside the Docker container to access the Docker application. If you want some other port to access the port on Docker, you can also do port forwarding.
- As soon as the container runs, “npm start” to execute.
Finally, we run the docker image by running the command below:
docker build . -t my-image
To run this image we run the below command:
docker run -p 80:8089 my-image
Uses of Docker
I am listing down the uses of Docker below:
- Docker is best for applications that require database installation along with code execution. Instead of installing the database first and then running the application, we can package both the application and the database together for the complete installation and execution of the application.
- Another usage of Docker is the setting up of front and backend applications. Today applications are built on a full-stack combined with a database. If you want to run such a stack, instead of running all the applications separately we can run them together in a single go.
In this post, we saw the ins and outs of Docker. Docker is great to act as your development, testing, and final execution platform. Having Docker in place, we can also distribute the application as well as. If you are not using Docker until now, make sure you jump to it straight away.