你可以通过Codetainer创建基于浏览器的代码运行沙箱,可以方便地嵌入到你的Web 应用中。你可以把它当成是 codepicnic.com 的开源克隆版。
Codetainer以一个Web 服务运行,并且它还提供 API,你可以在HTML页面终端实时完成来创建、查看和附加代码到沙箱的这些操作。Codetainer后端基于 Docker以及其API来完成这一系列主要功能。
Codetainer采用Go语言开发
安装:
Docker版本1.8以上
Go版本1.4以上
godep
# set your $GOPATH go get github.com/codetainerapp/codetainer # you may get errors about not compiling due to Asset missing, it's ok. bindata.go needs to be created # by `go generate` first. cd $GOPATH/src/github.com/codetainerapp/codetainer # make install_deps # if you need the dependencies like godep make
执行完成将会创建./bin/codetainer
Docker
设置Docker监听TCP端口
DOCKER_OPTS="-H tcp://127.0.0.1:4500 -H unix:///var/run/docker.sock"
codetainer
# Docker API server and port DockerServer = "localhost" DockerPort = 4500 # Enable TLS support (optional, if you access to Docker API over HTTPS) # DockerServerUseHttps = true # Certificate directory path (optional) # e.g. if you use Docker Machine: "~/.docker/machine/certs" # DockerCertPath = "/path/to/certs" # Database path (optional, default is ~/.codetainer/codetainer.db) # DatabasePath = "/path/to/codetainer.db"
运行实例:
$ sudo docker pull ubuntu:14.04 $ codetainer image register ubuntu:14.04 $ codetainer create ubuntu:14.04 my-codetainer-name $ codetainer server # to start the API server on port 3000
在WEB APP中嵌入codetainer:
1.把 codetainer.js 复制到webapp中;
2.网页中包含codetainer.js和jquery,创建一个div标签,作为存储codetainer终端的框架:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>lsof tutorial</title> <link rel='stylesheet' href='/stylesheets/style.css' /> <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script src="/javascripts/codetainer.js"></script> <script src="/javascripts/lsof.js"></script> </head> <body> <div id="terminal" data-container="YOUR CODETAINER ID HERE"> </body> </html>
3.执行javascript脚本以从codetainer API服务加载codetainer:
$('#terminal').codetainer({ terminalOnly: false, // set to true to show only a terminal window url: "http://127.0.0.1:3000", // replace with codetainer server URL container: "YOUR CONTAINER ID HERE", width: "100%", height: "100%", });
* 参考: GitHub 编译/0xroot 内容有所删减,转载请注明来自FreeBuf黑客与极客(FreeBuf.COM)