jsarbour
jsarbour

Reputation: 1168

Docker WORKDIR - on my machine or the container?

What context does the WORKDIR keyword in a Dockerfile refer to? Is it in the context I run docker build from or inside the container I am producing?

I find myself often putting RUN cd && ... in my docker files and am hoping there's another way, I feel like I'm missing something.

Upvotes: 13

Views: 6982

Answers (3)

markybb41
markybb41

Reputation: 300

It is inside the container.

Taken for the Dockerfile reference site https://docs.docker.com/engine/reference/builder/#workdir

The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.

So rather than adding RUN cd && ... you could do:

WORKDIR /path/to/dir
RUN command

Upvotes: 8

David Maze
David Maze

Reputation: 159555

All paths in a Dockerfile, except the first half of COPY and ADD instructions, refer to image filesystem paths. The source paths for COPY and ADD are relative paths (even if they start with /) relative to the build context (the directory at the end of the docker build command, frequently the directory containing the Dockerfile). Nothing in a Dockerfile can ever reference an absolute path on the host or content outside the build context tree.

The only difference between these two Dockerfiles is the directory the second command gets launched in.

RUN cd /dir && command1
RUN command2
WORKDIR /dir
RUN command1
RUN command2

Upvotes: 6

k0pernikus
k0pernikus

Reputation: 66718

WORKDIR sets the directory inside the image and hence allows you to avoid RUN cd calls.

Upvotes: 1

Related Questions