DMCA.com Protection Status Trending Topics About Devops: DOCKER GUIDE: HOW TO INSTALL DOCKER AND WRITE DOCKERFILES

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.