用于 Google 云运行的 Docker M1 mac,构建挂起

问题描述 投票:0回答:1

我正在尝试将我的平均堆栈网站发布到 gcr,当我尝试构建图像时,构建过程永远不会完成(我已经离开了多个小时)。这就是我构建和部署它的方式:

docker buildx build --platform linux/amd64 -t gcr.io/project_id/:latest -f Dockerfile.frontend 。 --推

gcloud 运行部署(已删除)
--image gcr.io/project_id/:最新
--平台管理
--欧洲西部地区1
--允许未经身份验证\

如果我只是简单地执行 npm run build-prod ,它会在几秒钟内完成,因此可以构建它。

package.json

  "name": "frontend",
  "version": "0.0.1",
  "license": "",
  "scripts": {
    "build-dev": "ng build --configuration=development,localized",
    "build-prod": "ng build --configuration=production,localized --verbose"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "16.2.12",
    "@angular/cdk": "16.2.12",
    "@angular/common": "16.2.12",
    "@angular/compiler": "16.2.12",
    "@angular/core": "16.2.12",
    "@angular/forms": "16.2.12",
    "@angular/localize": "^16.2.3",
    "@angular/platform-browser": "16.2.12",
    "@angular/platform-browser-dynamic": "16.2.12",
    "@angular/router": "16.2.12",
    "@fullcalendar/core": "^6.1.8",
    "@fullcalendar/daygrid": "^6.1.8",
    "@fullcalendar/interaction": "^6.1.8",
    "@fullcalendar/timegrid": "^6.1.8",
    "@google-cloud/translate": "^8.0.1",
    "@googlemaps/js-api-loader": "^1.16.2",
    "@halftome/szamlazz-client": "^1.1.0",
    "@swc-node/register": "^1.6.7",
    "@swc/core": "^1.3.88",
    "axios": "^1.5.0",
    "chart.js": "^4.4.0",
    "file-saver": "^2.0.5",
    "html2canvas": "^1.4.1",
    "jspdf": "^2.5.1",
    "authpal-client": "^1.4.3",
    "jszip": "^3.10.1",
    "luxon": "^3.4.3",
    "primeflex": "^3.3.1",
    "primeicons": "^6.0.1",
    "primeng": "16.3.1",
    "prismjs": "^1.29.0",
    "rxjs": "~7.8.1",
    "tslib": "^2.3.0",
    "sass": "^1.69.4",
    "web-animations-js": "^2.3.2",
    "xlsx": "^0.18.5",
    "zone.js": "^0.13.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~16.2.0",
    "@angular-devkit/core": "~16.2.0",
    "@angular-devkit/schematics": "~16.2.0",
    "@angular/cli": "~16.2.0",
    "@angular/compiler-cli": "~16.2.0",
    "@angular/language-service": "~16.2.0",
    "@schematics/angular": "~16.2.0",
    "@types/google.maps": "^3.54.0",
    "@types/luxon": "^3.3.2",
    "typescript": "~5.1.3",
    "@types/file-saver": "^2.0.5",
    "@angular/localize": "^16.2.0"
  }
}

Dockerfile.frontend

# ---- Base Node ----
FROM node:alpine as base
# Set working directory
WORKDIR /usr/src/app
# Copy package.json and package-lock.json (or yarn.lock)
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy application source code
COPY . .
# Build the application
RUN npm run build-prod

# ---- Nginx ----
FROM nginx:alpine as runner
# Copy nginx configuration from base stage
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Copy built application files from base stage
COPY --from=base /usr/src/app/dist /usr/share/nginx/html/
# Expose port
EXPOSE 80
# Run the application
CMD ["nginx", "-g", "daemon off;"]

nginx.conf

map $http_accept_language $accept_language {
    ~*^hu hu;
    ~*^en en;

    default en;
}

server {
    listen 80;
    resolver 8.8.8.8;

    set $backend_name (removed);
    server_name $backend_name;

    root /usr/share/nginx/html;
    index index.html index.htm;
    client_max_body_size 32m;

    add_header Content-Security-Policy "default-src 'self'; connect-src 'self' https://maps.googleapis.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https://cdn.jsdelivr.net; font-src 'self' data:; script-src 'self' 'unsafe-inline' https://maps.googleapis.com https://editor.unlayer.com; frame-src 'self' https://editor.unlayer.com; frame-ancestors 'self'";
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
    add_header X-Content-Type-Options "nosniff" always;

    gzip on;
    gzip_static on;
    gzip_comp_level 4;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_min_length 1000;

    location = /favicon.ico {
        try_files /hu/favicon.ico /en/favicon.ico =404;
    }

    location ~* \.css$ {
        add_header Content-Type text/css;
    }

    location ~* \.png$ {
        add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
        expires off;
    }

    location ~ ^/(hu|en)/ {
        try_files $uri $uri/ /$1/index.html?$args;
    }

    location ~ /(hu|en)?/vendor.js {
        expires 7d;
        add_header Cache-Control "public, max-age=604800";
        add_header Last-Modified $date_gmt;
        add_header ETag $request_uri;
    }


    location /api {
        proxy_ssl_server_name on;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header Host $backend_name(removed);

        proxy_pass https://$backend_name(removed):443;

        proxy_read_timeout 300s;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

  
    location / {
        if ($request_uri = '/') {
            rewrite ^/$ /$accept_language/ permanent;
        }
        try_files $uri $uri/ /$accept_language/index.html?$args;
    }

    error_page 500 502 503 504 /50x.html;
    error_page 404 =200 /en/index.html;

    location = /50x.html {
        internal;
    }
}

角度.json

  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",

  "projects": {
    "projectname": {
      "projectType": "application",
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",

      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "i18n": {
        "sourceLocale": "en",
        "locales": {
          "hu": {
            "translation": "src/locale/source.hu.json",
            "baseHref": "hu/"
          }
        }
      },

      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "i18nMissingTranslation": "warning",
            "outputPath": "dist/projectname",
            "index": "src/index.html",
            "main": "src/main.ts",

            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["src/scss/_styles.scss"]
          },
          "configurations": {
            "production": {
              "aot": true,
              "buildOptimizer": true,
              "optimization": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "5mb",
                  "maximumError": "6mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "4kb",
                  "maximumError": "8kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ]
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true,
              "index": "src/index.html"
            },

            "localized": {
              "localize": ["hu", "en"]
            }
          },
          "defaultConfiguration": "production"
        },

        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",

          "configurations": {
            "production": {
              "browserTarget": "projectname:build:production"
            },
            "development": {
              "browserTarget": "projectname:build:development"
            }
          },
          "defaultConfiguration": "development"
        },

        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "projectname:build"
          }
        },

        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["src/scss/_styles.scss"]
          }
        }
      }
    }
  },

  "cli": {
    "analytics": false
  }
}

tsconfig.json

{
  "compileOnSave": false,

  "compilerOptions": {
    "target": "es2022",
    "module": "esnext",
    "moduleResolution": "node",

    "lib": ["es2020", "dom"],

    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,

    "forceConsistentCasingInFileNames": true,

    "strict": true,
    "strictNullChecks": false,

    "noImplicitAny": false,
    "noImplicitOverride": true,
    "noImplicitReturns": false,

    "noPropertyAccessFromIndexSignature": false,
    "noFallthroughCasesInSwitch": true,

    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,

    "importHelpers": true
  },

  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true,
    "strictPropertyInitialization": false
  }
}

感谢您提前回复,如果您需要任何其他信息,请联系。

docker nginx mean apple-m1 google-cloud-run
1个回答
0
投票

Mac 上的 Docker 默认仅使用 50% 的机器内存。对于某些构建,这可能还不够。内存不足错误通常表现为挂起进程。尝试增加内存: https://docs.docker.com/desktop/settings/mac/

或者,您可以使用

gcloud run deploy SERVICE --source .
命令按源直接部署到 Cloud Run。这将在本机 x64 硬件上在云中运行构建,而不必在 arm64 架构上构建 x64: https://cloud.google.com/run/docs/deploying-source-code

© www.soinside.com 2019 - 2024. All rights reserved.