ARG in WORKDIR not applied

Why is it that the below Dockerfile sets the working directory while ignoring the ARG?

ARG TOPIC=esa

FROM asadow/dev_env_r:4.3.2
RUN apt-get update […]

WORKDIR /home/{$TOPIC}

When I start a container from this image, I am in the “home” directory, and no “esa” folder exists.

https://docs.docker.com/engine/reference/builder/#arg

Arguments must be after the FROM instruction. ARG before the FROM instrution can only be used in the FROM instruction. And I don’t think {$TOPIC} is a valid syntax, but I could be wrong. The documented and definitely working syntax is ${TOPIC} or just simply $TOPIC

1 Like

Ah, thank you!

Do you know why there is no build error, however? Why would WORKDIR /home/$TOPIC simply ignore $TOPIC?

Apologies with the syntax.

It is explained in the same documentation I linked in my previous post.

Prior to its definition by an ARG instruction, any use of a variable results in an empty string.

In other words, it works like variables in a shell, so unset variables will be used as empty string. You can use this syntax to avoid it:

WORKDIR /home/${TOPIC:?}

Or add a custom error message:

WORKDIR /home/${TOPIC:?Do not leave it empty}
1 Like

Many lessons learned. Thanks again.