簡體   English   中英

使用 Bazel 構建 Node.Js Docker 鏡像

[英]Build Node.Js Docker Image with Bazel

動機

我是 Bazel 的新手,找不到足夠的資源來為 Node.Js 構建 Docker 鏡像。

所以我有一個用 Typescript 編寫的 Node.js 應用程序,它依賴於另外兩個 Typescript 包。 我的目標是構建一個 Docker 鏡像,之后可以將其部署到 Kubernetes。

我已經有一個工作的Dockerfile

FROM node:10-alpine
WORKDIR /usr/app/src
COPY package.json .
COPY yarn.lock .
COPY ./packages ./packages
COPY ./services/gateway ./services/gateway
RUN yarn install
COPY ./tsconfig.settings.json ./
COPY ./tsconfig.json ./
WORKDIR /usr/app/src/services/gateway
CMD yarn start

但我正在努力通過 Bazel 復制它。


我當前的設置

項目根目錄中的WORKSPACE.bazel文件:

workspace(name = "learning_bazel_monorepo")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

###############################
# NOEJS                       #
###############################
http_archive(
    name = "build_bazel_rules_nodejs",
    sha256 = "16fc00ab0d1e538e88f084272316c0693a2e9007d64f45529b82f6230aedb073",
    urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.42.2/rules_nodejs-0.42.2.tar.gz"],
)

# Setup the NodeJS toolchain
load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "yarn_install")
node_repositories()

# Setup Bazel managed npm dependencies with the `yarn_install` rule.
# The name of this rule should be set to `npm` so that `ts_library` and `ts_web_test_suite`
# can find your npm dependencies by default in the `@npm` workspace. You may
# also use the `npm_install` rule with a `package-lock.json` file if you prefer.
# See https://github.com/bazelbuild/rules_nodejs#dependencies for more info.
yarn_install(
  name = "npm",
  package_json = "//:package.json",
  yarn_lock = "//:yarn.lock",
)

###############################
# TYPESCRIPT                  #
###############################
http_archive(
    name = "build_bazel_rules_typescript",
    url = "https://github.com/bazelbuild/rules_typescript/archive/0.20.3.zip",
    strip_prefix = "rules_typescript-0.20.3",
)

load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")
install_bazel_dependencies()

# Set up TypeScript toolchain
load("@npm_bazel_typescript//:index.bzl", "ts_setup_workspace")
ts_setup_workspace()

###############################
# DOCKER                      #
###############################
http_archive(
    name = "io_bazel_rules_docker",
    sha256 = "14ac30773fdb393ddec90e158c9ec7ebb3f8a4fd533ec2abbfd8789ad81a284b",
    strip_prefix = "rules_docker-0.12.1",
    urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.12.1/rules_docker-v0.12.1.tar.gz"],
)

load("@io_bazel_rules_docker//repositories:repositories.bzl", container_repositories = "repositories")
container_repositories()

load("@io_bazel_rules_docker//nodejs:image.bzl", _nodejs_image_repos = "repositories")
_nodejs_image_repos()

BUILD.bazel應用程序目錄中的 BUILD.bazel:

package(default_visibility=["//visibility:public"])

load("@npm_bazel_typescript//:index.bzl", "ts_library")
ts_library(
    name = "gateway",
    srcs = [":src/index.ts"],
    module_name = "@learning-bazel-monorepo/gateway",
    deps = [
        "@npm//express"
    ],
)

load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
nodejs_image(
    name = "gateway_image",
    entry_point = "services/gateway/dist/index.js",
    node_modules = "@npm//:node_modules",
    data = [":gateway"],
)

運行bazel build //...時出現錯誤:

ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/BUILD.bazel' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:BUILD.bazel'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/dev.Dockerfile' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:dev.Dockerfile'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/dist/index.d.ts' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:dist/index.d.ts'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/dist/index.d.ts.map' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:dist/index.d.ts.map'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/dist/index.js' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:dist/index.js'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/dist/server.d.ts' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:dist/server.d.ts'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/dist/server.d.ts.map' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:dist/server.d.ts.map'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/dist/server.js' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:dist/server.js'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/node_modules/.bin/nodemon' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:node_modules/.bin/nodemon'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/node_modules/.bin/ts-node' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:node_modules/.bin/ts-node'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/node_modules/.bin/tsc' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:node_modules/.bin/tsc'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/node_modules/.bin/tsserver' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:node_modules/.bin/tsserver'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/package.json' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:package.json'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/prod.Dockerfile' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:prod.Dockerfile'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/src/index.ts' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:src/index.ts'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/src/server.ts' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:src/server.ts'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/tsconfig.json' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:tsconfig.json'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/tsconfig.tsbuildinfo' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:tsconfig.tsbuildinfo'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/@learning-bazel-monorepo/gateway/yarn-error.log' is invalid because '@npm//node_modules/@learning-bazel-monorepo/gateway' is a subpackage; perhaps you meant to put the colon here: '@npm//node_modules/@learning-bazel-monorepo/gateway:yarn-error.log'?
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/History.md' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/LICENSE' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/Readme.md' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/index.js' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/lib/application.js' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/lib/express.js' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/lib/middleware/init.js' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/lib/middleware/query.js' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/lib/request.js' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/lib/response.js' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/lib/router/index.js' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/lib/router/layer.js' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/lib/router/route.js' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/lib/utils.js' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/lib/view.js' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/.cache/bazel/_bazel_flo/0eadfacb2a20f703f8711dd83de17c79/external/npm/express/BUILD.bazel:167:1: Target '@npm//:node_modules/express/package.json' contains an error and its package is in error and referenced by '@npm//express:express__files'
ERROR: /home/flo/Desktop/learning-bazel-monorepo/services/gateway/BUILD.bazel:14:1: Target '@npm//:node_modules' contains an error and its package is in error and referenced by '//services/gateway:gateway_image.binary'
ERROR: Analysis of target '//services/gateway:gateway' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.105s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded, 0 targets configured)
    Fetching @nodejs_image_base; Restarting.

如果您想/需要查看文件結構,可以在此處執行此操作: https ://github.com/flolude/learning-bazel-monorepo。

您無法確定目標,因為您的兩個規則都被命名為“網關”。 將 nodejs_image 規則的名稱更改為“foo”,然后嘗試bazel build //...

嘗試在構建文件中使用此導入:

load("@npm_bazel_typescript//:index.bzl", "ts_library")

,取自 rules_nodejs 示例回購:

https://github.com/bazelbuild/rules_nodejs/blob/master/examples/app/BUILD.bazel

另外,請確保您的 package.json 具有以下開發依賴項。

  "devDependencies": {
    "@bazel/bazel": "latest",
    "@bazel/ibazel": "latest",
    "@bazel/buildifier": "latest",
    "@bazel/typescript": "latest",
    "typescript": "~3.4.0"
  },

此外,在您的 WORKSPACE 文件中,嘗試復制示例的 WORKSPACE 文件:

http_archive(
    name = "build_bazel_rules_nodejs",
    sha256 = "16fc00ab0d1e538e88f084272316c0693a2e9007d64f45529b82f6230aedb073",
    urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.42.2/rules_nodejs-0.42.2.tar.gz"],
)

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
    name = "npm",
    package_json = "//:package.json",
    yarn_lock = "//:yarn.lock",
)

load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")

install_bazel_dependencies()

load("@npm_bazel_typescript//:index.bzl", "ts_setup_workspace")

ts_setup_workspace()

https://github.com/bazelbuild/rules_nodejs/blob/master/examples/app/WORKSPACE

更新:

查看我構建 ts_library 和 nodejs_image 的最小回購協議: https ://github.com/mancini0/minimal_bazel_docker_nodejs

請注意,您的工作區文件的 yarn_install 規則應指向您的 package.json、yarn.lock 等...“:package.json”(如果它們位於項目的根目錄下)或“//mysubproject:package.json”/ "//mysubproject:yarn.lock" 如果他們住在 mysubproject 中。

我認為一個問題是,您正在調用yarn_install ,而bazel正在尋找npm_install 這就是它找不到的原因

因此,您要么必須改用npm_install ,要么通過將 load("@ yarn_install load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "npm_install")更改為load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "yarn_install") ) 來加載 yarn_install 規則load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "yarn_install")WORKSPACE.bazel

另一個問題可能是http_archive被加載了兩次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM