Server not picking up correct memory allocation
manjitjohal
PROOP

3 months ago

I'm trying to deploy a server on my pro account but it keeps seeming to pick up that it has 20mb of memory instead of 32gb.

I've attached a copy of my dockerfile to show what it should be picking up

# --- Base Image ---

# Use a specific version of Node.js 20 LTS for reproducible builds.

FROM node:20.11.1-bullseye-slim as base

# --- Environment Variables ---

# Set essential ENV variables at the top for clarity.

# Tells Puppeteer to use the version of Chrome we install below, not download its own.

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

# Points Puppeteer to the installed Chrome executable.

ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/google-chrome-stable

# --- CRITICAL FIX 1: Correct Node.js Memory Settings ---

# REMOVED the hardcoded --max-old-space-size. This is the most important change.

# Now, Node.js will automatically detect the container's memory limit set in Railway.

# Note: Railway doesn't allow --expose-gc, so we leave NODE_OPTIONS empty or unset

# ENV NODE_OPTIONS=""

# --- CRITICAL FIX 2: Add Puppeteer flags for stability in containers ---

# These flags are essential for running Chrome headless in a Docker environment.

# They reduce memory usage and prevent crashes related to sandboxing and shared memory.

ENV PUPPETEER_ARGS="--no-sandbox --disable-setuid-sandbox --disable-dev-shm-usage --disable-accelerated-2d-canvas --disable-gpu --no-first-run --no-zygote --single-process --disable-background-networking"

# Set the working directory

WORKDIR /app

# --- System Dependencies ---

# Install Google Chrome and its dependencies cleanly.

# Using --no-install-recommends reduces the final image size.

RUN apt-get update \

&& apt-get install -y --no-install-recommends \

wget \

gnupg \

ca-certificates \

# These are the dependencies Puppeteer needs to run Chrome

libgbm-dev \

libasound2 \

libatk-bridge2.0-0 \

libcairo2 \

libcups2 \

libdbus-1-3 \

libexpat1 \

libfontconfig1 \

libgcc1 \

libgconf-2-4 \

libgdk-pixbuf2.0-0 \

libglib2.0-0 \

libgtk-3-0 \

libnspr4 \

libpango-1.0-0 \

libpangocairo-1.0-0 \

libstdc++6 \

libx11-6 \

libx11-xcb1 \

libxcb1 \

libxcomposite1 \

libxcursor1 \

libxdamage1 \

libxext6 \

libxfixes3 \

libxi6 \

libxrandr2 \

libxrender1 \

libxss1 \

libxtst6 \

lsb-release \

python3 \

&& wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \

&& echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list \

&& apt-get update \

&& apt-get install -y google-chrome-stable --no-install-recommends \

# Clean up apt caches to reduce image size

&& apt-get purge -y --auto-remove wget gnupg \

