I have a little more information on this.
I got the error when it reached WORKDIR $VARDIR but only if $VARDIR was in a chain of ENV variables.
Example of broken behavior:
ENV container docker
VARDIR /opt/mydir
WORKDIR $VARDIR
Example of working correctly behavior:
ENV container docker
ENV VARDIR /opt/mydir
WORKDIR $VARDIR