Bird
Raised Fist0
Dockerdevops~5 mins

FROM instruction for base image in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you build a Docker image, you start from a base image that provides the basic environment. The FROM instruction tells Docker which base image to use, so you don't have to build everything from scratch.
When you want to create a custom application environment starting from a standard Linux system like Ubuntu or Alpine.
When you need to build a container image that includes a specific programming language runtime like Python or Node.js.
When you want to base your image on an official database image like MySQL or Redis to add custom configurations.
When you want to create a multi-stage build to optimize the final image size by using multiple FROM instructions.
When you want to specify a particular version or tag of a base image to ensure consistency across deployments.
Config File - Dockerfile
Dockerfile
FROM python:3.11-slim

WORKDIR /app

COPY . /app

RUN pip install --no-cache-dir -r requirements.txt

CMD ["python", "app.py"]

The FROM line sets the base image to Python 3.11 slim version, which is a lightweight Python environment.

WORKDIR sets the working directory inside the container.

COPY copies your application files into the container.

RUN installs the Python dependencies.

CMD defines the command to run your app when the container starts.

Commands
This command builds a Docker image named 'my-python-app' using the Dockerfile in the current directory. It reads the FROM instruction to start from the Python 3.11 slim base image.
Terminal
docker build -t my-python-app .
Expected OutputExpected
Sending build context to Docker daemon 4.096kB Step 1/6 : FROM python:3.11-slim 3.11-slim: Pulling from library/python Digest: sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Status: Downloaded newer image for python:3.11-slim ---> abcdef123456 Step 2/6 : WORKDIR /app ---> Running in 123456abcdef Removing intermediate container 123456abcdef ---> 7890abcd1234 Step 3/6 : COPY . /app ---> abc123def456 Step 4/6 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in def456abc123 Collecting flask Downloading Flask-2.3.2-py3-none-any.whl (100 kB) Installing collected packages: flask Successfully installed flask-2.3.2 Removing intermediate container def456abc123 ---> 456def789abc Step 5/6 : CMD ["python", "app.py"] ---> Running in 789abc456def Removing intermediate container 789abc456def ---> 123abc456def Successfully built 123abc456def Successfully tagged my-python-app:latest
→
-t - Assigns a name and optionally a tag to the image
This command lists all Docker images on your system, so you can verify that 'my-python-app' was created successfully.
Terminal
docker images
Expected OutputExpected
REPOSITORY TAG IMAGE ID CREATED SIZE my-python-app latest 123abc456def 10 seconds ago 120MB python 3.11-slim abcdef123456 2 days ago 115MB
This command runs a container from the 'my-python-app' image to test that your app starts correctly. The --rm flag removes the container after it stops.
Terminal
docker run --rm my-python-app
Expected OutputExpected
* Serving Flask app 'app' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
→
--rm - Automatically removes the container when it exits
Key Concept

If you remember nothing else from this pattern, remember: the FROM instruction sets the starting point for your Docker image, defining the base environment your app will run in.

Common Mistakes
Using FROM without specifying a valid image name or tag
Docker will fail to build because it cannot find the base image to start from.
Always specify a valid base image name and tag, for example, 'FROM python:3.11-slim'.
Using 'latest' tag implicitly by writing 'FROM python' without a tag
This can lead to unpredictable builds because the 'latest' tag can change over time.
Specify an explicit tag like 'FROM python:3.11-slim' to ensure consistent builds.
Trying to use multiple FROM instructions without understanding multi-stage builds
Only the last FROM stage is used for the final image unless multi-stage build syntax is properly used.
Learn and use multi-stage build syntax properly if you need multiple FROM instructions.
Summary
The FROM instruction sets the base image for your Docker build.
Use docker build to create an image starting from the base image.
Verify the image with docker images and run it with docker run.

Practice

(1/5)
1. What does the FROM instruction do in a Dockerfile?
easy
A. It sets the base image for the Docker build.
B. It copies files from the host to the container.
C. It runs commands inside the container.
D. It exposes ports for the container.

