如何在构建云构建 Docker 镜像时传递环境变量和参数?

huangapple go评论63阅读模式
英文:

How to pass env variables and arguments while building cloud build docker images?

问题

我正在尝试学习Cloudbuild,并从一个简单的项目开始。在这个项目中,当我提交到GitHub的分支时,会构建一个Docker容器,该容器会复制一个Bash脚本文件并打印其内容。

我想在构建容器时传递变量,以便可以在Docker容器和脚本中访问它们。

以下是我的文件:

Dockerfile:

FROM ubuntu:latest
ARG _PID
ENV PJID=$_PID
COPY script.sh /
RUN echo "The ENV variable value is $PJID"
RUN chmod +x /script.sh
ENTRYPOINT ["/script.sh"]

script.sh

#!/bin/bash
echo "Hello, world! The new1  time is $(date)."
echo "$(PROJECT_ID)"

cloudbuild.yaml

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/quickstart-docker-repo/test:tag1','--build-arg=ENV=$_PID', '.' ]
images:
- 'gcr.io/$PROJECT_ID/quickstart-docker-repo/test:tag1'
# [END cloudbuild_quickstart_build]
substitutions:
  _PID: $PROJECT_ID
options:
  logging: CLOUD_LOGGING_ONLY

但是,当我拉取并运行我的Cloud Shell中的这个镜像时,它显示如下输出。

Hello, world! The new1  time is Mon Jul 17 11:39:31 UTC 2023.
/script.sh: line 3: PROJECT_ID: command not found

我想要打印出 RUN echo "The ENV variable value is $PJID" 的输出,它在我的容器中,以及 echo "$(PROJECT_ID)" 的输出,它在我的脚本文件中。

有人可以告诉我这里有什么问题吗?

英文:

I am trying to learn cloudbuild and starting with a simple project where,when I commit to a branch of a github, a docker container is build,which copies a bash script file and print its content.

I want to pass variables while building the container,so that it can be accessed in docker container,as well as the scripts.

Here are my files:

Dockerfile:

FROM ubuntu:latest
ARG _PID
ENV PJID=$_PID
COPY script.sh /
RUN echo "The ENV variable value is $PJID"
RUN chmod +x /script.sh
ENTRYPOINT ["/script.sh"]

script.sh

#!/bin/bash
echo "Hello, world! The new1  time is $(date)."
echo "$(PROJECT_ID)"

cloudbuild.yaml

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/quickstart-docker-repo/test:tag1','--build-arg=ENV=$_PID', '.' ]
images:
- 'gcr.io/$PROJECT_ID/quickstart-docker-repo/test:tag1'
# [END cloudbuild_quickstart_build]
substitutions:
  _PID: $PROJECT_ID
options:
  logging: CLOUD_LOGGING_ONLY

But,when I pull and run this image of my cloudshell,it show below output.

Hello, world! The new1  time is Mon Jul 17 11:39:31 UTC 2023.
/script.sh: line 3: PROJECT_ID: command not found

I want to print the output of RUN echo "The ENV variable value is $PJID" which is in my container as well as output of echo "$(PROJECT_ID)" which is in my script file.

can someone tell me what's wrong here?

答案1

得分: 2

你走错了方向!有很多问题需要解决。

首先,你在docker构建命令中的参数'--build-arg=ENV=$_PID'。你的docker文件中没有名为ENV的参数,所以它无法工作。

其次,docker文件中的这一行RUN echo "The ENV variable value is $PJID"只在构建时评估,而不是在运行时。在容器创建后,你将永远看不到这一行。

然后,在你的脚本中这一行echo "$(PROJECT_ID)"看起来很奇怪。$()语法用于评估(就像一个命令)。如果你想打印环境变量的值,请使用大括号${}

最后,在这一行echo "$(PROJECT_ID)"中,项目ID不在环境中。你可以在运行时设置它(这一次与仅在构建时评估的构建参数不同)。为此,你可以使用这种类型的docker命令运行你的容器:docker run -e PROJECTID=GreatProject ...

英文:

You are not on the right way! Many things to fix.

Firstly, your parameter in the docker build command '--build-arg=ENV=$_PID'. There is no arg in your dockerfile named ENV, so, it can't work.

Secondly, this dockerfile line RUN echo "The ENV variable value is $PJID" is only evaluated at Build time, not at runtime. You will never see this line after the container creation.

Then, this line echo "$(PROJECT_ID)" in your script is weird. $() syntax if for evaluating (like a command). If you want to print the env var value, use the bracket ${}.

Finally, still on this line echo "$(PROJECT_ID)", the project ID is not in the environment. You can set it at runtime (this time, the difference with Build arg that are evaluated at build time only). For that, you can run your container with this type of docker command docker run -e PROJECTID=GreatProject ...

huangapple
  • 本文由 发表于 2023年7月17日 19:49:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定