-
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
On the
Dockerfile 2
you set aWORKDIR
and, with nothing there, the next step isRUN 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 forWORKDIR
does not have to match anything.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