Do I need to add node_modules to .gcloudignore when running "gcloud builds submit ./project-folder"?

This is my project structure:

project/
  node_modules/
  src/
  .gcloudignore
  cloudbuild.yaml
  Dockerfile
  package.json

Here is how I’m building it:

gcloud builds submit ./project --config=./project/cloudbuild.yaml --project=$PROJECT_ID   // AND SOME SUBSTITUTIONS

This is my cloudbuild.yaml file:

steps:
  # BUILD IMAGE
  - name: "gcr.io/cloud-builders/docker"
    args:
      - "build"
      - "--tag"
      - "gcr.io/$PROJECT_ID/$_SERVICE_NAME:$_TAG_NAME"
      - "."
    timeout: 180s

    # PUSH IMAGE TO REGISTRY
  - name: "gcr.io/cloud-builders/docker"
    args:
      - "push"
      - "gcr.io/$PROJECT_ID/$_SERVICE_NAME:$_TAG_NAME"
    timeout: 180s

    # DEPLOY CONTAINER WITH GCLOUD
  - name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
    entrypoint: gcloud
    args:
      - "run"
      - "deploy"
      - "$_SERVICE_NAME"
      - "--image=gcr.io/$PROJECT_ID/$_SERVICE_NAME:$_TAG_NAME"
      - "--platform=managed"
      - "--region=$_REGION"
      - "--port=8080"
      - "--allow-unauthenticated"
    timeout: 180s

# DOCKER IMAGES TO BE PUSHED TO CONTAINER REGISTRY
images: 
  - "gcr.io/$PROJECT_ID/$_SERVICE_NAME:$_TAG_NAME"

And here is my Dockerfile:

FROM node:12-slim

WORKDIR /

COPY ./package.json ./package.json
COPY ./package-lock.json ./package-lock.json

COPY ./src ./src

RUN npm ci

From my configuration file, since nothing is being told to copy the node_modules folder, it seems unnecessary to add node_modules to .gcloudignore. But is it?

I’m asking this because I saw this answer that said:

When you run gcloud builds submit… you provide some source code and either a Dockerfile or a configuration file. The former is a simple case of the second, a configuration file containing a single step that runs docker build….

Configuration files (YAML) list a series of container images with parameters that are run in series. Initially Cloud Build copies a designated source (can be the current directory) to a Compute Engine VM (created by the service) as a directory (that’s automatically mounted into each container) as /workspace.

If it copies the source, will it copy node_modules as well? Should I add it to .gcloudignore or is it not necessary?

Source: Docker Questions

LEAVE A COMMENT