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?
Within the virtual image, the path is the default Docker path /var/lib/docker .
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.
The WORKDIR command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory.
There are two important directories when building a docker image:
WORKDIR
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
.
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 base image might have set it), but 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...
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 . .
then the contents of your build context directory will be copied to the /myapp
dir inside your docker image.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With