Dockerfile中的变量似乎被认可了吗?

发布于 2025-02-07 16:06:02 字数 574 浏览 2 评论 0原文

我正在使用DockFile构建图像。我想通过命令行设置容器的用户名,以避免权限问题。

DockFile如下所示,我使用了user_namegroup_id的变量。但是当我构建时,问题不断出现。 错误是:groupAdd:option' - gid'需要一个参数 我猜想$ {group_id}和$ {user_name}都被识别为空字符串,但是在创建容器时,不应该分配它们的值吗? 我已经搜索了一些示例,并且根据示例,我不太了解问题在哪里?

请帮我! 谢谢!

FROM matthewfeickert/docker-python3-ubuntu:latest
ARG USER_NAME
ARG USER_ID
ARG GROUP_ID


RUN groupadd -r --gid ${GROUP_ID} ${USER_NAME} 
RUN useradd --no-log-init -r -g ${GROUP_ID} -u ${USER_ID} ${USER_NAME}

USER ${USER_NAME}
WORKDIR /usr/local/src

I am building an image using Dockfile. I would like to set the Username of the container via the command line to avoid permission issues.

The Dockfile is shown below, I used the variables of USER_NAME, GROUP_ID. But when I build, the problem keeps appearing.
The error is: groupadd: option '--gid' requires an argument
I'm guessing that both ${GROUP_ID} and ${USER_NAME} are recognized as empty strings, but shouldn't they be assigned values ​​when the container is created?
I've googled a few examples and based on the examples, I don't quite see where the problem is?

Please help me!
Thanks!

FROM matthewfeickert/docker-python3-ubuntu:latest
ARG USER_NAME
ARG USER_ID
ARG GROUP_ID


RUN groupadd -r --gid ${GROUP_ID} ${USER_NAME} 
RUN useradd --no-log-init -r -g ${GROUP_ID} -u ${USER_ID} ${USER_NAME}

USER ${USER_NAME}
WORKDIR /usr/local/src

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖上得阳光 2025-02-14 16:06:02

运行容器时,您可以使用Docker Run -U选项指定任意用户ID。

docker run -u 1003 ... my-image

这不需要图像中的任何特殊设置。在容器的/etc/passwd文件中,用户ID不存在,但是除了某些化妆品问题外,还没有任何后果,还没有交互式调试外壳的提示。

典型的用途是使您的容器访问绑定的数据目录:

docker run \
  -e DATA_DIR=/data \
  -v "$PWD/app-data:/data" \
  -u $(id -u) \
  ... \
  my-image

我通常建议将特定的用户ID传递到图像构建中。这将使用户ID“烘烤”,并且如果有人想运行图像,则必须重建图像。

设置一些非root用户通常是一个好习惯,但是只要它不是零,它的用户ID就无关紧要。反过来,通常也是一个很好的做法,最好将大部分应用程序源代码保留由根用户拥有,以便该应用程序不能意外地覆盖本身。

FROM matthewfeickert/docker-python3-ubuntu:latest

# Create an arbitrary non-root user; we don't care about its uid
# or other properties
RUN useradd --system user

# Still as root, do the normal steps to install and build the application
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY ./ ./

# Still as root, make sure the data directory exists
ENV DATA_DIR=/data
RUN mkdir "$DATA_DIR" && chown user "$DATA_DIR"
# VOLUME ["/data"]

# Normal metadata to run the container, only switching users now
EXPOSE 5000
USER user
CMD ["./app.py"]

此设置仍将与最初显示的扩展Docker Run命令一起使用:Docker Run -V选项将导致容器的/data目录在主机上的数字UID所有者上(希望)匹配Docker Run -U uid。

When you run the container, you can specify an arbitrary user ID with the docker run -u option.

docker run -u 1003 ... my-image

This doesn't require any special setup in the image. The user ID won't exist in the container's /etc/passwd file but there aren't really any consequences to this, beyond some cosmetic issues with prompts in interactive debugging shells.

A typical use of this is to give your container access to a bind-mounted data directory:

docker run \
  -e DATA_DIR=/data \
  -v "$PWD/app-data:/data" \
  -u $(id -u) \
  ... \
  my-image

I'd generally recommend not passing a specific user ID into your image build. This would make the user ID "baked in", and if someone with a different host uid wanted to run the image, they'd have to rebuild it.

It's often a good practice to set up some non-root user, but it doesn't matter what its user ID is so long as it's not zero. In turn, it's also typically a good practice to leave most of your application source code owned by the root user so that the application can't accidentally overwrite itself.

FROM matthewfeickert/docker-python3-ubuntu:latest

# Create an arbitrary non-root user; we don't care about its uid
# or other properties
RUN useradd --system user

# Still as root, do the normal steps to install and build the application
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY ./ ./

# Still as root, make sure the data directory exists
ENV DATA_DIR=/data
RUN mkdir "$DATA_DIR" && chown user "$DATA_DIR"
# VOLUME ["/data"]

# Normal metadata to run the container, only switching users now
EXPOSE 5000
USER user
CMD ["./app.py"]

This setup will still work with the extended docker run command shown initially: the docker run -v option will cause the container's /data directory to take on its numeric uid owner from the host, which (hopefully) matches the docker run -u uid.

痞味浪人 2025-02-14 16:06:02

您可以通过如下所示传递构建ARG。

docker build --build-arg USER_NAME=test --build-arg USER_ID=805 --build-arg GROUP_ID=805 -t tag1 .

另外,作为最佳实践,请考虑在ARGS中添加默认阀。因此,如果用户未指定ARG,则将选择默认值。

You can pass the build args as shown below.

docker build --build-arg USER_NAME=test --build-arg USER_ID=805 --build-arg GROUP_ID=805 -t tag1 .

Also, as a best practice consider adding default vales to the args. So if the user doesn't specify the args the default values will be picked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文