Skip to content

allow setting env vars only at build time #3345

@juchem

Description

@juchem

Description
This is a feature request to allow setting environment variables at build time, which don't linger around at container runtime.

This would be equivalent to adding the --env cli argument to docker build.

The best use case for this is proxy servers:
When building images in a CI/CD or development environment, it would be highly beneficial to have caching proxies for HTTP/HTTPS and package managers (e.g.: apt-cacher-ng). This could greatly speed up the development, build and test cycles; as well as relieve pressure on the outbound internet connection and local uncached artifact servers and whatnot.

The straightforward way one would expect to do this is through --build-arg and ARG. It is common, though, to have the default value for an environment variable be configurable at build time:

ARG http_proxy="..."
ENV http_proxy="${http_proxy}"

Overriding the value of ARG http_proxy at build time, in that case, would end up also overriding the value of ENV http_proxy at runtime.

One may argue that, for proxies, specifically, it's a bad idea to default a value in the image and it should be left empty, therefore the proper way to do this would be:

ARG http_proxy="..."  # use it at build time
ENV http_proxy=""  # leave it blank at runtime

That's generally true, but use-cases vary and it does make sense for very specific use-cases.
Also, environment variables aren't only used for proxy configurations. They're rather general tools, so a general solution is much in demand. It just so happens that the point is more easily expressed with proxy servers.

Other alternatives could look something like:

ARG build_time_http_proxy=""  # overridable at build time
ARG http_proxy=""  # default value for runtime
ENV http_proxy="${http_proxy}"

RUN [ -z "${build_time_http_proxy}" ] || export http_proxy="${build_time_http_proxy}"

That leads to a less-elegant solution, though, and doesn't work when the proxy is set in a base image. It requires mass editing all Dockerfiles and adding the above workaround.

The proposed solution would enable the intended effect to be obtained by simply adding --env https_proxy=... to docker build in the associated build system target, thus propagating to all images for free.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions