DevOps Interview Preparation Course
Docker
Docker Question 6
This lesson explores two Dockerfiles and explains which resulting Docker image will be smaller and why. The only difference between these Dockerfiles is the choice of the base image. Dockerfile 1 uses the lightweight "node:12-alpine" image, while Dockerfile 2 uses the default "node" image, which typically points to the latest version and is based on a more comprehensive Debian distribution.
Detailed Examination of the Dockerfiles
Below are the final, corrected versions of both Dockerfiles:
# Dockerfile 1
# syntax=docker/dockerfile:1
FROM node:12-alpine
RUN apk add --no-cache python2 g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
# Dockerfile 2
# syntax=docker/dockerfile:1
FROM node
RUN apk add --no-cache python2 g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
Important Note
For Dockerfile 2, although the command uses apk
for package installation, the default "node" image is typically based on a Debian-derived distribution that uses apt-get
. You may need to adjust the package installation command for compatibility.
Why Dockerfile 1 Results in a Smaller Image
By using the "node:12-alpine" base image, Dockerfile 1 benefits from Alpine Linux’s minimalistic design. Alpine images include only the essential packages required for many applications, leading to a significantly leaner and more efficient build. In contrast, Dockerfile 2's default "node" image often contains additional packages and dependencies, resulting in a larger file size.
Key Takeaways for Interview Discussions
When explaining this topic in an interview, you might say:
"In comparing the two Dockerfiles, Dockerfile 1's use of the 'node:12-alpine' base image—a lightweight Alpine Linux-based image—results in a leaner build. On the other hand, Dockerfile 2 employs the more comprehensive 'node' image, which tends to be heavier due to extra packages and dependencies. Therefore, Dockerfile 1 is preferable when optimizing for image size."
Visual Comparison
Conclusion
This example underscores a key strategy in Docker image optimization: starting with a minimal base image can greatly reduce the overall size, which is particularly advantageous in production environments where deployment speed and resource usage are critical.
For further reading on Docker best practices, consider reviewing the following resources:
Understanding these differences helps in designing efficient Docker images that are both secure and performance-oriented.
Watch Video
Watch video content