Search code examples
dockerdocker-volume

Docker: environment variable in `VOLUME` instruction?


Let say I have this variable: ENV MY_DATA="/my/dir/data"

And then use it as: VOLUME [$MY_DATA]

After building image and connecting to container, what I see is this:

enter image description here

If I add it as VOLUME ["/my/dir/data"], then such artifact directories do not appear. Is there some special syntax I need to use via VOLUME so it would render my ENV correctly?


Solution

  • Here's a quick Dockerfile for illustration:

    FROM busybox
    # ARG MY_DATA="/my/dir/data" <- Works too.
    ENV MY_DATA="/my/dir/data"
    VOLUME $MY_DATA
    ENTRYPOINT ls -d /my/dir/data
    

    Build the image and run it:

    docker build -t <tag> .
    docker run -t --rm <tag>
    /my/dir/data        # list the volume created as expected
    - or -
    docker run -it --entrypoint "" --rm <tag> sh
    ls -d /my/dir/data  # list the volume created as expected