dockerdockerfiletilde-expansion

Tilde Expansion Doesn't Work in Docker CMD Command


I have a Dockerfile with the following line:

USER lodgepole

CMD ["tail", "-f", "~/block.txt"]

But container exits with error that the file ~/block.txt is not found

The following does work as expected:

CMD ["tail", "-f", "/home/lodgepole/block.txt"]

Is this a bug in Docker or is there a limitation related to tilde expansion that I'm not aware of?

Using Docker 20.10.9 on Linux.


Solution

  • Tilde expansion is done by the shell. When you use the exec form of CMD, there is no shell to do the expansion.

    You can use the shell form of CMD instead, which runs the command through a shell, like this

    CMD tail -f ~/block.txt
    

    or you can use the exec form and run the shell explicitly, like this

    CMD ["/bin/sh", "-c", "tail -f ~/block.txt"]
    

    The two commands do the exact same thing. If you use the shell form, it'll run the shell in the same way that the exec form does.

    More info here: https://docs.docker.com/engine/reference/builder/#cmd