Dockerfile: Avoid Duplicate Aliases in FROM Instructions

Duplicate stage aliases in FROM instructions break multi-stage clarity and can fail builds. Use unique aliases for deterministic stage references.

Problem

Different FROM instructions use the same alias. This causes build errors and makes the Dockerfile harder to maintain.

Description

Multi-stage builds rely on stage aliases for artifact transfer and readability. Reusing the same alias across stages creates ambiguity and can break build behavior when COPY --from references no longer point to the intended stage.

Unique aliases improve maintainability during refactors, especially in larger Dockerfiles with build, test, and runtime stages. Clear stage naming also reduces review errors and speeds debugging when build steps fail.

Related rules: avoid self-referencing COPY –from, consolidate RUN instructions, pin image versions.

Solution

Assign a unique alias to each FROM stage and keep names descriptive. This preserves deterministic stage references and cleaner multi-stage workflows.

Problematic code

FROM ubuntu:20 as builder
RUN apt-get update && apt-get install --no-install-recommends -y build-essential
FROM node:14 as builder
RUN npm install

Verified code

FROM ubuntu:20 as builder-ubuntu
RUN apt-get update && apt-get install --no-install-recommends -y build-essential
FROM node:14 as builder-node
USER nobody
RUN npm install

Source of the description

Dockerfile reference: FROM