63

In the docker docs getting started tutorial part 2, it has one make a Dockerfile. It instructs to add the following lines:

# Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app 

What is /app, and why is this a necessary step?

1

3 Answers 3

138

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 the parent image might have set it), but setting it is 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 . . ADD data/mydata.csv /usr/share/mydata.csv 

then...

  • the COPY command will copy the contents of your build context directory to the /myapp dir inside your docker image.
  • the ADD command will copy file data/mydata.csv under your build context dir to the /usr/share dir at the docker image filesystem root level (<dest> in this case starts with /, so it's an absolute path).
Sign up to request clarification or add additional context in comments.

13 Comments

Finally an answer and example that really explains this s#!*. So far all I've seen is "WORKDIR THIS" and "COPY THAT" with no explanation what is being copied to where and what is the magical app folder that everyone seems to have. Thanks.
hurray! Dear @Paulo could you add a bit more on how the base image (let's assume alpine) filesystem is seen within the container image and with respect to the WORKDIR, defined above?
Your comment about base image setting the WORKDIR was super helpful. I wasn't able to figure out how the path was /app from the get go and the docker documentation doesn't state that either.
This thing about the build context and workdir should be part of the getting started guide.
Also important to note is the --from=<stage> option on commands like COPY. These change the "build context" to be the output from the specified stage of the build rather than the native filesystem.
|
26

WORKDIR is a good practice because you can set a directory as the main directory, then you can work on it using COPY, ENTRYPOINT, CMD commands, because them will execute pointing to this PATH.

Docker documentation: https://docs.docker.com/engine/reference/builder/

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.

The WORKDIR instruction can be used multiple times in a Dockerfile. If a relative path is provided, it will be relative to the path of the previous WORKDIR instruction.

Dockerfile Example:

FROM node:alpine WORKDIR '/app' COPY ./package.json ./ RUN npm install COPY . . CMD ["npm", "run", "start"] 

A alpine node.js was created and the workdir is /app, then al files are copied them into /app

Finally npm run start command is running into /app folder inside the container.

You should exec the following command in the case you have sh or bash tty:

docker exec -it <container-id> sh

or

docker exec -it <container-id> bash

After that you can do ls command and you will can see the WORKDIR folder.

I hope it may help you

4 Comments

Okay follow up quesion @Hugo Lesta. Is WORKDIR created within the context of that specific container? In other words, where is that directory created?
@IntrastellarExplorer you should exec the following command in the case you have sh or bash tty: docker exec -it <container-id> sh or docker exec -it <container-id> bash After that you can do ls command and you will can see the WORKDIR folder.
Alright I ran the command with bash. @Hugo Lesta the input: root@c95328d92adf:/app# pwd and the output: /app Does this mean that /app is just a sub-directory of the a root directory in the container?
Hey @IntrastellarExplorer sorry for my delay, that is right!
12

You need to declare a working directory and move your code into it, because your code has to live somewhere. Otherwise your code wouldn't be present and your app wouldn't run. Then when commands like RUN, CMD, ENTRYPOINT, COPY, and ADD are used, they are executed in the context of WORKDIR.

/app is an arbitrary choice of working directory. You could use anything you like (foo, bar, or baz), but app is nice since it's self-descriptive and commonly used.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.