×
☰ See All Chapters

Docker WORKDIR command

The WORKDIR command can be used to set the desired working directory. By default working directory will be root directory.  The WORKDIR command set the working directory to the path you specify in the command. RUN, CMD, and ENTRYPOINT commands will consider the directory set by the WORKDIR instruction. If no WORKDIR command is available to set the work directory then RUN, CMD, and ENTRYPOINT commands will use the root directory.

Docker WORKDIR command syntax

WORKDIR <workdirpath>

<workdirpath>: Work directory to set

  • The path can be either absolute or relative. In case of a relative path, it will be relative to the current path. If WORKDIR is used before then relative path will be set according to that previous WORKDIR path. If no WORKDIR command used before then path will be set relative to root director. 

  • If <workdirpath> starts from / then it is absolute path. Path will be set from root. 

FROM openjdk:13-jdk-alpine

WORKDIR /test/path/

WORKDIR /app/path/

ENTRYPOINT ["pwd"]

docker-workdir-command-0
 
  • If <workdirpath> does not starts from / then it is relative path. Path will be set relative to the current path. 

docker-workdir-command-1
 

<workdirpath> can optionally end with /, WORKDIR app/path and WORKDIR app/path/ both are same.

docker-workdir-command-2
 

All Chapters
Author