Common DockerFile directives:
Estimated reading: 4 minutes 4 views

🐳 DockerFile Instruction – FROM Explained in Detail

The FROM instruction is the foundation of every DockerFile. It’s the first instruction you’ll use and arguably one of the most critical. It tells Docker which base image to use for building your new image.

Let’s dive deep into what FROM does, how to use it, and some best practices for working with it.


📌 What is the FROM Instruction?

The FROM instruction specifies the base image for subsequent instructions in the DockerFile. Every DockerFile must begin with a FROM unless it uses ARG before it (to define a variable for use in the FROM line).

📘 Syntax:

FROM <image>[:<tag>] [AS <name>]

🔹 <image>: The name of the base image
🔹 <tag> (optional): The tag/version of the image (default is latest)
🔹 AS <name> (optional): Used for naming stages in multi-stage builds


💡 Examples of the FROM Instruction

✅ Basic Example:

FROM ubuntu:20.04

This tells Docker to use Ubuntu 20.04 as the base image.

✅ With Latest Tag (implicit):

FROM node

This is the same as:

FROM node:latest

✅ Using Multi-Stage Build:

FROM golang:1.18 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp

FROM alpine:latest
COPY --from=builder /app/myapp /usr/local/bin/myapp
CMD ["myapp"]

🔸 In the above, we use golang as a build environment and copy the compiled binary into a lightweight alpine image. This reduces the final image size drastically.


🛠️ Why is FROM Important?

  • 🧱 Sets the base environment for your application.
  • 🐛 Helps manage dependencies and system tools.
  • 🏗️ Enables multi-stage builds for optimization.
  • 📦 Controls the size and security of your final image.

🐳 DockerFile FROM Command – Function Table

🛠️ Function📝 Description📌 Example
1. Set a base imageDefines the base image to build your Docker image fromFROM ubuntu:22.04
2. Use official imagesUses prebuilt official images from Docker HubFROM node:20-alpine
3. Use custom/private imagesUses images from your own registry or a private repoFROM myregistry.com/myapp/base:latest
4. Use a specific tagSelects a particular version of an imageFROM python:3.11-slim
5. Use image by digestUses an immutable reference for more secure buildsFROM nginx@sha256:abc123...
6. Create multi-stage buildsUses multiple FROM instructions to build in stages and reduce image sizeFROM golang:1.21 as builder
FROM alpine
7. Name build stagesAssigns a name to a stage for reuse later in the DockerFileFROM node:20 as build-stage
8. Pull from private registryPulls images from a secure/private image registryFROM registry.example.com/app/image:tag
9. Specify platformEnsures image runs on a specific CPU architectureFROM --platform=linux/amd64 node:20

🚨 Common Mistakes to Avoid

❌ Mistake✅ Solution
Omitting the FROM instructionAlways include FROM as the first instruction
Using large base imagesPrefer minimal images like alpine or scratch
Forgetting to pin tagsUse specific tags like python:3.11 instead of latest

🧰 Best Practices

🔒 Use minimal base images like alpine to reduce attack surface
📦 Always pin the version of the image (FROM node:18)
🛠️ Leverage multi-stage builds to create efficient, production-ready images
✅ Regularly update base images to patch vulnerabilities


🔚 Conclusion

The FROM instruction is your starting point when building a Docker image. It sets the tone for everything that follows. Using the right base image, tagging it correctly, and optimizing with multi-stage builds can make your Docker images more secure, efficient, and production-ready.


📝 Quick Recap

✅ Starts every DockerFile
✅ Specifies the base image
✅ Can be used multiple times in multi-stage builds
✅ Accepts image name, tag, and alias (AS)
FROM scratch creates a blank image
✅ Use with ARG for dynamic base image selection


📚 FAQs – FROM Instruction in Dockerfile

❓ What happens if I don’t specify a FROM instruction?

➡️ Docker will throw an error:
Dockerfile parse error: No FROM instruction found


❓ Can I have more than one FROM instruction?

✅ Yes! This is common in multi-stage builds. Each FROM starts a new build stage.

FROM node:18 AS build
# build steps

FROM nginx:alpine
# use files from build stage

❓ What is scratch in FROM scratch?

🔍 scratch is a special, empty image. It’s used to build minimal images from scratch, often in Go, Rust, or C.

FROM scratch
COPY mybinary /mybinary
CMD ["/mybinary"]

❓ Can I use environment variables in the FROM instruction?

🔧 Yes, but only with ARG declared before the FROM instruction:

ARG BASE_IMAGE=python:3.10
FROM ${BASE_IMAGE}

❓ What’s the difference between FROM image and FROM image AS name?

🆚 AS name is used in multi-stage builds to label the stage so you can refer to it later.

Example:

FROM golang:1.20 AS build
...
COPY --from=build /app /app

Leave a Reply

Your email address will not be published. Required fields are marked *

Share this Doc

FROM

Or copy link

CONTENTS
Scroll to Top