GCP Cloud Run Job 带有自定义命令,无法在工作目录中找到脚本。

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

GCP Cloud Run Job with custom command can't find scripts in WORKDIR

问题

尝试创建两个基于相同Docker镜像的GCP Cloud Run作业,只是使用不同的命令。我可以在本地使用Docker运行作业,但当我尝试在GCP上创建Cloud Run作业时,它们无法找到Dockerfile中定义的WORKDIR中放置的脚本。例如:

# Dockerfile
WORKDIR /app
COPY script1.py .
COPY script2.py .

在我的本地机器上:

docker build -t image .
docker run image python3 script1.py
docker run image python3 script2.py

这两者都正常工作。但如果我将其部署为Cloud Run作业:

gcloud beta run jobs deploy job1 \
image=path-to-image:latest \
--region=region \
--command="/usr/bin/python3 script1.py"

会失败,显示以下错误:

terminated: Application failed to start: invalid status ::14: could not start container: 
no such file or directory

请注意,尽管在本地运行Docker容器时,python3位于路径中,但在Cloud Run上似乎需要/usr/bin/python3

我已经尝试过以下选项,但都未能在Cloud Run作业中找到脚本:

--command="/usr/bin/python3 script1.py"
--command="/usr/bin/python3 script1.py /app/script1.py"
--command="/usr/bin/python3./script1.py"

有人知道如何正确使用WORKDIR使自定义命令与Cloud Run协同工作吗?

英文:

Trying to create two GCP cloud run jobs based on the same docker image, just different commands. I can run the jobs locally using Docker, but when I try and create cloud run jobs on GCP they fail to find the scripts placed in the WORKDIR defined in the Dockerfile. E.g.

#Dockerfile
WORKDIR /app
COPY script1.py .
COPY script2.py .

On my local machine:

docker build -t image .
docker run image python3 script1.py
docker run image python3 script2.py

Both work fine. But if I deploy as cloud run jobs:

    gcloud beta run jobs deploy job1 \
    image=path-to-image:latest \
    --region=region\
    --command="/usr/bin/python3 script1.py"

Fails with:

terminated: Application failed to start: invalid status ::14: could not start container: 
no such file or directory

Note, I seem to need /usr/bin/python3 on cloud run even though python3 is on the path running a docker container locally.

I have tried:

--command="/usr/bin/python3 script1.py"
--command="/usr/bin/python3 script1.py /app/script1.py" 
--command="/usr/bin/python3./script1.py" 

All fail to find the scripts when running as a cloud run job. Anyone know how to make custom commands work correctly with WORKDIR?

答案1

得分: 1

Cloud Run 在 gcloud beta run jobs 部署命令中指定自定义命令时,会覆盖 Dockerfile 中使用的 CMD。在您的情况下,这意味着您必须显式指定脚本的路径,因为默认情况下不会运行 python3 命令。

Docker

WORKDIR /app
COPY script1.py .
COPY script2.py .
CMD ["python3", "script1.py"]

然后,在 gcloud beta run jobs deploy 命令中,您可以将脚本的路径作为参数传递:

示例:

gcloud beta run jobs deploy job1 image=path-to-image:latest --region=region --args="/app/script1.py"
英文:

Cloud Run overrides the CMD used in the Dockerfile when a custom command is specified in the gcloud beta run jobs deploy command. In your situation, this means that you must explicitly specify the path of the script because the python3 command is not run by default.

Docker

WORKDIR /app 
COPY script1.py . 
COPY script2.py . 
CMD ["python3", "script1.py"]

Then, in the gcloud beta run jobs deploy command, you can pass the path to the script as an argument:

Sample:

gcloud beta run jobs deploy job1 image=path-to-image:latest --region=region --args="/app/script1.py"

答案2

得分: 1

Christian 正确,但为了完整起见,我展示了自定义命令和作业的参数。让我感到困惑的事情(应该早就意识到)是 命令不能包含空格,也就是说,它必须确实是一个命令,而不是一个命令加参数。因此,需要分开传递命令和参数。

# Dockerfile
WORKDIR /app
COPY script1.py .
COPY script2.py .
#CMD whatever,我们将覆盖它

要在 GCP 上部署等效的作业:

gcloud beta run jobs deploy job1 \
    --image=path-to-image:latest \
    --region=region\
    --command="/usr/bin/python3"
    --args="script1.py"

gcloud beta run jobs deploy job2 \
    --image=path-to-image:latest \
    --region=region\
    --command="/usr/bin/python3"
    --args="script2.py"
英文:

Christian is correct, but for completeness I'm showing custom command and args to the job. The thing that confused me (should have realized) is that a command cannot contain spaces, i.e. it really must be a command, not a command + args. So pass the command and args separately.

# Dockerfile
WORKDIR /app 
COPY script1.py . 
COPY script2.py . 
#CMD whatever, we will override it

To deploy equivalent jobs on GCP:

gcloud beta run jobs deploy job1 \
    --image=path-to-image:latest \
    --region=region\
    --command="/usr/bin/python3"
    --args="script1.py"

gcloud beta run jobs deploy job2 \
    --image=path-to-image:latest \
    --region=region\
    --command="/usr/bin/python3"
    --args="script2.py"

huangapple
  • 本文由 发表于 2023年3月7日 01:15:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653846.html
匿名

发表评论

匿名网友

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

确定