What is “/app” working directory for a Dockerfile?

There are two important directories when building a docker image:

  • the build context directory.
  • the WORKDIR directory.

Build context directory

It’s the directory on the host machine where docker will get the files to build the image. It is passed to the docker build command as the last argument. (Instead of a PATH on the host machine it can be a URL). Simple example:

docker build -t myimage .

Here the current dir (.) is the build context dir. In this case, docker build will use Dockerfile located in that dir. All files from that dir will be visible to docker build.

The build context dir is not necessarily where the Dockerfile is located. Dockerfile location defaults to current dir and is otherwise indicated by the -f otpion. Example:

docker build -t myimage -f ./rest-adapter/docker/Dockerfile ./rest-adapter

Here build context dir is ./rest-adapter, a subdirectory of where you call docker build; the Dokerfile location is indicated by -f.

WORKDIR

It’s a directory inside your container image that can be set with the WORKDIR instruction in the Dockerfile. It is optional (default is /, but base image might have set it), but considered a good practice. Subsequent instructions in the Dockerfile, such as RUN, CMD and ENTRYPOINT will operate in this dir. As for COPY and ADD, they use both…

COPY and ADD use both dirs

These two commands have <src> and <dest>.

  • <src> is relative to the build context directory.
  • <dest> is relative to the WORKDIR directory.

For example, if your Dockerfile contains…

WORKDIR /myapp
COPY . .

then the contents of your build context directory will be copied to the /myapp dir inside your docker image.

Leave a Comment