DMCA.com Protection Status Trending Topics About Devops: 09/11/21

Saturday, 11 September 2021

DOCKER GUIDE: HOW TO INSTALL DOCKER AND WRITE DOCKERFILES

 In this blog we will discuss how to install it in our system and write docker files .

Here I will use ubuntu 20.04 to install docker.

Before installing docker make sure following system requirements are met

  1. Linux kernel version >= 3.8
  2. 64-bit Processor
  3. Memory :- 2GB recommended (512 MB minimum)

To check the version of Linux kernel, use command :-

uname -r

To install docker in your systems, Follow below steps:-

  1. update your OS using command :- sudo apt-get update to get latest packages(here “sudo” ensures that command runs with root access)

2. To download latest docker packages from docker site, some necessary certificates are required that has to be downloaded using command:-

sudo apt-get install apt-transport-https ca-certificates

3. Next step is to add the Linux official GPG key. GPG key is used by Linux to keep your files and packages safe and encrypted. To add GPG key use below command:-

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

4. After adding GPG key verify it using command:-

sudo apt-key fingerprint 0EBFCD88

5. Next, set up a stable docker repository . to do this use following command:-

sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable”

6. Update ubuntu packages using sudo apt-get update to ensure all packages are up to date

7. Finally install docker engine from docker website using command:-

sudo apt-get install docker-ce

9. Congratulations now you have docker installed on your ubuntu machine. To check the docker version use command:-

sudo docker version

DockerFiles:-

In my previous post DOCKER GUIDE: WHAT IS DOCKER AND WHY USE IT:- i wrote about images and their use in docker . Dockerfile is simply a text file that contains instructions to build an image. All docker images have their own dockerfiles.

Some important commands to write dockerfiles:-

  1. FROM :- FROM command is used to set the base image where next instructions of dockerfile will run.
  2. COPY :- copy command is used to copy contents of one directory on host machine to another directory inside the container.
  3. RUN :- run command is used to run any command on the current image.
  4. WORKDIR :- workdir command is used to set the working directory where subsequent instructions of dockerfile will run.
  5. CMD :- CMD command is used to execute a command after creation of the container. Each dockerfile runs only one CMD instruction. If there are more than one CMD instruction in a dockerfile then the last CMD instruction will run and all others will be skipped.

Now suppose you have a small nodeJS application . Let us build an image of this application by writing a dockerfile.

  • FROM node:10-alpine:- Set node:10-alpine image as the base image for rest of the instructions of dockerfile.
  • RUN mkdir -p /home/app:- create a directory /home/app
  • COPY ./project /home/app:- copy all content of the project folder to /home/app directory of the container.
  • WORKDIR /home/app:- set /home/app as working directory, all the subsequent instructions on dockerfile will run here.
  • RUN npm install:- install all the dependencies of your project mentioned in package.json and package-lock.json files.
  • CMD [“node”, “index.js”]:- execute the application.

BUILD THE IMAGE :- After writing the dockerfile next step is to build the image. Use below command to build image from our dockerfile :-

Sudo docker build -t <Image-name>:<tag-name> .

Note:- “sudo” is used here to run the command with root access.

IMAGE TAGGING :- In the above command -t flag is used for tagging your image. Tagging is a process of adding metadata to your docker images like version no. If you do not specify any tag name for the image, docker assigns it by itself as “latest”. Tagging is a optional process

The above command runs each instruction specified in your dockerfile and build your image in layers.

RUN THE IMAGE:- To run your image use command:-

Sudo Docker run <image>

GET YOUR IMAGE INFO:- To verify your image, run command:-

Sudo Docker images

Above command will give you a list of all the images in your system with their details .

Congratulations, you have successfully installed docker in your system and created your application’s image via dockerfile.

DOCKER GUIDE: WHAT IS DOCKER AND WHY USE IT:-

 

WHAT IS DOCKER?

According to official docker documentation, docker is an open platform for developing, shipping and running applications.

The basic idea of docker is to significantly reduce the time between writing the code and deploying it in production.

When you deploy an application using docker, it runs inside containers. Each container stores up code and all the dependencies required to run that code. Due to this, Docker is also referred to as a container management service many times.

WHY USE DOCKER?

  • Less Downtime:- Docker allows us to clean and repair applications without any significant downtime.
  • Cost Effective:- With docker, multiple containers can run on single server which significantly reduces the use of resources and makes docker more cost effective
  • Inbuilt version control system:- Docker has its inbuilt version control system. Using this feature you can roll back to your previous code anytime you want.
  • Portability:- Docker provides portability. You can containerize your applications and run those containers wherever you want whenever you want.
  • Security:- Docker provides security. Since applications run inside containers in docker that provide an isolated environment, without proper authorization one container would not be able to access another.

SOME IMPORTANT DOCKER TERMS

Docker Images:-Each container is a running instance of a docker image. A Docker image stores all the application code, config files, libraries etc. required to build and run an application.

Docker Hub:-Docker Hub is a repository that calls itself the world’s largest repository (of container images) and is used to store and manage container images publicly and privately.

DockerFile:-A file that is used to build docker images. This file contains instructions to create docker images.

Docker Daemon:-Docker daemon can be considered as the heart of docker. When a client sends a command it goes to docker daemon. Docker daemon is a service that is responsible for listening to all the API requests from Docker client in the form of docker commands like creating images ,running them in containers etc.

Docker Registry:-Docker registry is an open source storage or service that is to store docker images. Docker Hub is also a docker registry. Docker registry can also be used to track versions of docker images in repositories. It also allows you to tag each image for identification.

DOCKER ARCHITECTURE:-

Docker Architecture consists of 3 main parts:-

  • Docker client
  • Docker host
  • Docker Registry

Docker Client:- Docker uses client-server architecture. Docker client is responsible for sending docker commands given by docker users to docker daemon.

Docker Daemon:- As Discussed earlier , docker daemon is responsible for listening commands or API requests from docker client. It is a service that runs on the host operating system.

Currently docker daemon can only run on Linux since it utilizes many Linux kernel features.

Docker registry:- Docker registry is used to store docker images. One popular example is docker hub for which docker is configured to work by default. You can pull any image from docker hub using docker pull command to use it. You can push any image you created, to docker hub using docker push command. You can also create your private registry.

CONCLUSION

According to me, Docker is worth a try for every developer out there as it saves time and makes deployment really simple. Containers are lightweight and use of containerization in docker helps to provide your applications an isolated and secure environment to run.

At last I would like to thank you for reading it and I hope it helped you. my next article will on process of installing docker on windows and Linux and learning to write dockerfile. Hope you will enjoy it too !!!!!!!!!!!