如何执行"docker run"通过已启动的VM上的云功能? [英] How can I execute "docker run" via a Cloud Function on a VM which has been started?

本文介绍了如何执行"docker run"通过已启动的VM上的云功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VM内,我有一个容器,该容器带有一个我想通过Cloud Function运行的简单Python脚本.我了解如何启动/停止VM,但无法弄清楚如何通过Cloud Function运行脚本.是否可以通过Cloud Function做到这一点?

Within a VM, I have a container with a simply Python script I'd like to run via a Cloud Function. I understand how to start/stop the VM, but I cannot figure out how to run the script via the Cloud Function. Is it possible to do this via a Cloud Function?

以下是详细信息:

这是GCP云功能,可使用Python SDK的代码启动VM.

This is the GCP Cloud Function which starts a VM, with code using the Python SDK.

from googleapiclient import discovery

service = discovery.build('compute', 'v1')

# Start the VM

# Project ID defined here
project = '' 
zone = 'us-central1-a'  
# name of the VM
instance = 'my-instance'

request = service.instances().start(project=project, zone=zone, instance=instance)
response = request.execute()

print("success!")

此VM my-instance 已与Docker容器一起部署(请参见

This VM my-instance was deployed with a Docker container (see here for details).

在VM内,我可以像往常一样使用docker容器,例如使用 docker run -it容器名称python3 myscript.py .(出于此问题的目的,这就像运行脚本以python打印"hello world"一样,如此

Within the VM, I can use the docker container as I normally would, e.g. with docker run -it container-name python3 myscript.py. (For the purpose of this question, it's exactly like running a script which prints "hello world" in python, as explained in this link)

如何简单地执行 docker run -it my-container python3 my-script.py 之类的东西?

How can I simply execute something like docker run -it my-container python3 my-script.py?

是否可以将 docker run 设置为在实例启动时执行?

Is it possible to set up docker run to be executed when the instance is launched?

推荐答案

要在VM启动时执行某些操作,可以使用启动脚本.但是,请不要将命令与 -it 一起使用,因为它是用于交互模式的;只需在启动脚本中执行docker run.

To execute something when the VM starts, you can use startup scripts. however, don't use the command with -it it's for interactive mode; simply perform a docker run in the startup script.

紧接着,您可以编写代码以在docker run命令结束时自动停止VM.

Just after, you can write a code to stop automatically the VM when the docker run command is over.

这篇关于如何执行"docker run"通过已启动的VM上的云功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