Swipe left or right to navigate to next or previous post

Ultimate explanation on various keywords used in Docker file

27 Dec 2022 . category: Docker . Comments
#Server #Docker

Ultimate explanation on various keywords used in Docker  - Tapan BK

A Docker file is a script that is used by Docker to generate containers automatically. It is a text document that contains all the instructions that is used to create an image from command line.

It allows programmers to build and run containers(self-contained applications) and systems. The Docker generates various light weight containers that help applications to work efficiently in different environments.

Frequently used Dockerfile keyword

  • Comments
  • FROM
  • MAINTAINER
  • CMD
  • ENTRYPOINT
  • WORKDIR
  • ENV
  • COPY
  • LABEL
  • RUN
  • ADD
  • .dockerignore
  • ARG
  • EXPOSE
  • USER
  • VOLUME

Comments Keyword

Comments gives more details of the scope. Comments in the dockerfile start with #. It can be placed any where in Docker file.

     # from base image node

FROM Keyword

It must be the first command declared in the Dockerfile. Image cannot be build without this command.

It defines the base image to use to start the build process that can be pulled from the docker hub. If a image mentioned onf FROM image is not found on the host, Docker will try to find on Docker hub or other container repository.

    # from base image node
    FROM node:8.11-slim

This command pulls the node 8.11

MAINTAINER Keyword

It is not executing command that declares the author of the image

    MAINTAINER "Tapan B.K."

CMD Keyword

Command to be executed when running a container. It is used to give the default commands when the image is instantiated. The docker file must contain one CMD command. If a Dockerfile has multiple CMDs, it will only run the last one.

    # command executable and version
    CMD ["node","-v"]
    CMD ["node"]

ENTRYPOINT Keyword

It is used as an executable for the container. In following example, we are using the ENTRYPOINT for executable command and use CMD command to pass some default commands to executable.

    CMD ["-v"]
    ENTRYPOINT ["node"]

Points to note:

  • if we specify executable in ENTRYPOINT, we can use CMD to pass default parameters to it.
  • if not, we can specify executable and default params in the CMD.
  • We can override the default command given in CMD while running the container

WORKDIR Keyword

WORKDIR sets the working directory for all the consecutive commands. We can have the multiple WORKDIR commands, and it will be appended with a relative path. The WORKDIR directive is used to set where the command defined with CMD is to be executed.

Following example will set the WORKDIR to /home/tapan/api

    WORKDIR /home/tapan
    WORKDIR api

ENV Keyword

This command sets environment variables for the subsequent instructions in the build stage.

    ENV workdirectory /home/tapan/api
    WORKDIR $workdirectory

COPY Keyword

It is used to copy files or directories from source host filesystem to a destination in the container file system.

Following COPY command copies the package.json file our system to a container file system

    COPY package.json .

LABEL Keyword

LABEL command is used to add some metadata to the image.

    LABEL "about"="This file is just am example to demonstarte the LABEL"

RUN Keyword

Run executes the instruction in a new layer on top of the existing image. The new layer is committed and user for the next instruction in the Dockerfile. It takes a command as its argument and runs it to form the image.

    RUN npm install

ADD Keyword

It is similar to COPY. It is more feature-rich than COPY. COPY is more preferred over ADD

ADD is used to add files or directories and remote files from URL from source host filesystem to a destination in the container file system.

    ADD index.js .

ARG Keyword

ARG is used to pass some arguments to instruction. This is only command tha can be added before FROM other than a comment

    # from base image node
    ARG NODE_VERSION=8.11-slim
    FROM node:$NODE_VERSION
    FROM amazon/aws-lambda-nodejs:12
    ARG MSG
    ENV HELLO Hello ${MSG}

HELLO is an environment variable accessible in all the systems running inside the container defined by this image. It is set to the text message Hello ${MSG} where MSG is an argument that can be set via the docker build command:

    docker build --build-arg MSG="Hello World" -t my-awesome-app

If you want to pass multiple argument

    docker build --build-arg MSG="Hello World"  --build-arg name="Tapan BK" -t my-awesome-app

ARG can also be nested:

    FROM amazon/aws-lambda-nodejs:12
    ARG MSG="Hello world"
    ARG COOLMSG="Welcome, ${MSG}"
    RUN echo ${COOLMSG}

EXPOSE Keyword

The EXPOSE keyword is used to expose a port inside the Docker image to the outside world.

This command associate a specified port to enable networking between the running process inside the container and the host

    EXPOSE 3070

We expose the Docker image on port 3070, which we can then use when running the container.

USER Keyword

USER instruction sets user to use when running the image and for running any instructions that runs on the Dockerfile

    RUN useradd tapanbk
    USER tapanbk

VOLUME Keyword

VOLUME is used to create a mount point with the specified name. This command enable access from container to a directory on the host machine.

    RUN mkdir /dockerexample
    VOLUME /dockerexample

Finally, Basic structure of Docker file containing all the above keywords

    # from base image node
    ARG NODE_VERSION=8.11-slim
    FROM node:$NODE_VERSION
    LABEL "about"="This file is just am example to demonstrate the LABEL"
    ENV workdirectory /home/tapanbk
    RUN mkdir /dockerexample
    VOLUME /dockerexample
    COPY package.json .
    RUN ls -ll &&\
        npm install
    
    RUN useradd tapanbk &&\
        mkdir -p $workdirectory &&\
        chown tapanbk $workdirectory
        
    USER tapanbk
    WORKDIR $workdirectory
    ADD index.js .
    RUN ls -l
    EXPOSE 3070
    # command executable and version
    ENTRYPOINT ["node"]

Tapan B.K. | Full Stack Software Engineer

Tapan B.K. is Full Stack Software Engineer. In his spare time, Tapan likes to watch movies, visit new places.