48

The Docker image (Windows-based) includes an application directory at C:\App. Inside that directory reside several sub-folders and files, including a batch file called process.bat. The Dockerfile (used to build the image) ends like this:

ENTRYPOINT [ "C:\\App\\process.bat" ]

When I instantiate this image using the command: docker run company/app, the batch file runs, but it fails at the point where other files under C:\App are referenced. Essentially, the working directory is still C:\ from the Docker container's entry-point.

Is there a way to set the working directory within the Dockerfile? Couple of alternatives do exist:

  • Add -w C:\App to the docker run
  • In the batch file, I can add a line at the beginning cd /D C:\App

But is there a way to specify the working directory in the Dockerfile?

2 Answers 2

65

WORKDIR /App is a command you can use in your dockerfile to change the working directory.

1
  • Yup, WORKDIR changes the CWD for the command specified in ENTRYPOINT. Commented Aug 3, 2023 at 8:49
30

If /App is a mounted volume then you should specify VOLUME /App before WORKDIR to use it with ENTRYPOINT, otherwise it does not be seen by ENTRYPOINT:

VOLUME ["/App"]
WORKDIR /App
ENTRYPOINT ["sh", "start.sh"]

Which start.sh is within /App directory.

2
  • This doesn't work if you are using the image build by that Dockerfile to build another image, whose Dockerfile which might not set the same WORKDIR back to the original location at the end.
    – Josh M.
    Commented May 30, 2023 at 19:13
  • @JoshM. This is for runtime man. This is not for manipulating and using files in the process of building an image. This get used when the image is ran as a container. Commented Jun 2, 2023 at 19:08

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