node.js - 使用 Bazel 构建 Node.Js Docker 镜像

标签 node.js typescript docker 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 在 Node.Js 应用程序的目录中:

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中,则为“//mysubproject:package.json”/“//mysubproject:yarn.lock”。

关于node.js - 使用 Bazel 构建 Node.Js Docker 镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59119010/

相关文章:

amazon-web-services - AWS Elastic Beanstalk上的Docker中的Docker

docker 中的 django + uwsgi

node.js - 以编程方式运行 Gulp 4 任务

Marathon 上的 Docker 容器未完成

node.js - 使用nodejs在远程服务器上创建文件

javascript - 把 http ://on hrefs missing a protocol in Angular

javascript - 无法调用其类型缺少调用签名的表达式。输入 'typeof ""' 没有兼容的调用签名 React TypeScript

javascript - 从对象数组中的属性创建字符串文字联合

node.js - Mongoose:查询数据库排除包含非拉丁字符的结果

javascript - 在 fs.stat 之后获取 'undefined'