[Python] Cloud Run installs JS Modules every call

Discussão em 'Python' iniciado por Stack, Abril 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have a cloud run function in python, and some JS files. The python code calls the JS code via processes. For some reason every time I call the cloud run function it seems to install jsdom which slows it down by 8 seconds. I tried using a docker ignore file to ignore my node_modules folder but that didnt work. My Python/JS code does not manually install it. Why does it get installed every time? Shouldn't these node modules already be installed in the image? I do have autoscale down to zero by the way.

    [​IMG]

    Docker File

    FROM python:3.11-slim

    # Install Node.js 18
    RUN apt-get update && \
    apt-get install -y curl gnupg build-essential && \
    curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
    apt-get install -y nodejs && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

    # Set environment
    ENV PYTHONDONTWRITEBYTECODE=1
    ENV PYTHONUNBUFFERED=1
    WORKDIR /app

    # Copy requirements and install
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt

    # Copy Node.js package files and install Node packages
    COPY package*.json ./
    RUN npm install

    # Copy all project files
    COPY . .

    # Set Flask environment variables
    ENV PORT 8080
    ENV GOOGLE_APPLICATION_CREDENTIALS /app/service-account.json

    # Expose port
    EXPOSE 8080

    # Start the Flask app
    CMD ["python", "main.py"]


    Requirements.txt

    google-cloud-firestore
    jsbeautifier
    javascript
    requests
    colorama
    esprima
    flask
    httpx


    package.json

    {
    "name": "firebasekeyparser",
    "version": "1.0.0",
    "description": "**To use:**",
    "main": "blob.js",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
    "@babel/generator": "^7.27.0",
    "@babel/parser": "^7.27.0",
    "@babel/traverse": "^7.27.0",
    "axios": "^1.9.0",
    "canvas": "^3.1.0",
    "jsdom": "^26.1.0"
    }
    }


    code.mjs

    const axios = require('axios');
    const jsdom = require('jsdom');
    const { JSDOM } = jsdom;
    const parser = require('@babel/parser');
    const traverse = require('@babel/traverse').default;
    const generate = require('@babel/generator').default;

    ...


    Commands to deploy

    gcloud builds submit --tag gcr.io/<project id>/firebase-key-parser

    gcloud run deploy firebase-key-parser \
    --image=gcr.io/<project id>/firebase-key-parser \
    --platform=managed \
    --region=us-central1 \
    --allow-unauthenticated

    Continue reading...

Compartilhe esta Página