Intrastellar Explorer
Intrastellar Explorer

Reputation: 2461

What is "/app" working directory for a Dockerfile?

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?

Upvotes: 60

Views: 98345

Answers (3)

Paulo Merson
Paulo Merson

Reputation: 14495

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).

Upvotes: 134

Hugo Lesta
Hugo Lesta

Reputation: 789

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

Upvotes: 26

Gavin Miller
Gavin Miller

Reputation: 43845

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.

Upvotes: 12

Related Questions