Running Docker via PHP exec

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

Running Docker via PHP exec

问题

我需要一个比这个更安全的替代方法:sudo usermod -aG docker www-data

嘿,大家好,

我需要帮助在浏览器中使用PHP的"exec"函数运行Docker容器,容器使用"bwits/pdf2htmlex"镜像。以下是我正在使用的代码:

<php
exec("docker run --rm -v /var/www/html/ConverterPDF/html/files/pdf/:/pdf -v /var/www/html/ConverterPDF/html/files/html:/html bwits/pdf2htmlex pdf2htmlEX --zoom 3 /pdf/file.pdf --dest-dir /html 2>&1");
?>

然而,当通过PHP的"exec"函数执行此代码时,我遇到了权限问题。为了解决这个问题,我使用了以下命令:

sudo usermod -aG docker www-data

虽然这解决了问题,但似乎并不是一个安全的解决方案。我想知道是否有更安全的替代方法来处理这种情况。

英文:

I need a safer alternative than this one: sudo usermod -aG docker www-data

Hey guys,

I need help running a Docker container using the "bwits/pdf2htmlex" image through the PHP "exec" function in the browser. Here is the code I'm using:

<php
exec("docker run --rm -v /var/www/html/ConverterPDF/html/files/pdf/:/pdf -v /var/www/html/ConverterPDF/html/files/html:/html bwits/pdf2htmlex pdf2htmlEX --zoom 3 /pdf/file.pdf --dest-dir /html 2>&1");
?>

However, I ran into permission issues when executing this code through PHP's "exec" function. To get around this, I used the following command:

sudo usermod -aG docker www-data

While this resolved the issue, it doesn't appear to be a secure solution. I would like to know if there is a safer alternative to deal with this situation.

答案1

得分: 2

你不能不造成极大的安全问题而运行一个容器。如果你可以运行这个容器,那么你也可以运行以下命令并对主机系统上的任何文件进行更改:

docker run -v/:/host busybox vi /host/etc/sudoers

通常,使这更安全的方法涉及以某种方式打包应用程序,以便您可以在不使用Docker CLI或API的情况下运行它。您引用的镜像 打包了 pdf2htmlEX 工具,您也可以尝试在运行PHP服务的环境中安装该工具。您还可以构建一个包含最小HTTP服务器和该工具的镜像,并在收到HTTP POST请求时将该工具作为子进程运行。

如果您必须让自己访问Docker套接字,从而获得对整个主机的无限制的根级访问权限,那么将应用程序的用户添加到docker组是一种不错的方法。如果您的服务框架允许指定其他Unix组,那么您也可以将docker添加为一个组。

英文:

You can't docker run a container without creating an extremely large security issue. If you can docker run that container, then you can also

docker run -v/:/host busybox vi /host/etc/sudoers

and otherwise make any change to any file on the host system.

Paths to make this more secure generally involve packaging up the application in some way where you can run it without using the Docker CLI or API. The image you reference packages the pdf2htmlEX tool, and you might be able to just install that tool in the environment you're running the PHP service. You also might be able to build an image that contains a minimal HTTP server and the tool, and runs the tool as a subprocess in response to an HTTP POST request.

If you must give yourself access to the Docker socket, and thereby unrestricted root-level access to the entire host, adding the application's user to the docker group is a fine way to do it. If your service framework allows specifying additional Unix groups then you could also add docker as a group.

huangapple
  • 本文由 发表于 2023年6月29日 00:30:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575099.html
匿名

发表评论

匿名网友

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

确定