&& rm -rf /var/lib/apt/lists/*

# Create the 'python' symlink for compatibility

RUN ln -sf /usr/bin/python3 /usr/bin/python

# --- Application Build ---

# 1. The "builder" stage: Install all dependencies and build the TypeScript code.

FROM base as builder

WORKDIR /app

COPY package*.json tsconfig.json ./

RUN npm ci --no-audit --no-fund --network-timeout=300000

COPY src/ ./src

RUN npm run build

# 2. The "production" stage: Copy only what's needed to run the app.

FROM base

WORKDIR /app

# Copy package.json for runtime information

COPY package*.json ./

# Install only production dependencies

RUN npm ci --production --no-audit --no-fund --network-timeout=300000 \

&& npm cache clean --force

# Copy compiled code from the "builder" stage

COPY --from=builder /app/dist ./dist

# --- Security and Permissions ---

# Run the application as a non-root user for better security.

RUN useradd --create-home --shell /bin/bash whatsapp

# Create and set permissions for the session data directory BEFORE switching user

RUN mkdir -p /app/.wwebjs_auth && chown -R whatsapp:whatsapp /app

USER whatsapp

# --- Health & Execution ---

# Health check to allow Railway to monitor application health

HEALTHCHECK --interval=45s --timeout=10s --start-period=60s --retries=3 \

CMD node -e "require('http').get('http://localhost:' + (process.env.PORT || 3000) + '/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1); }).on('error', () => process.exit(1));"

# Expose the application port

EXPOSE 3000

# The command to start the application

CMD ["node", "dist/index.js"]

Solved$10 Bounty

7 Replies

Railway
BOT

3 months ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


brody
EMPLOYEE

3 months ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open brody 3 months ago


Hello!

Are you sure that this service needs 2GB of RAM? NodeJS doesn't have a default memory limit. If it's using 20MB, it likely only needs 20. Not a limit.


manjitjohal
PROOP

3 months ago

I am deploying a Node.js service that uses the Puppeteer library to control a headless Google Chrome browser. The purpose of this service is to automate interactions with a complex, real-time, third-party web application.

The Challenge:

The headless Chrome process is inherently resource-intensive. We are observing significant memory usage, leading to container instability and restarts, even after allocating what we believe should be sufficient resources.

What We've Done:

Our Dockerfile is optimized for this workload, including using the recommended Puppeteer launch arguments like --no-sandbox and --disable-dev-shm-usage.

We have ensured that our Node.js runtime is not configured with a hardcoded memory limit (--max-old-space-size), so it should adapt to the memory provided by the container.

here are some sample logs:

025-09-22 19:23:23 [whatsapp-mcp-server] warn: High memory usage detected: 94.06% {"version":"1.0.0"}

2025-09-22 19:23:53 [whatsapp-mcp-server] warn: High memory usage detected: 94.26% {"version":"1.0.0"}

2025-09-22 19:24:23 [whatsapp-mcp-server] info: Memory usage: 24.45MB / 25.89MB {"version":"1.0.0"}

2025-09-22 19:24:23 [whatsapp-mcp-server] warn: High memory usage detected: 94.51% {"version":"1.0.0"}

2025-09-22 19:24:53 [whatsapp-mcp-server] warn: High memory usage detected: 94.70% {"version":"1.0.0"}

2025-09-22 19:25:23 [whatsapp-mcp-server] info: Memory usage: 24.57MB / 25.89MB {"version":"1.0.0"}

2025-09-22 19:25:23 [whatsapp-mcp-server] warn: High memory usage detected: 94.95% {"version":"1.0.0"}

2025-09-22 19:25:53 [whatsapp-mcp-server] warn: High memory usage detected: 95.14% {"version":"1.0.0"}

2025-09-22 19:26:23 [whatsapp-mcp-server] info: Starting comprehensive Chrome cleanup {"version":"1.0.0","authPath":"/app/.wwebjs_auth"}

2025-09-22 19:26:23 [whatsapp-mcp-server] info: Memory usage: 24.74MB / 25.89MB {"version":"1.0.0"}

2025-09-22 19:26:23 [whatsapp-mcp-server] warn: High memory usage detected: 95.63% {"version":"1.0.0"}

2025-09-22 19:26:24 [whatsapp-mcp-server] info: Chrome cleanup completed {"version":"1.0.0","success":true,"filesRemoved":0,"processesKilled":0,"errors":0}

2025-09-22 19:26:53 [whatsapp-mcp-server] warn: High memory usage detected: 96.06% {"version":"1.0.0"}

2025-09-22 19:27:23 [whatsapp-mcp-server] info: Memory usage: 24.17MB / 25.89MB {"version":"1.0.0"}

2025-09-22 19:27:23 [whatsapp-mcp-server] warn: High memory usage detected: 93.41% {"version":"1.0.0"}

2025-09-22 19:27:53 [whatsapp-mcp-server] warn: High memory usage detected: 93.61% {"version":"1.0.0"}

2025-09-22 19:28:23 [whatsapp-mcp-server] info: Memory usage: 24.28MB / 25.89MB {"version":"1.0.0"}

2025-09-22 19:28:23 [whatsapp-mcp-server] warn: High memory usage detected: 93.86% {"version":"1.0.0"}


Have you tried setting --max-old-space-size=512? Your Dockerfile is AI generated so I've attached a cleaned up version.

FROM node:20.11.1-bullseye-slim as base
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/google-chrome-stable

ENV NODE_OPTIONS="--max-old-space-size=512"
ENV PUPPETEER_ARGS="--no-sandbox --disable-setuid-sandbox --disable-dev-shm-usage --disable-accelerated-2d-canvas --disable-gpu --no-first-run --no-zygote --single-process --disable-background-networking"

WORKDIR /app

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
wget \
gnupg \
ca-certificates \
libgbm-dev \
libasound2 \
libatk-bridge2.0-0 \
libcairo2 \
libcups2 \
libdbus-1-3 \
libexpat1 \
libfontconfig1 \
libgcc1 \
libgconf-2-4 \
libgdk-pixbuf2.0-0 \
libglib2.0-0 \
libgtk-3-0 \
libnspr4 \
libpango-1.0-0 \
libpangocairo-1.0-0 \
libstdc++6 \
libx11-6 \
libx11-xcb1 \
libxcb1 \
libxcomposite1 \
libxcursor1 \
libxdamage1 \
libxext6 \
libxfixes3 \
libxi6 \
libxrandr2 \
libxrender1 \
libxss1 \
libxtst6 \
lsb-release \
python3 \
&& wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list \
&& apt-get update \
&& apt-get install -y google-chrome-stable --no-install-recommends \
&& apt-get purge -y --auto-remove wget gnupg \
&& rm -rf /var/lib/apt/lists/*
RUN ln -sf /usr/bin/python3 /usr/bin/python

FROM base as builder
WORKDIR /app
COPY package*.json tsconfig.json ./
RUN npm ci --no-audit --no-fund --network-timeout=300000
COPY src/ ./src
RUN npm run build

FROM base
WORKDIR /app

COPY package*.json ./

RUN npm ci --production --no-audit --no-fund --network-timeout=300000 \
&& npm cache clean --force

COPY --from=builder /app/dist ./dist

RUN useradd --create-home --shell /bin/bash whatsapp

RUN mkdir -p /app/.wwebjs_auth && chown -R whatsapp:whatsapp /app
USER whatsapp

HEALTHCHECK --interval=45s --timeout=10s --start-period=60s --retries=3 \
CMD node -e "require('http').get('http://localhost:' + (process.env.PORT || 3000) + '/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1); }).on('error', () => process.exit(1));"

EXPOSE 3000

CMD ["node", "dist/index.js"]

manjitjohal
PROOP

3 months ago

Thanks, let me try that and I'll let you know my progress. Just out of curiosity what needed to be cleaned up from the AI version?


manjitjohal

Thanks, let me try that and I'll let you know my progress. Just out of curiosity what needed to be cleaned up from the AI version?

The comments are extremely distracting for me. AI also is generally not good at doing- well anything- for future reference. If you learn to write your own Dockerfile you'll gain a life skill and it will work better.


Did you end up getting this working?


Status changed to Solved noahd about 2 months ago


Loading...