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.
Software Engineer at IBM, ISDL
4 年Nice one Snehal. Interesting read.
Solutions Architect || Ex- Persistent, GlobalLogic
4 年Snehal Rajmane Yes indeed! Very familiar with the story, thanks for sharing.
AWS Cloud Migration Specialist | Expert in Migration Strategies
4 年Well articulated Snehal
Full-stack Developer | Web Development, HTML5
4 年Nice article.