Serhii Shushliapin
Serhii Shushliapin

Reputation: 2708

Building a Docker image with a configured version of node package installed

I want to build a Docker image which contains a node package installed. If the package version is omitted or hardcoded in the Dockerfile, everything is OK (@14.0.0):

FROM stefanscherer/node-windows:12.16.1-nanoserver-1909
RUN npm install -g @sitecore-jss/[email protected]

Build command and result:

docker build -t sitecore-jss-cli:14.0.0-nanoserver-1909 .

Successfully built 1c0ebbcd5be2
Successfully tagged sitecore-jss-cli:14.0.0-nanoserver-1909

But when the version is passed as an argument (to be able to build any version), the error occurs. Please take a look at the updated Dockerfile:

ARG SITECOREJSS_VERSION
FROM stefanscherer/node-windows:12.16.1-nanoserver-1909
RUN npm install -g @sitecore-jss/sitecore-jss-cli@${SITECOREJSS_VERSION}

Command with the argument and error:

docker build --build-arg SITECOREJSS_VERSION=14.0.0 -t sitecore-jss-cli:14.0.0-nanoserver-1909 .

...
npm ERR! code EINVALIDTAGNAME
npm ERR! Invalid tag name "${SITECOREJSS_VERSION}": Tags may not have any characters that encodeURIComponent encodes.

Looks like the argument needs to be escaped in some way. Any clue how to fix that?

Upvotes: 1

Views: 497

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 122007

You have two problems:

  1. Ordering

    An ARG outside the FROM block is only accessible in the FROM line itself. In this case, as you don't need to use the --build-arg as part of the FROM, move it inside:

    FROM stefanscherer/node-windows:12.16.1-nanoserver-1909
    ARG SITECOREJSS_VERSION
    ...
    

    If you need to use it in FROM and elsewhere in the Dockerfile, you need to be explicit about that:

    ARG SITECOREJSS_VERSION
    FROM ...
    ARG SITECOREJSS_VERSION
    ...
    
  2. Interpolation

    Per this issue on GitHub, if you want to do interpolation in the commands in Windows images, you need to use %:

    RUN npm install -g @sitecore-jss/sitecore-jss-cli@%SITECOREJSS_VERSION%
    

So the complete working version would be:

FROM stefanscherer/node-windows:12.16.1-nanoserver-1909
ARG SITECOREJSS_VERSION
RUN npm install -g @sitecore-jss/sitecore-jss-cli@%SITECOREJSS_VERSION%

Upvotes: 2

Related Questions