A Dockerfile is a text document that contains a list of commands and instructions used to build a Docker image.


Supported commands

  • FROM: Specifies the base image to start from (mandatory)
  • MAINTAINER: Sets the author of the Dockerfile
  • RUN: Executes commands
  • ADD: Copies files into the filesystem of the image
  • ENV: Sets environment variables
  • ENTRYPOINT: Specifies the entry-point to use when running the container
  • CMD: Defines the default command to run when the container starts
  • EXPOSE: Maps a port into the container
  • VOLUME: Creates a named volume
  • WORKDIR: Sets the working directory for subsequent commands
  • USER: Sets the user and group ID.

Example

# Ex: Dockerfile that creates a docker image for a node app 
 
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
docker build -t myimage .

Passing dynamic arguments

ARG PORT=3000 
FROM node:18-alpine 
WORKDIR /app 
COPY . . 
RUN yarn install --production CMD ["node", "src/index.js"] 
EXPOSE $PORT
docker build --build-arg PORT=4000 -t myimage .


Refs

  1. https://docs.docker.com/develop/develop-images/dockerfile_best-practices/