...
Code Block |
---|
WORKDIR /app |
iii) Install dependencies. We add instructions for installing dependencies before copying the code as docker caches the results of each and every instruction while building image layers. For every code change we do not want it to fetch and install dependencies all over again. A sample instruction for installing dependencies in case of node is given below -
Code Block |
---|
COPY package.json /app
RUN npm install |
“RUN” instruction executes a particular command which is provided at the time of building the image.
iv) Next, we specify which files in our application dir directory should go into the image -
...
Here, “.” represents the path where the files to be copied in into the image lie and “/app” represents the path inside the image i.e. internal filesystem inside docker container.iv)
v) We add “EXPOSE” instruction so as to specify at which port the application is listening on.
Code Block |
---|
EXPOSE 3000 |
vi) At last we add a “CMD” instruction to provide the command that should be executed once the container starts.
Code Block |
---|
CMD ["node", "app.js"] |
The difference between “RUN” and “CMD” instructions is that the “RUN” instruction executes at the time image is getting created whereas “CMD” instruction executes when a container based on this image starts.