skip to Main Content
  • Dockerfile 1:

    FROM node:latest
    WORKDIR /app
    COPY ..
    RUN npm install
    EXPOSE 3000
    CMD ["node", "index.js"]
    
  • Dockerfile 2:

    FROM node:latest
    WORKDIR /app
    RUN npm install
    COPY ..
    EXPOSE 3000
    CMD ["node", "index.js"]
    

How are these two Dockerfiles different? When I build the docker file, everything will be executed except CMD step, but how are they different?

2

Answers


  1. On the Dockerfile 2 you set a WORKDIR and, with nothing there, the next step is RUN npm install, which executes in this empty directory.

    Taking a guess here, but I believe you are confused because your project is inside a /app folder itself. But remember the core idea of a container is to be isolated. The container has its own filesystem, whichever name you pick for WORKDIR does not have to match anything.

    Login or Signup to reply.
  2. the difference is in the Dockerfile 1 you are copying the directory before the packages installation

    the Dockerfile 2 one you’re copying after copying the directory

    the first might not be working because package.json not exist in the working directory

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search