使用Bazel构建Node.js Docker镜像

3

动机

我是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()

在Node.js应用程序的目录中,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
2个回答

3

由于两个规则都命名为“gateway”,因此您无法确定目标。将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中,则使用"//mysubproject:package.json" / "//mysubproject:yarn.lock"。


好的,那修复了我的当前错误!但现在我又遇到了另一个错误。你能解释一下为什么 //... 有效吗?老实说这让我很困惑 :) - Florian Ludewig
1
bazel build //... 的意思是“构建所有内容”,bazel 足够智能,知道实际上需要重新构建什么。我会查看您的新错误。 - mancini0
太好了,这很有帮助。但现在它找不到Typescript了(我已经更新了我的问题)。但我不知道该如何包含它 https://github.com/bazelbuild/rules_typescript/releases - Florian Ludewig
TypeScript的问题现在已经解决了。但是我修复得越多,就会出现越多的错误。你介意克隆这个仓库并自己尝试一下吗?(https://github.com/flolude/learning-bazel-monorepo) - Florian Ludewig
我创建了一个最小的仓库,用于构建ts_library和nodejs_image,地址在这里:https://github.com/mancini0/minimal_bazel_docker_nodejs - mancini0
如果您能看一下我的最新问题,那将非常棒! - Florian Ludewig

1
我认为问题之一是您正在调用yarn_install,而bazel正在寻找npm_install。那就是它无法找到的原因。
因此,您可以改用npm_install或通过更改WORKSPACE.bazel中的load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "npm_install")load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "yarn_install")来加载yarn_install规则。
另一个问题可能是http_archive被加载了两次。

好的,我解决了yarn_install问题。但是现在我又遇到了一个新问题(我已经更新了我的问题)。 - Florian Ludewig

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接