Dockerfile

Dockerfile

Overview:

Dockerfile is a document with a set of commands that you need to build docker image. The creation of Docker images is done via files named Dockerfile.

Dockerfile instructions:

  • FROM - Dockerfile must start with FROM instruction. It defines the base image for your docker image. You can use FROM instruction multiple times in a single dockerfile. You can pass --platform flag to specify the platform of base image.
# Specify base image 
FROM node:lts-alpine3.13

  • LABEL - It will add a label to your image. It’s always a key-value pair. Docker Labels allow you to specify metadata for Docker objects like organizing images by project, record licensing information. MAINTAINER is deprecated instruction. You use a LABEL instead of the MAINTAINER field.
# Adds metadata to an image
LABEL version="1.0" maintainer="[email protected]"

  • ENV - Used to set environment variables. You can set multiple variables at a time or set the version numbers. ENV will persist when a container is run from the resulting image.
# Set env
ENV VERSION=1.0.0

  • USER - Used to set the username which is to run the container. ROOT is the default user for dockerfile.
# Use test user to run dockerimage
USER user

  • RUN - Used to execute docker commands in your dockerfile. You can say this is the most-used instruction in dockerfile. You can execute multiple commands using multiple lines with RUN.
# Execute command
RUN npm install

  • EXPOSE - Used to inform Docker that the container listens on a specific network port at runtime. You can specify whether the port listens on TCP or UDP, the default is TCP.
#Exposing 8080 port
EXPOSE 8080

  • ADD - Used to copy new files, directories, or remote file URLs from <source> and adds them to the filesystem of the image at the path <destination>. ADD can pull files from url sources and also extract compressed files.
# Add /bar
ADD . /bar

  • COPY - Used to copy new files or directories from <source> and adds them to the filesystem of the image at the path <destination>.
#Copy package.json
COPY ./package.json .

  • WORKDIR - Used to define the working directory of a Docker container. RUN, CMD, ENTRYPOINT, COPY and ADD instructions will be executed in the specified working directory. For clarity, troubleshooting, maintaining, and avoiding repetition of adding root path in dockerfile, you should use this instruction.
# Create Working directory
WORKDIR /home/test

  • VOLUME - It creates a mount point with the specified name. Used to enable access from the container to a directory on the host machine.
VOLUME ["/data"]

  • ENTRYPOINT - ENTRYPOINT instruction has two forms shell and executable form.
#exec form of entrypoint
ENTRYPOINT ["/bin/npm"]

  • CMD - Used to execute a command at runtime when the container is executed. You can override CMD when you are running your container by specifying your command. CMD instruction has two forms shell and executable form.
# exec form of CMD
CMD [ "npm", "start" ]


# shell form of CMD
CMD echo "This is a test." | wc -
  • ONBUILD - The ONBUILD instruction adds to the image a trigger instruction to be executed at a later time when the image is used as the base for another build. This allows reducing Dockerfile size by factoring repetitive configuration in a parent image.
  • STOPSIGNAL - Used to send system call signal to the container. Signal can be valid number or signal name.
# Graceful Shutdown
STOPSIGNAL SIGQUIT

  • HEALTHCHECK - Used to checks the status of a container. When a container created with healthcheck option, it has a health status in addition to its normal status.
HEALTHCHECK --interval=5s CMD curl --fail https://localhost:8080/ || exit 1


Reference: For more details, you can check official dockerfile documentation.

Stay tuned for the most interesting topic Dockerfile: multi-stage builds !!!

PS: Please don't copy-paste this article. If you find it useful please feel free to share it, and yes don't forget to give credits.

Prasanna Kelkar

Software Engineer at IBM, ISDL

4 年

Nice one Snehal. Interesting read.

Shashank Mani Tripathi

Solutions Architect || Ex- Persistent, GlobalLogic

4 年

Snehal Rajmane Yes indeed! Very familiar with the story, thanks for sharing.

Shubham Salani

AWS Cloud Migration Specialist | Expert in Migration Strategies

4 年

Well articulated Snehal

Abhijit Mone

Full-stack Developer | Web Development, HTML5

4 年

Nice article.

要查看或添加评论,请登录

Snehal Rajmane的更多文章

  • Docker Compose II

    Docker Compose II

    I. Define the Project Create a directory for your project, in this directory, we will create project-specific…

  • Docker Compose I

    Docker Compose I

    Docker Compose is a tool for running multi-container applications. All containers work together.

  • Advanced Dockerfiles: Multistage Build

    Advanced Dockerfiles: Multistage Build

    Docker images usually built with dockerfile with instructions, dependencies, and build steps. Dockerfile instructions…

    2 条评论
  • Part II: Docker Commands

    Part II: Docker Commands

    1. Copy files/folders: Yes, we can copy files/folders from local machine to container and vice versa.

    3 条评论
  • Introduction to Docker

    Introduction to Docker

    What is Docker? Docker is a container orchestration tool, which is used for developing, shipping, and running…

    6 条评论

社区洞察

其他会员也浏览了