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 DockerfileRUN
: Executes commandsADD
: Copies files into the filesystem of the imageENV
: Sets environment variablesENTRYPOINT
: Specifies the entry-point to use when running the containerCMD
: Defines the default command to run when the container startsEXPOSE
: Maps a port into the containerVOLUME
: Creates a named volumeWORKDIR
: Sets the working directory for subsequent commandsUSER
: 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 .