Dockerfile 101.
- Companies create custom images for their applications.
Dockerfile
is a text document having commands to create an image.- Dockerfiles start from a parent image or base image. Such base images are mostly lightweight Linux OS image that has node, npm or whatever tool you need for your application on top of it.
- Every image consists of multiple image layers. This makes Docker very efficient as image layers can be cached.
- REMEMBER: Each instruction in the
Dockerfile
creates one layer. These layers are stacked & each one is a delta of the changes from the previous layer.
# Base image
FROM node:19-alpine
# Copies package.json from host to docker env.
# The "/" at the end ensures directory creation at the Linux root (front "/")
COPY package.json /app/
COPY src /app/
# WORKDIR sets the working directory for all following commands.
WORKDIR /app
# Runs this command in a shell inside the container environment
RUN npm install
# The instruction that is to be executed when a Docker container starts
# There can be only on "CMD" instruction in a Dockerfile.
CMD ["node", "server.js"]
Build an image
# -t or --tag
docker build -t node-app:1.0 .