Introduction to Docker containers. We all are familiar with the name Docker, now a days. It is a containerisation technology to create multiple containers on a Host machine.
Read part II -> Managing Docker containers – basics, part 2
What is a Linux container?
We discussed this topic in our previous post. The basic idea behind traditional virtual machine and containerisation technique.
What is a Linux container?
Basic: Containerisation helps to isolate processes. You can run your App/Services as an isolated process, running from a distinct image that provides all files necessary to support the processes.
Basically Linux containers are OS level virtualisation technique for running multiple isolated Linux systems (containers) on a control host using a single Linux kernel.
Why we need containerisation for deploying web applications or sites? The answer of this questions pointing finger to micro-services.
Consider the scenario, you are running a large online shopping web site with thousands of daily or hourly visitors. The web server, where you hosted the site must handle many things at a time.
Like, need to serve pages to new visitors, different product queries, order management, product listing/sorting for visitors.
Imagine, all of these are handling a single web server and a single database. Wow!! The server should be on fire most of the time in a day. It will definitely go down or slow state during peak hours.
Here we go with micro services concept. A large application is brocken down into smaller services, each of those services can be term as micro services.
Another way, micro services can be considered as small processes that communicate each other over a network, to fulfil one particular goal.
Advantages of micro services
Micro-services implementation has a lot of advantages. Some of them are listed/explained below:
1. Freedom of developers. Micro services implementation provides freedom to developers. As the service is sliced into smaller units, each units can be deployed and then redeployed independently without compromising the integrity of the larger application.
Also they can write code for different services in different language.
2. Fault isolation. If one microservice fails, the others will continue to work.
3. We can easily integrate those services to any continues integration tools like jenkins.
4. Scalability and reusability, as well as efficiency.
5. As it’s small pieces, understand the code unit easily.
6. Work very well with containers, such as Docker.
7. More flexible for everyone, developer as well as administrator.
Introduction to Docker containers. Why Docker?
As discussed, Docker can create containers for deploying those micro services. It’s a tool/software to create containers quickly and effectively from Linux command line.
We will discuss all those details soon!!
Main three parts of Docker!
Main three parts of Docker are; Docker hub, Docker image and Docker containers. Docker hub is the place where you can share your images and download any images as per availability.
This is something like Git hub. You can pull and push images to and from Docker hub. Docket hub location is https://hub.docker.com/
Docker images are the important element to create a container. We can pull images from the Docker hub to your local machine / server or you can build your own Docker image using Dockerfile.
Then you can run a container using that image on your local repo. Okay let’s start the topic, Introduction to Docker containers.
What is Docker file?
Dockerfile: The default file for building docker images.
Docker can build images automatically by reading the instructions from this file. This is a normal text file contains all the commands to build an image. You have to maintain a specific format to write Dockerfile.
The “docker build” command will build an image from Dockerfile.
docker build PATH/URL
docker build .
PATH represents the path to Dockerfile and URL means any links to Dockerfile.
The (.) represents current working directory.
Actually, the image build is run by Docker daemon, not by the commandline interface. The entire context (content in the directory) will be sent to the Docker daemon when building the Docker image.
Tips to remember:
1. Always select an empty directory as context and keep your Dockerfile in that directory.
2. Add only the files needed for building the Docker image.
3. Do not use “/” (root directory) as context, cuz the entire contents under “/” will be sent to the Docker daemon.
4. To increase the build performance, exclude files and directories (unwanted) by adding a .dockerignore file to the context directory. Will explain about this later.
Building a Docker image
This is the main section on our topic, Introduction to Docker containers. Normally we write Dockerfile in the repository directory itself and build the image using (.) options as PATH.
We can use “-f” flag with “docker build” to point a docker fle anywhere in your file system.
docker build -f path/to/Dockerfile .
To create a tag on image, you can use “-t” option with “docker build.” This should be helpful to create notes on changes on image.
docker build -t myAppver:0.0.1 .
How to write a Dockerfile?
As I mentioned earlier, we have to maintain a certain format to write Dockerfiles. The default syntax for writing a Dockerfile is pasted below:
# comment INSTRUCTION arguments
Normally we write INSTRUCTIONS in uppercase. Actually it’s not case sensitive, however, it is good for a separation. Docker daemon runs instructions in Dockerfile in order. /
The Dockerfile must starts with the instruction FROM, for the base image.
Lot of INSTRUCTIONS are there to write Dockerfile. I will add more details soon. Some of them are: FROM, RUN, EXPOSE, CMD etc..
The lines starts with “#” is for commending something. The line starts with # is treated as comment, anywhere else is treated as arguments.
# This is my first Dockerfile RUN echo "This is my test build."
You can use “docker run” command to start container using the image. I hope this article gives you a quick intro to Docker containers.. We completed the first part of Introduction to Docker containers.
Will add more docs soon.