Freewind
Freewind

Reputation: 198188

Is it possible to show the `WORKDIR` when building a docker image?

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

Upvotes: 86

Views: 162993

Answers (8)

Sid110307
Sid110307

Reputation: 568

If you need to list after building, you can run docker-compose exec <img> pwd or docker-compose exec <img> ls.

Upvotes: 0

rami reddy
rami reddy

Reputation: 1

You can use below command

RUN `echo ls`

Upvotes: -1

Matt
Matt

Reputation: 74660

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 . 

Upvotes: 80

Lars Ejaas
Lars Ejaas

Reputation: 181

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.

Upvotes: 2

Evgeny
Evgeny

Reputation: 1273

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. 

Upvotes: 12

Vikram Deokar
Vikram Deokar

Reputation: 171

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 .

Upvotes: 12

Sergey Skripko
Sergey Skripko

Reputation: 364

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

Upvotes: 2

ethergeist
ethergeist

Reputation: 619

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)

Upvotes: 18

Related Questions