Solution

  1. Step 1: Understand the role of FROM

    The FROM instruction defines the starting point or base image for building a Docker image.
  2. Step 2: Compare with other instructions

    Other instructions like COPY, RUN, and EXPOSE have different purposes, not setting the base image.
  3. Final Answer:

    It sets the base image for the Docker build. -> Option A
  4. Quick Check:

    FROM = base image [OK]
Hint: FROM always sets the base image in Dockerfile [OK]
Common Mistakes:
  • Confusing FROM with COPY or RUN instructions
  • Thinking FROM exposes ports
  • Assuming FROM copies files
2. Which of the following is the correct syntax for the FROM instruction to use the Ubuntu 22.04 image?
easy
A. FROM ubuntu/22.04
B. FROM ubuntu-22.04
C. FROM ubuntu:22.04
D. FROM ubuntu@22.04

Solution

  1. Step 1: Recall Docker image tag syntax

    The correct format is image_name:tag, so ubuntu:22.04 is valid.
  2. Step 2: Check other options

    Options with dashes, slashes, or @ are invalid for tags in FROM instruction.
  3. Final Answer:

    FROM ubuntu:22.04 -> Option C
  4. Quick Check:

    Use colon for tag in FROM [OK]
Hint: Use colon ':' to specify image tag in FROM [OK]
Common Mistakes:
  • Using dash '-' instead of colon ':' for tags
  • Using slash '/' incorrectly in image name
  • Using '@' instead of ':' for tags
3. Given this Dockerfile snippet:
FROM python:3.12-slim
RUN python --version

What will be the output when building this Docker image?
medium
A. Python 3.11.x
B. Python 3.12.x
C. Error: python command not found
D. Python 2.7.x

Solution

  1. Step 1: Identify base image version

    The base image is python:3.12-slim, which includes Python version 3.12.
  2. Step 2: Understand RUN command output

    The RUN command executes python --version, which prints the Python version installed in the image.
  3. Final Answer:

    Python 3.12.x -> Option B
  4. Quick Check:

    FROM python:3.12-slim runs Python 3.12 [OK]
Hint: Tag in FROM matches Python version output [OK]
Common Mistakes:
  • Assuming default Python version is 3.11 or 2.7
  • Thinking slim images lack Python
  • Confusing build output with runtime
4. Identify the error in this Dockerfile snippet:
FROM alpine
FROM ubuntu:20.04
RUN echo "Hello"
medium
A. FROM instruction must be last line.
B. Missing tag for alpine image causes error.
C. Multiple FROM instructions without stage names cause error.
D. There is no error in this Dockerfile snippet.

Solution

  1. 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).
  2. 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.
  3. Final Answer:

    There is no error in this Dockerfile snippet. -> Option D
  4. Quick Check:

    Multiple FROM without AS is valid [OK]
Hint: Multiple FROM instructions do not require stage names [OK]
Common Mistakes:
  • Assuming multiple FROM lines always work without names
  • Thinking RUN syntax is wrong here
  • Believing FROM must be last line
5. You want to build a multi-stage Dockerfile using node:18 as the build stage (named 'build') and nginx:alpine as the final stage. Which FROM instructions correctly set this up?
hard
A. FROM node:18 AS build FROM nginx:alpine
B. FROM node:18 FROM nginx:alpine AS build
C. FROM node:18 FROM nginx:alpine
D. FROM node:18 AS build FROM nginx:alpine AS build

Solution

  1. Step 1: Understand multi-stage build syntax

    Use AS to name the build stage, e.g., FROM node:18 AS build.
  2. Step 2: Set final stage without conflicting names

    The final stage uses FROM nginx:alpine without a stage name or with a different name.
  3. 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.
  4. Final Answer:

    FROM node:18 AS build FROM nginx:alpine -> Option A
  5. Quick Check:

    Use AS to name build stage first [OK]
Hint: Name build stage with AS, final stage without or different name [OK]
Common Mistakes:
  • Naming both stages the same
  • Placing AS on final stage incorrectly
  • Omitting AS for build stage in multi-stage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard