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
ENV MY_HOME /test
WORKDIR ${MY_HOME}
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
root@2148a274721a:/${MY_HOME}#
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: [https://index.docker.io/v1/]
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.
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:
ENV DIRPATH /path
WORKDIR $DIRPATH/$DIRNAME
The output of the final pwd command in this Dockerfile would be /path/$DIRNAME
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.