DEV Community

bhargavirengarajan21
bhargavirengarajan21

Posted on • Updated on

What is Dockerfile ? (Docker Series - III)

If anything missed kindly add in comment

  1. A sequential set of instruction for Docker engine , or a set of commands to assemble the an image
  2. Primary way of interacting with Docker and migrating containers.
  3. Each sequential instructions are processed individually and results are stored as stack of layers. This sequential layer managed by file system becomes a docker image.
  4. Sequential layers helps to cache and helps to troubleshoot.This precreated layer are reused by docker daemon when we run two docker file that has same layer at some stage.

Format:

ARG
FROM
RUN
ADD|COPY
ENV
CMD
ENTRYPOINT
EXPOSE
Enter fullscreen mode Exit fullscreen mode

No extention required, can be created from any text editor, its good to start a docker file name with 'D' capital.

EXAMPLE:

ARG VERSION=16.04
FROM Ubuntu:${VERSION}
RUN apt-get update -y
CMD ["bash"]
Enter fullscreen mode Exit fullscreen mode

Comments in Dockerfile is represented using '#'. But all hash preceeded sentence is not comments. That also means parser directive.

Parser directive
parser directive, one of the docker command indicates, how the docker file should be handled or read. It should be at the top of the file.

Types of parser:

  1. syntax: Its only available in buildKit, but its used
    to know the location of the dockerfile.

  2. escape: it available anywher, it used to specify the
    character to escape. default is '\'.

COMMANDS:

  1. ARG is the only instruction that may precede FROM
  2. FROM can appear multiple times within a single Dockerfile to create multiple images or use one build stage as a dependency for another.
  3. WORKDIR This sets the working directory for Run, cmd, copy. By default the WORKDIR is created if not specified.
  4. RUN to execute any command in new layer on top of current layer RUN ["executable", "param1", "param2"]. (exec form) another way of executing command irrespective of the shell based. eg RUN ["/bin/bash", "-c", "echo hello"].
  5. ADD Add new files from src and copies to dest. used to copy to docker image. should be used with caution.
  6. COPY Copy new files from src to dest, used to copy to docker container.
  7. Env Provide value for environment variable
  8. CMD Used to execute command in shell docker file can contain only on CMD , if more than one specified last final will be executed.
  9. Entry point Used to execute command when container is initiated
  10. Expose expose a particular port with specific protocols

Top comments (0)