Search code examples
dockerfiledocker-multi-stage-build

Name a stage per build-arguments using a Dockerfile with multi-stage-builds


Is there a possibiliity to name a build-stage? I am searching something like the following example:

ARG NAME

FROM python:3.7-alpine as modul-${NAME}
# ...

If I try this example this error occurs:

Error response from daemon: Dockerfile parse error line 5: invalid name for build stage: "modul-${NAME}", name can't start with a number or contain symbols

I also tryed to use the argument as tag (modul:${NAME}) with the same result.


Solution

  • You can do this with BuildKit, which requires docker 18.09+. (https://docs.docker.com/develop/develop-images/build_enhancements/)

    All you have to do is set an env variable before building:

    DOCKER_BUILDKIT=1 docker build -t whatever .

    I don't think it's possible without BuildKit.