Pass arguments to parent Dockerfile
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
You cannot pass a build argument back into the Dockerfile that originally created an already-built parent image. Build arguments exist only while the current image is being built. If you want a child build to influence base-image selection or to reuse a value from the parent, you have to design that explicitly with ARG, ENV, or image tags.
Understand What a "Parent Dockerfile" Really Is
Once a base image has been built, Docker no longer sees the original Dockerfile as a live parent object. The child Dockerfile only sees the resulting image referenced by FROM. That means a command like docker build --build-arg FOO=bar . affects the current build, not the historical Dockerfile that produced my-base:latest.
So the answer depends on what you are really trying to do:
- choose which base image to use
- reuse a value during the current child build
- persist configuration from the base image into the child image
Those are different cases, and Docker handles them differently.
Use ARG to Parameterize the Current Build
If you want to choose the base image dynamically, declare an ARG before FROM and use it in the image reference.
Build it like this:
The NODE_VERSION argument works before FROM because Docker explicitly allows that pattern. The APP_ENV argument is available only after it is declared in the stage where it is used.
Persist Values Explicitly with ENV
If a value needs to survive into descendant images or into the running container, ARG alone is not enough because build arguments are not persisted automatically. The common pattern is to convert a build argument into an environment variable.
A child image built from this base can then read APP_HOME because it was baked into the image metadata as an environment variable.
That is not "passing an argument to the parent Dockerfile." It is persisting a value in the built parent image so descendants can consume it.
Redeclare ARG in Each Stage That Needs It
This is another place developers get tripped up. In multi-stage builds, an ARG must be declared in each stage where it is referenced.
The value can be passed once from the command line, but the ARG instruction still has to appear in every stage that wants to use it.
Common Pitfalls
The biggest mistake is assuming a child Docker build can reach back into the Dockerfile that produced the base image and set its ARG values after the fact. That is not how Docker's build model works.
Another common issue is expecting ARG values to exist at runtime. They do not, unless you explicitly copy them into ENV or another persistent layer.
People also often forget to redeclare ARG after FROM or in a later stage, which makes the value appear to "disappear" even though Docker is following the normal scoping rules.
Summary
- Build arguments affect the current Docker build, not an already-built parent Dockerfile.
- Use
ARGbeforeFROMwhen you want to parameterize base-image selection. - Use
ENVif a value must persist into child images or running containers. - Redeclare
ARGin every stage that needs it. - Think in terms of image metadata and build scope, not parent-Dockerfile inheritance.
Related reading
- Pass AWS credentials IAM role credentials to code running in Docker container
- Pass environment variables from docker-compose to container at build stage
- Passing data between a fragment and its container activity
- Passing JVM args to Docker image of Spring boot app on Kubernetes
- Passing long configuration file to Kubernetes
- Passing NODE_ENV to docker to run package.json scripts
- Permission denied on accessing host directory in Docker
- Pod in Kubernetes always in pending state

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.