FROM instruction for base image in Docker - Time & Space Complexity
We want to understand how the time it takes to build a Docker image grows when using the FROM instruction.
Specifically, how does choosing a base image affect the build time as the image size changes?
Analyze the time complexity of this Dockerfile snippet.
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y curl
COPY app /app
CMD ["/app/start.sh"]
This snippet starts from an Ubuntu base image, installs packages, copies app files, and sets the command to run.
Look for repeated steps that affect build time.
- Primary operation: Downloading and extracting the base image layers.
- How many times: Once per build if not cached; depends on number of layers in the base image.
The build time grows as the base image size and number of layers increase.
| Input Size (MB) | Approx. Operations (Download & Extract) |
|---|---|
| 50 | Low |
| 200 | Medium |
| 1000 | High |
Pattern observation: Larger base images take longer to download and extract, so build time increases roughly with image size.
Time Complexity: O(n)
This means build time grows linearly with the size of the base image.
[X] Wrong: "The FROM instruction always takes the same time regardless of the base image size."
[OK] Correct: Larger base images have more data to download and extract, so they take more time.
Understanding how base image size affects build time helps you make smart choices in real projects and shows you think about efficiency.
What if we used a multi-stage build with smaller base images? How would the time complexity change?
Practice
FROM instruction do in a Dockerfile?Solution
Step 1: Understand the role of FROM
The FROM instruction defines the starting point or base image for building a Docker image.Step 2: Compare with other instructions
Other instructions like COPY, RUN, and EXPOSE have different purposes, not setting the base image.Final Answer:
It sets the base image for the Docker build. -> Option AQuick Check:
FROM = base image [OK]
- Confusing FROM with COPY or RUN instructions
- Thinking FROM exposes ports
- Assuming FROM copies files
FROM instruction to use the Ubuntu 22.04 image?Solution
Step 1: Recall Docker image tag syntax
The correct format isimage_name:tag, soubuntu:22.04is valid.Step 2: Check other options
Options with dashes, slashes, or @ are invalid for tags in FROM instruction.Final Answer:
FROM ubuntu:22.04 -> Option CQuick Check:
Use colon for tag in FROM [OK]
- Using dash '-' instead of colon ':' for tags
- Using slash '/' incorrectly in image name
- Using '@' instead of ':' for tags
FROM python:3.12-slim RUN python --version
What will be the output when building this Docker image?
Solution
Step 1: Identify base image version
The base image ispython:3.12-slim, which includes Python version 3.12.Step 2: Understand RUN command output
The RUN command executespython --version, which prints the Python version installed in the image.Final Answer:
Python 3.12.x -> Option BQuick Check:
FROM python:3.12-slim runs Python 3.12 [OK]
- Assuming default Python version is 3.11 or 2.7
- Thinking slim images lack Python
- Confusing build output with runtime
FROM alpine FROM ubuntu:20.04 RUN echo "Hello"
Solution
Step 1: Check multiple FROM usage
Multiple FROM instructions are allowed for multi-stage builds even without stage names (AS). Earlier stages are discarded unless referenced (by name or index).Step 2: Verify other parts
FROM alpine defaults to :latest (valid image). The RUN applies to the final stage (ubuntu:20.04) and is syntactically correct.Final Answer:
There is no error in this Dockerfile snippet. -> Option DQuick Check:
Multiple FROM without AS is valid [OK]
- Assuming multiple FROM lines always work without names
- Thinking RUN syntax is wrong here
- Believing FROM must be last line
node:18 as the build stage (named 'build') and nginx:alpine as the final stage. Which FROM instructions correctly set this up?Solution
Step 1: Understand multi-stage build syntax
UseASto name the build stage, e.g.,FROM node:18 AS build.Step 2: Set final stage without conflicting names
The final stage usesFROM nginx:alpinewithout a stage name or with a different name.Step 3: Check options
FROM node:18 AS build FROM nginx:alpine correctly names the build stage and then starts the final stage. Others misuse stage names or omit them incorrectly.Final Answer:
FROM node:18 AS build FROM nginx:alpine -> Option AQuick Check:
Use AS to name build stage first [OK]
- Naming both stages the same
- Placing AS on final stage incorrectly
- Omitting AS for build stage in multi-stage
Ready to prove it?
Pass the test and this pattern moves unknown
