dockerfile - Docker equivalent of cd someth*ng using WORKDIR (wildcard change directory) - Stack Overflow

admin2025-04-17  3

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/

Share Improve this question edited Jan 31 at 0:35 Danny Beckett asked Jan 30 at 20:14 Danny BeckettDanny Beckett 20.9k26 gold badges113 silver badges142 bronze badges 7
  • it doesnt support globbing – Chris Doyle Commented Jan 30 at 20:31
  • Right @ChrisDoyle but what is the solution to change directory to a folder where only the partial name is known? Turn the script into a single gigantic RUN command, spanning dozens of lines, each concatenated with && \, starting with a cd? Since RUN cd only affects the same line, not subsequent RUN commands. Surely there's a better way? The docs say you can use an ENV in a WORKDIR, but there is no mention of using ARG. According to this answer you can't set an ENV var to the result of a RUN command (to find the dir name). – Danny Beckett Commented Jan 30 at 20:40
  • 1 This seems like an XY problem. Maybe if you share a bit about your docker file and why you feel you need this type of solutions. I have built hundreds of docker images and not once been in a position where i didnt know what dir I need to be in and had to use a glob pattern. – Chris Doyle Commented Jan 30 at 20:43
  • 2 Have a RUN that creates a symlink with a constant name (or renames the downloaded content to a constant name, etc). Use that constant name in a subsequent WORKDIR directive. Issue mooted. – Charles Duffy Commented Jan 30 at 20:47
  • @ChrisDoyle My Dockerfile clones a customer's Git repo. The files it needs to consume always live in a subdirectory of the clone; but that subdir can be named anything, with a known suffix - specifically .wiki - E.G. /something.wiki/ ..... maybe the solution is to just mv it to some constant name using BASH before changing dir with WORKDIR – Danny Beckett Commented Jan 30 at 20:47
 |  Show 2 more comments

2 Answers 2

Reset to default 1

Use a RUN to create a constant name

RUN set -- /some/*ing; test -d "$1" || exit; ln -sfT -- "$1" /workdir
WORKDIR /workdir
  • WORKDIR does not support the use of a * as a wildcard
  • RUN cd does support the use of a * as a wildcard, but will not affect subsequent RUN commands
  • RUN 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
  • Hard links are not supported for directories
  • Symbolic links are supported for directories, and support the use of a * as a wildcard

The 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!

转载请注明原文地址:http://www.anycun.com/QandA/1744892402a89104.html