137

We can use WORKDIR to set a directory as work directory in Dockerfile, but what's the default value if we don't set?

John Smith
  • 7,243
  • 6
  • 49
  • 61
Freewind
  • 193,756
  • 157
  • 432
  • 708

4 Answers4

92

docker workdir

says it is /, so the root directory

ThoFin
  • 1,467
  • 14
  • 20
62

The default is indeed / as stated elsewhere. It is worth mentioning, though, that you will almost never be running from an empty docker image (FROM scratch), so the WORKDIR is likely set by the base image you're using.

For instance, https://github.com/docker-library/tomcat/blob/master/Dockerfile-alpine.template has WORKDIR $CATALINA_HOME, and https://github.com/dockerfile/ubuntu/blob/master/Dockerfile has WORKDIR /root. (https://hub.docker.com/r/base/archlinux/~/dockerfile/ does not use WORKDIR however.)

It is best, therefore, to set your own WORKDIR explicitly.

lmat - Reinstate Monica
  • 7,289
  • 6
  • 48
  • 62
26

The default working directory for running binaries within a container is the root directory (/), but the developer can set a different directory with the Dockerfile WORKDIR command. The operator can override this with:

-w="": Working directory inside the container

Here: https://docs.docker.com/engine/reference/run/#workdir

Paulo Merson
  • 13,270
  • 8
  • 79
  • 72
hvardhan
  • 460
  • 8
  • 14
5

As there is are no users but a root in a just born docker or a container strictly speaking. So, for every change there will be a commit for that container as this guy said. Hence the pwd of that or as you asked the WORKDIR is / root by default and every time you exec a /bin/bash like this:

$docker exec -i -t 53f784fwer54 /bin/bash

on a running container it will put you here / in the root dir.

Community
  • 1
  • 1
  • 7
    Your answer is not only off-topic, it's just plain wrong. First, the question is: What is the default WORKDIR of an IMAGE if one is not set explicitly in the Dockerfile. Your answer saying that the default workdir of a new CONTAINER is `/` is incorrect and irrelevant. The default workdir of a new container is the same as the default workdir of the image. For instance, I just ran `docker exec -ti /bin/bash;` then in the new bash process, `pwd`, and I see that the working directory is `/app`. – lmat - Reinstate Monica Nov 09 '20 at 22:33