We have discussed the most important keywords commonly used in Dockerfiles. Let's look at a realistic and somewhat complex example of a Dockerfile. The interested reader might note that it looks very similar to the first Dockerfile that we presented in this chapter. Here is the content:
FROM node:9.4
RUN mkdir -p /app
WORKDIR /app
COPY package.json /app/
RUN npm install
COPY . /app
ENTRYPOINT ["npm"]
CMD ["start"]
OK, so what is happening here? Evidently, this is a Dockerfile that is used to build an image for a Node.js application; we can deduce this from the fact that the base image node:9.4 is used. Then the second line is an instruction to create a /app folder in the filesystem of the image. The third line defines the working directory or context in the image to be this new /app folder. Then, on line four, we copy a package.json file into the /app folder inside the image. After this, on line five, we execute the npm install command inside the container; remember, our context is the /app folder and thus, npm will find the package.json file there that we copied on line four.
After all Node.js dependencies are installed, we copy the rest of the application files from the current folder of the host into the /app folder of the image.
Finally, on the last two lines, we define what the startup command shall be when a container is run from this image. In our case, it is npm start, which will start the Node application.