11# Check out https://hub.docker.com/_/node to select a new base image
2- ARG nodeVer=18-alpine
3-
4- # select a base image to build from
5- FROM node:$nodeVer AS BASE
6-
7- # Take the build variables for image base
8- ARG SERVICE_NAME
9- ARG FROM_FOLDER
10- # This command is used to install some dependencies in the Docker image.
11- # Nessasary for running node-prune and npm install
12- RUN apk update && apk add --no-cache --virtual .gyp \
13- python3 \
14- make \
15- g++ \
16- bash \
17- curl
18-
19- # This is used to download and install the `node-prune` tool in the Docker image.
20- RUN curl -sfL https://gobinaries.com/tj/node-prune | bash -s -- -b /usr/local/bin
2+ FROM node:18-alpine
213
224# Set to a non-root built-in user `node`
235USER node
246
257# Create app directory (with user `node`)
268RUN mkdir -p /home/node/app
279
28- # Set the working directory to `/home/node/app`
2910WORKDIR /home/node/app
3011
3112# Install app dependencies
3213# A wildcard is used to ensure both package.json AND package-lock.json are copied
3314# where available (npm@5+)
3415COPY --chown=node package*.json ./
3516
36- # The following two commands is used to copy the `packages`,`service` directory from the local file system to the Docker image.
37- # The `--chown=node` flag ensures that the ownership of the copied files/directories is set to the `node` user.
38- # This is important because the subsequent commands in the Dockerfile are executed with the `node` user,
39- # and it needs the appropriate permissions to access and modify the copied files/directories.
40- COPY --chown=node packages ./packages
41-
42- COPY --chown=node $FROM_FOLDER/$SERVICE_NAME ./$FROM_FOLDER/$SERVICE_NAME
43-
44- # Installing all dependencies
4517RUN npm install
4618
47- # Building the app
48- # set the Working Directory to the service
49- WORKDIR /home/node/app/$FROM_FOLDER/$SERVICE_NAME
50- # Run Build Command
51- RUN npm run build
52-
53- # Run node-prune
54- RUN npm prune --production
55- RUN /usr/local/bin/node-prune
56-
57- # Start fresh for a smaller image size
58- FROM node:$nodeVer
59-
60- # Take the build variables for image stage
61- ARG SERVICE_NAME
62- ARG FROM_FOLDER
63-
64- RUN mkdir -p /home/node/app
65-
66- USER node
67-
68- WORKDIR /home/node/app
69-
70-
71- # These `COPY` commands are used to copy files and directories from the `BASE`
72- # stage of the Docker image to the current stage.
73- COPY --from=BASE --chown=node /home/node/app/node_modules ./node_modules
74- COPY --from=BASE --chown=node /home/node/app/package.json ./
75- COPY --from=BASE --chown=node /home/node/app/package-lock.json ./
76- COPY --from=BASE --chown=node /home/node/app/packages ./packages
77- COPY --from=BASE --chown=node /home/node/app/$FROM_FOLDER/$SERVICE_NAME ./$FROM_FOLDER/$SERVICE_NAME
78-
79- # Set the working directory to `/home/node/app/services/auth-service`
80- WORKDIR /home/node/app/$FROM_FOLDER/$SERVICE_NAME
19+ # Bundle app source code
20+ COPY --chown=node . .
8121
22+ RUN npm run build
8223
8324# Bind to all network interfaces so that it can be mapped to the host OS
8425ENV HOST=0.0.0.0 PORT=3000
8526
8627EXPOSE ${PORT}
87-
88- CMD [ "node" , "." ]
28+ CMD [ "node" , "." ]
0 commit comments