How to unset ENV in dockerfile?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Docker does not provide an UNSET instruction to remove environment variables set by ENV. Once set with ENV, a variable persists in all subsequent layers and in the running container. To work around this, you can set the variable to an empty string, use ARG instead of ENV for build-time-only variables, or use a shell command to unset it within a single RUN instruction. The approach depends on whether the variable is needed at build time, runtime, or both.
The Problem
Any ENV variable is baked into the image metadata and available in every subsequent layer and at container runtime.
Method 1: Set to Empty String
The simplest approach — override the variable with an empty value:
The variable still exists (it shows up in env output), but its value is empty. Most applications treat an empty string the same as unset.
Method 2: Use ARG Instead of ENV
If the variable is only needed during the build (not at runtime), use ARG instead:
ARG variables are automatically discarded after the build stage. They do not persist in the image metadata.
Converting ENV to ARG
Method 3: Unset in a RUN Command
Use unset in a shell command, but it only affects that single RUN instruction:
This is useful when a base image sets an ENV you want to suppress for a specific command:
Method 4: Multi-Stage Build
Use a multi-stage build to avoid carrying ENV variables from one stage to the next:
Multi-stage builds are the cleanest way to ensure build-time secrets do not leak into the final image.
Method 5: Override at Runtime
Override or unset environment variables when running the container:
Method 6: Use an Entrypoint Script
Create an entrypoint script that unsets variables before starting the application:
The exec "$@" replaces the shell process with the main command, so the unset variables remain gone.
Inspecting ENV Variables in an Image
Common Pitfalls
- Thinking
unsetin RUN persists:RUN unset MY_VARonly affects that single shell session. The nextRUNinstruction starts a new shell where theENVvariable is restored. EachRUNcreates a new layer with a fresh environment. - Secrets in ENV are visible in image metadata:
docker inspectanddocker historyreveal allENVvalues. Never put passwords, API keys, or tokens inENVinstructions. Use Docker BuildKit secrets (--mount=type=secret) orARGwith multi-stage builds instead. - ARG and ENV interaction:
ARGvalues can be captured byENV(ENV MY_VAR=$MY_ARG), which makes the value persist. If you useARGfor secrets, do not assign them toENV. - Base image ENV variables: Parent images may set
ENVvariables you do not control. Usedocker inspect base-imageto see inherited variables. Override them withENV VAR=or unset in an entrypoint script. - Empty string vs truly unset: Setting
ENV MY_VAR=makes the variable exist with an empty value. Some applications distinguish between "variable exists but empty" and "variable does not exist" (e.g., checkingif [ -z "${MY_VAR+x}" ]). Use an entrypoint script withunsetfor true removal.
Summary
- Docker has no
UNSETinstruction —ENVvariables persist in all subsequent layers and at runtime - Set to empty (
ENV VAR=) for the simplest workaround - Use
ARGinstead ofENVfor build-time-only variables - Use multi-stage builds to prevent build-time variables from leaking into the final image
- Use an entrypoint script with
unsetfor true runtime removal - Never store secrets in
ENV— use Docker BuildKit secrets orARGwith multi-stage builds
Related reading
- How to update a set of pods running in kubernetes?
- How to update docker stack without restarting all services
- How to update /etc/hosts file in Docker image during docker build
- How to update existing images with docker-compose?
- How to upgrade docker container after its image changed
- How to use --volume option with Docker Toolbox on Windows?
- How to use an init container to check if MySQL is ready for connections?
- How to use bash with an Alpine based docker image?

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.