Skip to content

Docker support #102

@nicolasherediaerro

Description

@nicolasherediaerro

Hi! 👋
I’ve been working on another project and thought it would be useful to add Docker support here as well. I already have a working Dockerfile setup from that project, and I think it would translate well to this one.

# -------- Base image stage --------
# Use official Node.js Alpine image for smaller footprint
FROM node:23.11.0-alpine AS base

# Configure PNPM and PATH
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"

# Enable Corepack to manage PNPM
RUN corepack enable

# Define working directory
WORKDIR /app

# -------- Install production dependencies --------
FROM base AS prod-deps

# Copy package manager files
COPY package.json pnpm-lock.yaml ./

# Install only production dependencies using cache to speed up builds
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
    pnpm install --prod --frozen-lockfile --ignore-scripts

# -------- Build stage with native dependencies --------
FROM base AS build

# Install required system packages to build native modules
RUN apk add --no-cache \
    python3 \
    py3-pip \
    build-base \
    g++ \
    make \
    python3-dev \
    gcc \
    musl-dev \
    linux-headers

# Install node-gyp globally to compile native Node.js modules
RUN npm install -g node-gyp

# Configure environment for cross-compiling node-snap7
ENV npm_config_build_from_source=true
ENV npm_config_target_arch=arm64
ENV npm_config_target_platform=linux
ENV npm_config_target_libc=musl

# Copy project dependency files
COPY package.json pnpm-lock.yaml ./

# Install all dependencies, ignoring scripts like prepare
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
    pnpm install --frozen-lockfile --ignore-scripts

# Copy source code into the container
COPY . .

# Rebuild node-snap7 with specific target platform settings
RUN cd node_modules/node-snap7 && \
    node-gyp rebuild --target_arch=arm64 --target_platform=linux --target_libc=musl

# Build your application (assumes pnpm run build compiles to /dist)
RUN pnpm run build

# -------- Final runtime image --------
FROM node:23.11.0-alpine AS runner

# Set working directory
WORKDIR /app

# Copy production-ready node_modules and build output
COPY --from=build --chown=node:node /app/node_modules ./node_modules
COPY --from=build --chown=node:node /app/dist ./dist

# Use non-root user for better security
USER node

# Expose application port
EXPOSE 8080

# Default command to run the app
CMD ["node", "dist/index.js"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions