Dockerfile WORKDIR seems to ignore ENV value


I have a problem with WORKDIR command in Dockerfile. It seems to not accept variable I set by ENV command before.
My sample dockerfile showing this behaviour is

FROM ubuntu:latest


CMD /bin/bash

I build

docker build -t me2d/test:1.3 .

When I run I get

petr@linux64:~/my/docker/test$ docker run -ti --rm me2d/test:1.3

What am I doing wrong? Using command WORKDIR /test works as expected. ENV values should work by reference
Thanks for any hint.

EDIT: Of course the problem is my current directory when running the container is named ${MY_HOME} and not /test.

My docker is:

petr@linux64:~/my/docker/test$ docker info
Containers: 2
Images: 36
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Dirs: 40
Execution Driver: native-0.2
Kernel Version: 3.13.0-59-generic
Username: me2d
Registry: []
WARNING: No swap limit support


The ‘docker build’ command never evaluates environment variables in any Dockerfile directive directly.

This isn’t made obvious by the fact that the RUN commands are all encapsulated in a ‘/bin/sh -c’ command. Any environment variable reference is passed to the shell and the shell evaluates it.

There is no involvement of /bin/sh for WORKDIR, so the value gets passed in verbatim.

Hopefully this explanation makes sense.


Thanks for explanation Jeff, I can understand that.
Just one more question: does it mean that Dockerfile reference is wrong in this point? It explicitly states:

The WORKDIR instruction can resolve environment variables previously set using ENV. You can only use environment variables explicitly set in the Dockerfile. For example:

The output of the final pwd command in this Dockerfile would be /path/$DIRNAME

Here’s the link:


Ah, in that case, I am wrong. I would suspect that you are seeing that behavior since you are using the ${MY_HOME} format instead of the $MY_HOME format as seen in the docs.

Unfortunately not, using $MY_HOME is giving the same result… :-/

I’d also remove the underscore for testing, though the docs do say it should work.

oh, and what does docker version output?

That’s it.
I had old version of Docker.
With 1.7.1 it works fine.
Thanks for help!