85

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

7 Answers 7

80

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 . 
9
  • 17
    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. Commented Nov 17, 2020 at 2:25
  • 1
    Are you using something other than docker to build the image? or maybe using -q or getting a cached build in docker?
    – Matt
    Commented Nov 17, 2020 at 7:34
  • I use docker build, and also tried --no-cache flag Commented Nov 17, 2020 at 19:36
  • @Matt I think it does not work with BuildKit (only executed command is printend, not the output).
    – Wirone
    Commented Sep 1, 2021 at 14:59
  • 1
    I think the most foolproof is probably RUN echo $(pwd)
    – troyfolger
    Commented Aug 9, 2022 at 18:45
18

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)

12

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 .
11

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. 
1
  • 3
    This doesn't work either. It simply just shows the sha of the command and not the actual echo output.
    – Syed Saad
    Commented Oct 5, 2022 at 11:59
2

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

2

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.

-1

You can use below command

RUN `echo ls`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.