|
1 | | -# syntax = docker/dockerfile:1 |
| 1 | +# Stage 1: Build the Application |
| 2 | +# We use node:22 as the base for building and installing dependencies. |
| 3 | +FROM node:22 AS build |
2 | 4 |
|
3 | | -# Adjust NODE_VERSION as desired |
4 | | -ARG NODE_VERSION=22.16.0 |
5 | | -FROM node:${NODE_VERSION}-slim AS base |
| 5 | +# Set the working directory inside the container |
| 6 | +WORKDIR /usr/src/app |
6 | 7 |
|
7 | | -LABEL fly_launch_runtime="Node.js" |
| 8 | +# Copy package.json and package-lock.json first to leverage Docker caching. |
| 9 | +# If these files don't change, subsequent builds can skip 'npm install'. |
| 10 | +COPY package*.json ./ |
8 | 11 |
|
9 | | -# Node.js app lives here |
10 | | -WORKDIR /app |
| 12 | +# Install dependencies |
| 13 | +RUN npm install |
11 | 14 |
|
12 | | -# Set production environment |
13 | | -ENV NODE_ENV="production" |
14 | | -ARG YARN_VERSION=1.22.5 |
15 | | -RUN npm install -g yarn@$YARN_VERSION --force |
16 | | - |
17 | | - |
18 | | -# Throw-away build stage to reduce size of final image |
19 | | -FROM base AS build |
20 | | - |
21 | | -# Install packages needed to build node modules |
22 | | -RUN apt-get update -qq && \ |
23 | | - apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3 |
| 15 | +# Copy the rest of the application source code |
| 16 | +COPY . . |
24 | 17 |
|
25 | | -# Install node modules |
26 | | -COPY package-lock.json package.json yarn.lock ./ |
27 | | -RUN yarn install --frozen-lockfile |
| 18 | +# Stage 2: Create the Final Production Image |
| 19 | +# We use node:22 as the runtime image with all the necessary tools. |
| 20 | +FROM node:22 |
28 | 21 |
|
29 | | -# Copy application code |
30 | | -COPY . . |
| 22 | +# Set the working directory |
| 23 | +WORKDIR /usr/src/app |
31 | 24 |
|
| 25 | +# Copy the node_modules and built application files from the 'build' stage |
| 26 | +COPY --from=build /usr/src/app/node_modules ./node_modules |
| 27 | +COPY --from=build /usr/src/app/package*.json ./ |
| 28 | +COPY --from=build /usr/src/app . |
32 | 29 |
|
33 | | -# Final stage for app image |
34 | | -FROM base |
| 30 | +# Expose the port your app runs on |
| 31 | +ENV PORT=8080 |
| 32 | +EXPOSE $PORT |
35 | 33 |
|
36 | | -# Copy built application |
37 | | -COPY --from=build /app /app |
| 34 | +# Run the application using the non-root user (recommended for security) |
| 35 | +USER node |
38 | 36 |
|
39 | | -# Start the server by default, this can be overwritten at runtime |
40 | | -EXPOSE 3000 |
41 | | -CMD [ "yarn", "run", "start" ] |
| 37 | +# Define the command to start your application |
| 38 | +CMD [ "node", "index.js" ] |
0 commit comments