Search code examples
dockerdockerfile

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?


Solution

  • 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