In BASH we can change directory to an unknown name, using a *
as a wildcard - for example:
cd /some/*ing
will change directory to cd /some/thing/
(if there is only 1 match for the wildcard)
How can we do the same thing using WORKDIR
in a Dockerfile?
If I try to do WORKDIR /some/*ing
, then it instead creates a directory named literally /some/*ing/
and it changes directory to that, instead of /some/thing/
In BASH we can change directory to an unknown name, using a *
as a wildcard - for example:
cd /some/*ing
will change directory to cd /some/thing/
(if there is only 1 match for the wildcard)
How can we do the same thing using WORKDIR
in a Dockerfile?
If I try to do WORKDIR /some/*ing
, then it instead creates a directory named literally /some/*ing/
and it changes directory to that, instead of /some/thing/
RUN set -- /some/*ing; test -d "$1" || exit; ln -sfT -- "$1" /workdir
WORKDIR /workdir
WORKDIR
does not support the use of a *
as a wildcardRUN cd
does support the use of a *
as a wildcard, but will not affect subsequent RUN
commandsRUN mv
to move the directory to a constant known directory name could affect other programs (E.G. git init
) that expect the directory to exist at the unknown name*
as a wildcardThe following code can be used to change to a directory where only the last part of the directory's name is known (ending with .wiki
).
For example to change directory to /docs/someunknowndir.wiki/
, using a symlink at /docs/wiki/
:
# Starting directory
WORKDIR /docs
# Create a symlink from the unknown name to a known name
RUN ln -s *.wiki wiki
# Change to the dir, using the known name
WORKDIR wiki
# Do stuff with the dir - E.G.
RUN ls -Rlah .
# When finished doing stuff with the dir...
# Reset to the starting directory
WORKDIR ..
# Delete the dir at the unknown name + the symlink at the known name
RUN rm -rf *.wiki wiki
Thanks goes to @CharlesDuffy for the symlink idea!
RUN
command, spanning dozens of lines, each concatenated with&& \
, starting with acd
? SinceRUN cd
only affects the same line, not subsequentRUN
commands. Surely there's a better way? The docs say you can use anENV
in aWORKDIR
, but there is no mention of usingARG
. According to this answer you can't set anENV
var to the result of aRUN
command (to find the dir name). – Danny Beckett Commented Jan 30 at 20:40WORKDIR
directive. Issue mooted. – Charles Duffy Commented Jan 30 at 20:47.wiki
- E.G./something.wiki/
..... maybe the solution is to justmv
it to some constant name using BASH before changing dir withWORKDIR
– Danny Beckett Commented Jan 30 at 20:47