Skip to main content

Commands used in Dockefile - Part4

  Docker is an opensource tool used for virtualization and deliver software in the form of packages called Containers. It is one of the most important tools used in networking domain.  Dockerfile  is a template for building the image which contains commands needed to package the software. In this post, I am going to explain the commands ADD & COPY used in  Dockerfile .   ADD Command:   ADD command is used to copy files, directories or files from remote URLS to destination path in the image. The source paths can contain wildcards.   If the destination has relative path, it is relative to the Working directory of the image. Note that source path is always relative to the Docker build context.   ADD command will not support authentication. So, if there are any protected files to be added in  Dockerfile  building, use other tools like curl or  wget   Dockerfile  copying Single & Multiple files, directory using ADD inst...

Commands used in Dockerfile - Part3

 Docker is an opensource tool used for virtualization and deliver software in the form of packages called Containers. It is one of the most important tools used in networking domain. Dockerfile is a template for building the image which contains commands needed to package the software. In this post, I am going to explain the commands WORKDIR & HEALTHCHECK used in Dockerfile. 

WORKDIR Command: 

WORKDIR command is used to set the directory of the image on which the commands like RUN, COPY, ADD, ENTRYPOINT, CMD operates. 


The command usage is WORKDIR <Path_To_Dir>. If the directory not exists, it will be created. If the relative path is given to WORKDIR, that path will be relative to the previous WORKDIR command’s path.  


In Ubuntu, by default the WORKDIR will be /. Please find the below sample Dockerfile to get clear picture of WORKDIR command: 


FROM ubuntu:16.04 
WORKDIR /root 
RUN mkdir Manideep 
WORKDIR Manideep 
RUN mkdir Labs 
WORKDIR Tests 


Line1 specifies the base image as Ubuntu 16.04. Lines 2 sets working directory as /root. Line 3 creates a directory named Manideep in /root path. Line 4 sets working directory to /root/Manideep. Line 5 creates a directory named Labs in /root/Manideep. Line 6 sets creates a directory named Tests in /root/Manideep and set it as working directory 


HEALTHCHECK Command: 

HEALTHCHECK command is used to check whether container is working or not based on the test provided by the user. 


To disable the HEALTHCHECK in the image, use HEALTHCHECK NONE. To provide the test for checking the health, the command syntax is HEALTHCHECK OPTIONS CMD <User_Defined_Command>. Note that Dockerfile supports only one HEALTHCHECK instruction. If there are multiple such instructions, the last will be used. 


Before check of health of container, its health status will be starting. If the test is passed, the health status will be healthy. If the test fails, the health status will be unhealthy. 


The below options are available for HEALTHCHECK command: 

  • --retries=<Number> ==> Default value is 3. It specifies the number of retries for the test to get succeed 
  • --interval=Ns ==> Default value is 30s. It specifies the wait period between two checks of health 
  • --timeout=Ns ==> Default value is 30s. It specifies the maximum time to check the specified health condition 
  • --start-period=Ns ==> Default value if 0s. It specifies the wait time before performing the first health check as initially container needs some time to start 

Please find the below Dockerfile to get clear picture of HEALTHCHECK command 


FROM ubuntu:16.04 
RUN apt-get update && apt-get install curl -y 
HEALTHCHECK --start-period=30s --retries=3 CMD curl -f google.com 


Line1 specifies the base image as Ubuntu 16.04. Line 2 specifies to install curl. Line 3 specifies the health check condition as to curl google.com and number of retries as 3 and first check happens only after 30seconds.  

Health status and its corresponding log of up to 4096 bytes can be seen by inspecting the container. Please use the below commands to check health status, health status condition and log of health status 

  • docker inspect --format='{{json .Config.Healthcheck}}' <Container_ID> ==> Give the health check condition of the container 
  • docker inspect --format='{{json .State.Health.Status}}' <Container_ID> ==> Gives the health status of the container 
  • docker inspect --format='{{json .State.Health.Log}}' <Container_ID> ==> Gives the log of the health check of the container 

Note that the HEALTHCHECK is added from Docker version 1.12. Below versions will not support this 

Comments

Popular posts from this blog

Vyos - An Open Source Network Operating System

What is  a  Network Operating System?   Networking Operating System (NOS) is an O perating system  that  has the capability to  support workstations, database sharing, application sharing, file ,  and printer access sharing among multiple computers in a network. In general ,  NOS is a specialized operating system used for devices like router, switch or firewall. Features of Networking Operating System:   NOS has the following features: Basic features like protocol support, processor support, hardware detection and multiprocessing support for applications Authentication, access control, authorization ,  and restriction facilities are supported to provide security Provides file service, web service, printing and replication Has Naming and Directory Management services Has  the provision  for user management and remote access & system management Also has internetworking features like routing and ...

Notes on IPv4, IPv6 and MAC representations

  In companies related to networking domain, “ Explain IPv4, IPv6 and MAC ” is one of the common questions asked in technical interview. This post provides overview of these concepts. IP: IP is the protocol used in Networking layer. It is responsible for the delivering packets from source to destination based on the identifier known as IP address of the device. There are two popular ways of representing the devices in the network. They are: IPv4  (Internet  Protocol version 4) IPv6 (Internet Protocol version6) IPv4:   This version of IP uses 32-bit addresses to identify the device on the network.  Therefore,  the total number of addresses possible with IPv4 is 2 32 . These  32-bit  addresses are divided into 4 octets (batch of 8 bits) with each octet represented in decimal numbers and octets are separated by dot. In IPv4 the loopback address of a device is  127.0.0.1 . IPv4 supports unicasting, broadcasting and multicasting. To map IPv4 addre...

Introduction to Docker and Containers - Overview, Installation, Image and Container creation

Docker is an open source software used to develop and deliver the software in  the  form of packages known as Containers. Software used for hosting containers is known as Docker Engine. Docker is written in the Go programming language. Containers are isolated from each other. Each container has its own libraries, configuration files and there are mechanisms through containers  to  communicate with each other. These containers are created from images which can be modified or customized based on the need. All the containers share the host operating system and hence these are light-weight. An Image from which containers are created is nothing but a package of code, libraries, environment variables and configuration files. For knowing differences between docker containers and virtual machines, click  here . Here we learn how to install  D ocker, build an image, create containers using  the  image, clearing and viewing containers i...