Try   HackMD

nodejs Dockerfile tips

Type the following into the file. These statements produce a Dockerfile that describes the following:

  • The base stage includes environment setup which we expect to change very rarely, if at all.

    • Creates a new Docker image from the base image node:alpine. This base image has node.js on it and is optimized for small size.

    • Add curl to the base image to support Docker health checks.

    • Creates a directory on the image where the application files can be copied.

    • Exposes application port 3001 to the container environment so that the application can be reached at port 3001.

  • The build stage contains all the tools and intermediate files needed to create the application.

    • Creates a new Docker image from node:argon.

    • Creates a directory on the image where the application files can be copied.

    • Copies package.json to the working directory.

    • Runs npm install to initialize the node application environment.

    • Copies the source files for the application over to the image.

  • The final stage combines the base image with the build output from the build stage.

    • Sets the working directory to the application file location.

    • Copies the app files from the build stage.

    • Indicates the command to start the node application when the container is run.

Note: Type the following into the editor, as you may have errors with copying and pasting:

FROM node:alpine AS base RUN apk -U add curl WORKDIR /usr/src/app EXPOSE 3001 FROM node:argon AS build WORKDIR /usr/src/app # Install app dependencies COPY package.json /usr/src/app/ RUN npm install # Bundle app source COPY . /usr/src/app FROM base AS final WORKDIR /usr/src/app COPY --from=build /usr/src/app . CMD [ "npm", "start" ]