Docker / 终端命令在Jupyter Notebook单元格中

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

Docker / terminal command in a juypter notebook cell

问题

我正在尝试在Juypter Notebook中运行这个Docker函数:

subprocess.run(["docker", "run", "--rm", "-t", "--name", "stream", "-v", "/home/benson/OCR_project:/user-data/", "--user", "id -u:`id -g`", "-e", f"LICENSE_KEY={license_key}", "-e", f"TOKEN={token}", "platerecognizer/alpr-stream"])

这是来自此网站的:https://platerecognizer.com/

我尝试在while循环中执行此操作,但是当我尝试运行子进程时,即使只是在while循环之外的Juypter笔记本单元格中检查其工作情况,我都会收到以下错误:

CompletedProcess(args=['docker', 'run', '--rm', '-t', '--name', 'stream', '-v', '/home/benson/OCR_project:/user-data/', '--user', 'id -u:`id -g`', '-e', 'LICENSE_KEY=sB6WCDvshT', '-e', 'TOKEN=80cc7ff865e421cde42a2df9ae165f5f5482b5a5', 'platerecognizer/alpr-stream'], returncode=125)

125表示错误,什么都不运行,但我正在寻求一些建议。我不太熟悉在Juypter中运行终端命令和while循环。

英文:

I am trying to run this docker function in Juypter Notebook:

subprocess.run(["docker", "run", "--rm", "-t", "--name", "stream", "-v", "/home/benson/OCR_project:/user-data/", "--user", "id -u:`id -g`", "-e", f"LICENSE_KEY={license_key}", "-e", f"TOKEN={token}", "platerecognizer/alpr-stream"])

Which is from here: https://platerecognizer.com/

I am trying to do this in a while loop, but when I try and run the subprocess, even just in a Juypter notebook cell outside of the while loop to check its working, I get this error:​

CompletedProcess(args=['docker', 'run', '--rm', '-t', '--name', 'stream', '-v', '/home/benson/OCR_project:/user-data/', '--user', 'id -u:`id -g`', '-e', 'LICENSE_KEY=sB6WCDvshT', '-e', 'TOKEN=80cc7ff865e421cde42a2df9ae165f5f5482b5a5', 'platerecognizer/alpr-stream'], returncode=125)

125 means error, and nothing runs, but I was looking for some guidance. I'm not very fluent with running terminal commands in Juypter and a while loop.

答案1

得分: 2

gid = os.getegid()
uid = os.geteuid()

subprocess.run(["docker", "run", "--rm", "-t", "--name", "stream", "-v", "/home/benson/OCR_project:/user-data/", "--user", f"{uid}:{gid}", "-e", f"LICENSE_KEY={license_key}", "-e", f"TOKEN={token}", "platerecognizer/alpr-stream"])
英文:

Your problem is with:

"--user", "id -u:`id -g`"

This looks like an attempt to retrieve your UID and GID using the commands id -u and id -g. The id -g is enclosed in back ticks, which are used to tell the shell to execute the enclosed command and then replace the command with the command's output. For this to work, the id -u would also need to be enclosed in back ticks. However you are not executing the command from a shell. You can use two more python commands to get the UID and GID and then pass them to the subprocess.run(...) command:

import os
import subprocess

gid = os.getegid()
uid = os.geteuid()

subprocess.run(["docker", "run", "--rm", "-t", "--name", "stream", "-v", "/home/benson/OCR_project:/user-data/", "--user", f"{uid}:{gid}", "-e", f"LICENSE_KEY={license_key}", "-e", f"TOKEN={token}", "platerecognizer/alpr-stream"])

huangapple
  • 本文由 发表于 2023年3月12日 07:22:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710185.html
匿名

发表评论

匿名网友

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

确定