69

We have a problem with the WORKDIR when we building a docker image. Is it possible to print the value of WORKDIR?

We tried:

ECHO ${WORKDIR}

But there is no such instruction ECHO

Freewind
  • 193,756
  • 157
  • 432
  • 708

6 Answers6

65

There's no builtin way for Docker to print the WORKDIR during a build. You can inspect the final workdir for an image/layer via the .Config.WorkingDir property in the inspect output:

docker image inspect -f '{{.Config.WorkingDir}}' {image-name}

It's possible to view a Linux containers build steps workdir by printing the shells default working directory:

RUN pwd

or the shell often stores the working directory in the PWD environment variable

RUN echo "$PWD"

If the RUN step has run before and is cached, add the --no-cache flag. If you are using newer versions of docker with BuildKit, stdout from the build will need to be enabled with --progress=plain

docker build --no-cache --progress=plain . 
Matt
  • 68,711
  • 7
  • 155
  • 158
  • 13
    tried `RUN pwd` and `CMD pwd`, it does not print anything in the console while building the image. It only prints to the container's console. – A-Sharabiani Nov 17 '20 at 02:25
  • Are you using something other than docker to build the image? or maybe using `-q` or getting a cached build in docker? – Matt Nov 17 '20 at 07:34
  • I use `docker build`, and also tried `--no-cache` flag – A-Sharabiani Nov 17 '20 at 19:36
  • @Matt I think it does not work with BuildKit (only executed command is printend, not the output). – Wirone Sep 01 '21 at 14:59
  • @Wirone Thanks. can you confirm the fix? My docker instances are apparently stuck in the dark ages – Matt Sep 02 '21 at 00:57
  • I think the most foolproof is probably `RUN echo $(pwd)` – troyfolger Aug 09 '22 at 18:45
  • @troyfolger How would that change the output from a plain `pwd`? – Matt Aug 10 '22 at 04:56
  • @matt `RUN pwd` executes the `pwd` command ... the output is by default captured by the docker building process. `RUN echo $(pwd)` is telling RUN to *show* the output of the command. Try it both ways and see for yourself. – troyfolger Aug 10 '22 at 17:44
  • @troyfolger buildkit doesn't differentiate between the stdout of `pwd` or the stdout of `echo`. both are commands passed to sh. – Matt Sep 21 '22 at 08:04
17

There seems some recent change to the docker build command, where it hides stdout during the build process.

In short use DOCKER_BUILDKIT=0 docker build to get the "old" behavior back.

(reference)

ethergeist
  • 599
  • 4
  • 14
9

You can use below command in Dockerfile

RUN pwd && ls

Run the build with --progress=plain

docker build -t <image-name-you-want-to-give> --progress=plain .
Vikram Deokar
  • 141
  • 1
  • 5
8

You can check the content of directories during build steps and print it with commands like

RUN ls
RUN ls ..

Under ubuntu 20.04.4 LTS WSL2 I have to combine --progress=plain with debug mode and no-cache to see output:

docker -D build --progress=plain --no-cache. 
Evgeny
  • 791
  • 7
  • 15
  • 3
    This doesn't work either. It simply just shows the sha of the command and not the actual echo output. – Syed Saad Oct 05 '22 at 11:59
1

RUN doesn't print in my IDE console if I use docker-compose up. But CMD does. So try

CMD pwd

In case you have python in the image, this should also work

CMD ["python", "-c", "import os;print(os.getcwd())"]

Please note, only one, the last CMD command will be executed in the "container-run" phase. All others will be silently ignored. On the other side, there is a standard piping workaround:

CMD pwd && ls

Sergey Skripko
  • 336
  • 1
  • 8
1

I add the line to my dockerfile:

RUN pwd

To print the current workdir and then build the image using the command:

docker compose build --no-cache <name of service> 2>&1 | tee build.log

This will print the output in the terminal in a more verbose format and also do a complete log to the file build.log

I would expect you to be able to do something similar using just a docker command if you do not use docker compose.

Lars Ejaas
  • 153
  • 9