Search code examples
dockerdockerfile

Dynamic image resolution in docker multi stage builds


Is it possible to have a dynamic image resolution in docker for multistage builds?

The following works

ARG node_version=10-alpine

FROM node:${node_version} as stage1

but the following

FROM ubuntu as dbdump

ARG node_version=10-alpine

FROM node:${node_version} as stage1

fails with the error

Dockerfile:5
--------------------
   3 |     ARG node_version=10-alpine
   4 |
   5 | >>> FROM node:${node_version} as stage1
   6 |
   7 |
--------------------
ERROR: failed to solve: failed to parse stage name "node:": invalid reference format

Solution

  • Any ARGs that get used in FROM lines need to be declared before the very first FROM line. Anything that's after a FROM line is specific to that build stage.

    ARG node_version=10-alpine
    
    FROM ubuntu as dbdump
    ...
    
    FROM node:${node_version} as stage1
    ...