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
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 .
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
-q
or getting a cached build in docker?
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.
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 .
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.
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
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.