docker - 通过 Ajax 使用 docker 执行沙箱命令

标签 docker sandbox interpreter

我正在寻求有关此事的帮助,如果我想对在网站中键入的命令的执行进行沙箱处理,我有哪些选择?我想为编程语言创建一个在线解释器。

我一直在研究 docker,我该如何使用它?这是最好的选择吗?

最佳答案

codecube.io做这个。它是开源的:https://github.com/hmarr/codecube

作者写下了他的rationale and process 。该系统的工作原理如下:

  • A user types some code in to a box on the website, and specifies the language the code is written in
  • They click “Run”, the code is POSTed to the server
  • The server writes the code to a temporary directory, and boots a docker container with the temporary directory mounted
  • The container runs the code in the mounted directory (how it does this varies according to the code’s language)
  • The server tails the logs of the running container, and pushes them down to the browser via server-sent events
  • The code finishes running (or is killed if it runs for too long), and the server destroys the container

Docker 容器的入口点是 entrypoint.sh ,它在容器内运行:

prog=$1
<...create user and set permissions...>
sudo -u codecube /bin/bash /run-code.sh $prog

然后run-code.sh检查扩展并运行相关的编译器或解释器:

extension="${prog##*.}"
case "$extension" in
  "c")
    gcc $prog && ./a.out
    ;;
  "go")
    go run $prog
    ;;
<...cut...>

The server that accepts the code examples from the web, and orchestrates the Docker containers was written in Go. Go turned out to be a pretty good choice for this, as much of the server relied on concurrency (tailing logs to the browser, waiting for containers to die so cleanup could happen), which Go makes joyfully simple.

作者还详细介绍了他是如何实现资源限制、隔离以及安全思想的。

关于docker - 通过 Ajax 使用 docker 执行沙箱命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28086406/

相关文章:

docker - Vespa应用程序之间的互通

node.js - 如何将 docker 环境变量传递给 npm 脚本?

ios - 使用Sandbox在iOS中测试应用内购买的问题

iphone - 管理用于缓存图像的 iPhone 应用程序沙箱 tmp 目录大小

compiler-construction - 无类型 Lambda 演算的函数式语言

lambda - 如何制作一个可以序列化包括 lambda 函数在内的所有数据的解释器?

security - 在 Docker 中,为什么建议在 Dockerfile 中运行 `apt-get` 更新?

node.js - docker-compose npm install && npm start in 入口点

macos - 苹果电脑 : sandbox creation failed: 1002 (operation couldn't be completed 13)

python - "cheap exceptions"是解释型语言的典型值吗?