Docker tutorials – introduction part 1

In this series / category, I share the details and tips of Linux containerization technology using Docker daemon. Welcome to Docker world. We all are familiar with the name Docker, now a days. It is a technology to create multiple containers on a Host machine. In this blog post, we will cover the basics of Linux containers, micro services, advantages of micro services, important components of Docker, how to write a Docker file, how to build a Docker image etc.

Alright, here we go!!

What is a Linux container?

We discussed this topic in our previous post. The basic idea behind traditional virtual machine and containerisation technique.

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.

More, What is a Linux container?

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.

Introduction to Docker containers
Introduction to Docker containers

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. It will definitely go down or become slow during peak hours.

Here we go with micro services concept. A large application is broken 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 micro service fails, the others will continue to work.

3. We can easily integrate those services to any continues integration tools like Jenkins.

4. Scalability and re-usability, 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 components of Docker!

Main three parts of Docker are; Docker hub [registry], Docker image and Docker containers. Docker hub is the place where you can share your images and download any images as per availability. You can create your own private registry or can use the public one.

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.

Syntax

docker build PATH/URL

Example:

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 command-line 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, because 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 in our Introduction part. Normally we write Dockerfile in the repository directory [I mean, the location where the code and files present] 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.

Sample Dockerfile

# 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.

Managing Docker containers – basics, part 2

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

2 thoughts on “Docker tutorials – introduction part 1

Leave a Reply

Your email address will not be published. Required fields are marked *