Search⌘ K

Taking a Closer Look at the Docker Image

Explore how Docker builds images by processing Dockerfile instructions to create layers and metadata. Learn to inspect image history and understand the role of each instruction in containerizing applications effectively.

Now that the application is containerized, take a closer look at how some of the machinery works.

Examining the build process

The docker build command parses the Dockerfile one line at a time, starting from the top. We can insert comments by starting a line with the # character, and the builder will ignore them.

Shell-20
ARG NODE_VERSION=20.8.0
FROM node:${NODE_VERSION}-alpine
# Use production node environment by default
ENV NODE_ENV production
WORKDIR /usr/src/app
# Download dependencies as a separate step to take advantage of Docker's caching
# Leverage a cache mount to /root/.npm to speed up subsequent builds
# Leverage a bind mounts to package.json and package-lock.json to avoid having to copy them into
# into this layer
RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=package-lock.json,target=package-lock.json \
--mount=type=cache,target=/root/.npm \
npm ci --omit=dev
# Run the application as a non-root user.
USER node
# Copy the rest of the source files into the image.
COPY . .
# Expose the port that the application listens on.
EXPOSE 8080
# Run the application.
CMD node app.js

All non-comment lines are called instructions or steps and take the format <INSTRUCTION> <arguments>. Instruction names are not case-sensitive, but it’s common to write them in UPPERCASE to make them easier to ...