|
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 |
| 1 | +# syntax = docker/dockerfile:1 |
4 | 2 |
|
5 | | -# Set the working directory inside the container |
6 | | -WORKDIR /usr/src/app |
| 3 | +# Adjust NODE_VERSION as desired |
| 4 | +ARG NODE_VERSION=22.16.0 |
| 5 | +FROM node:${NODE_VERSION}-slim AS base |
7 | 6 |
|
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 ./ |
| 7 | +LABEL fly_launch_runtime="Node.js" |
11 | 8 |
|
12 | | -# Install dependencies |
13 | | -RUN npm install |
| 9 | +# Node.js app lives here |
| 10 | +WORKDIR /app |
14 | 11 |
|
15 | | -# Copy the rest of the application source code |
16 | | -COPY . . |
| 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 |
17 | 20 |
|
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 |
| 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 |
21 | 24 |
|
22 | | -# Set the working directory |
23 | | -WORKDIR /usr/src/app |
| 25 | +# Install node modules |
| 26 | +COPY package-lock.json package.json yarn.lock ./ |
| 27 | +RUN yarn install --frozen-lockfile |
| 28 | + |
| 29 | +# Copy application code |
| 30 | +COPY . . |
24 | 31 |
|
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 . |
29 | 32 |
|
30 | | -# Expose the port your app runs on |
31 | | -ENV PORT=8080 |
32 | | -EXPOSE $PORT |
| 33 | +# Final stage for app image |
| 34 | +FROM base |
33 | 35 |
|
34 | | -# Run the application using the non-root user (recommended for security) |
35 | | -USER node |
| 36 | +# Copy built application |
| 37 | +COPY --from=build /app /app |
36 | 38 |
|
37 | | -# Define the command to start your application |
38 | | -CMD [ "node", "index.js" ] |
| 39 | +# Start the server by default, this can be overwritten at runtime |
| 40 | +EXPOSE 3000 |
| 41 | +CMD [ "yarn", "run", "start" ] |
0 commit comments