Issues passing Docker build args

Hi!

I’ve a simple Dockerfile containing an Angular build:

FROM node:alpine AS build-env
WORKDIR /app

ARG BUILD_COMMAND

COPY package.json ./
COPY package-lock.json ./

RUN npm i

COPY projects ./projects
COPY config ./config
COPY angular.json ./
COPY tsconfig.json ./
COPY tailwind.config.js ./

RUN $BUILD_COMMAND

As you can see, I’ve an ARG to run a certain build command that will be a bit different depending on deploying to production or staging environment.

Locally, when I test the Dockerfile, I run:

docker build --build-arg "BUILD_COMMAND=npm run build:prod" -t frontend .

It works as expected.

The Render documentation then tells me:

As I understand this, I can simply set up an environment variable and use it as a build arg in my Dockerfile.

Therefore, I’ve setup the environment variable like this:

image

But when I run a deployment, the following log is outputted:

Oct 4 04:04:41 PM  #19 [build-env 10/11] COPY tailwind.config.js ./
Oct 4 04:04:41 PM  #19 DONE 0.1s
Oct 4 04:04:41 PM  
Oct 4 04:04:41 PM  #20 [build-env 11/11] RUN $BUILD_COMMAND
Oct 4 04:04:41 PM  #20 DONE 0.2s
Oct 4 04:04:41 PM  
Oct 4 04:04:41 PM  #21 [stage-1 3/4] COPY --from=build-env /app/dist/smudgy /usr/share/nginx/html
Oct 4 04:04:41 PM  #21 ERROR: failed to calculate checksum of ref u1af40o4ezjdscs8wd738ils6::mn7wero8o7q9bempjeowvxorf: failed to walk /home/user/.local/tmp/buildkit-mount2121883536/app/dist: lstat /home/user/.local/tmp/buildkit-mount2121883536/app/dist: no such file or directory
Oct 4 04:04:41 PM  ------
Oct 4 04:04:41 PM   > [stage-1 3/4] COPY --from=build-env /app/dist/smudgy /usr/share/nginx/html:
Oct 4 04:04:41 PM  ------
Oct 4 04:04:41 PM  Dockerfile:24

It looks like the arg BUILD_COMMAND has not been set. Not sure, if I should see that in the build log, but I can tell from the running duration of the command that I was not executed properly.

As said, it works locally on my machine when setting the build arg.

Any idea what I’m missing to make it work on render?

Thanks!

Hmm,

I would expect that that work - can you try add RUN echo $BUILD_COMMAND after the ARG to see if the value is output in the build as a first debug step here,

John B

Good idea, I updated my Dockerfile to be:

# Build Env
FROM node:alpine AS build-env
WORKDIR /app

ARG BUILD_COMMAND
RUN echo $BUILD_COMMAND

# [...]

And the output is this one here:

image

This does not look correct, right?

Ok, I investigated a bit further.

As soon as I have a space in the value of my environment variable, it will not be translated to a build arg.

I tried some escaping with quotes, backslashes etc., but I haven’t found a working one, yet.

My intermediate solution is now to define BUILD_COMMAND with a value of build:staging instead of npm run build:staging and move the npm run part directly into the Dockerfile.

But I think it should work with spaces as well, right?

What about putting quotes around the environment variable?

$ export VAR=FOO

$ echo $VAR
FOO
$ export VAR=FOO BAH
$ echo $VAR
FOO
$ echo "$VAR"
FOO
$ export VAR="FOO BAH"
$ echo $VAR
FOO BAH

John B

In the Render settings? If so, as I mentioned, I’ve already tried that with no luck.

Is your code sample badly formatted? Should I put that into the Dockerfile?

Hi I’m having the same issue, too. I’m pulling an env variable:

ARG RENDER_HOSTNAME

FROM node:16.17.1

RUN NEXT_PUBLIC_API_HOST=https://$RENDER_HOSTNAME.onrender.com npm run build

But my app always shows “https://.onrender.com”. It looks like the env var is never transformed to the build arg.

Hi there,
We’ve identified and issue with how we were passing build args which should have fixed build args containing spaces.

John B

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.