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
Post a Comment