gpt4 book ai didi

node.js - 有没有一种简单的方法可以在 Bitbucket Pipelines Docker 容器中更改为非 root 用户?

转载 作者:IT老高 更新时间:2023-10-28 21:21:01 25 4
gpt4 key购买 nike

Bitbucket Pipelines正在使用 Docker 容器执行任务,默认情况下 Docker 容器以 root 身份运行。这是 NPM 生命周期脚本的问题,因为 NPM 在运行脚本时会尝试降级其权限。

当执行 postinstall 脚本时,NPM throws an error无法在 wd %s %s (wd=%s) 中运行。最简单的解决方案是使用 --unsafe-perm 标志运行 npm install,但我不喜欢这种方法。

Docker 的 best practices用于编写 Dockerfiles 声明:

If a service can run without privileges, use USER to change to a non-root user.

在配置典型的 Docker 容器时,我会创建一个新的非 root 用户,并以该用户身份运行我的 npm 脚本。

看完Pipelines documentation我找不到任何与 Docker 的 USER 命令等效的东西。 我也许可以使用 useraddchownsu(还没有测试过),但有没有更简单的解决方案?

不幸的是,将 useraddchownsu 添加到 bitbucket-pipelines.yml 脚本部分会中断管道和结果在失败的 repo:push webhook.

image: node:6.2

pipelines:
default:
- step:
script:
- useradd --user-group --create-home --shell /bin/false node
- chown -R node: /opt/atlassian/bitbucketci/agent/build
- su -s /bin/sh -c "npm install" node
- su -s /bin/sh -c "npm run test:coverage --silent" node

管道响应

{
"code": 500,
"message": "There was an error processing your request. It has been logged (ID <removed>)."
}

最佳答案

这个问题有两点要解决。


要在 Bitbucket Pipelines 中以非 root 用户身份运行,您可以完全按照您的建议执行操作并使用 USER Docker 命令。 node:6.2 镜像不会映射到非 root 用户,因此如果您愿意,可以使用以下 Dockerfile 创建一个新的 Docker 镜像:

FROM node:6.2

USER foo

您收到的 500 错误似乎是此行的 YAML 解析问题:

- chown -R node: /opt/atlassian/bitbucketci/agent/build

“:”是 YAML 格式的特殊字符。表示键值对。为了解决这个问题,把该行的内容放在引号内,而不是像这样:

- "chown -R node: /opt/atlassian/bitbucketci/agent/build"

我还建议您现在为构建路径使用新的默认环境变量。 $BITBUCKET_CLONE_DIR。所以将行改为

- "chown -R node: $BITBUCKET_CLONE_DIR"

关于node.js - 有没有一种简单的方法可以在 Bitbucket Pipelines Docker 容器中更改为非 root 用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39077904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com