Docker Community Forums

Share and learn in the Docker community.

Dockerfile WORKDIR seems to ignore ENV value


(Petr Medek) #1

Hello

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

Petr


(Jeff Anderson) #2

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.

/Jeff


(Petr Medek) #3

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

Here’s the link: https://docs.docker.com/reference/builder/#workdir

Petr


(Jeff Anderson) #4

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.


(Petr Medek) #5

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


(Sven Dowideit) #6

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

http://docs.docker.com/reference/builder/#environment-replacement


(Sven Dowideit) #7

oh, and what does docker version output?


(Petr Medek) #8

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

Petr