1

Dockerfile

FROM chug/ubuntu14.04x64 

# Update the repository sources list
RUN echo "deb http://archive.ubuntu.com/ubuntu trusty main universe" > /etc/apt/sources.list
RUN apt-get update
# RUN apt-get upgrade

# Install all the languages/compilers we are supporting.
RUN apt-get install -y gcc
RUN apt-get install -y g++
RUN apt-get install -y php5-cli
RUN apt-get install -y ruby
RUN apt-get install -y python
RUN apt-get install -y mono-xsp2 mono-xsp2-base

RUN apt-get install -y mono-vbnc
RUN apt-get install -y npm
RUN apt-get install -y golang-go    
RUN apt-get install -y nodejs

RUN npm install -g underscore request express jade shelljs passport http sys jquery lodash async mocha moment connect validator restify ejs ws co when helmet wrench brain mustache should backbone forever debug && export NODE_PATH=/usr/local/lib/node_modules/

RUN apt-get install -y clojure1.4


# prepare for Java download
RUN apt-get install -y python-software-properties
RUN apt-get install -y software-properties-common

# grab oracle java (auto accept licence)
RUN add-apt-repository -y ppa:webupd8team/java
RUN apt-get update
RUN echo oracle-java8-installer shared/accepted-oracle-license-v1-1 
select true | /usr/bin/debconf-set-selections
RUN apt-get install -y oracle-java8-installer


RUN apt-get install -y gobjc
RUN apt-get install -y gnustep-devel &&  sed -i 's/#define 
 BASE_NATIVE_OBJC_EXCEPTIONS     1/#define BASE_NATIVE_OBJC_EXCEPTIONS     
 0/g' /usr/include/GNUstep/GNUstepBase/GSConfig.h


RUN apt-get install -y scala
RUN apt-get install -y mysql-server
RUN apt-get install -y perl

RUN apt-get install -y curl
RUN mkdir -p /opt/rust && \
curl https://sh.rustup.rs -sSf | HOME=/opt/rust sh -s -- --no-modify-
path -y && \
chmod -R 777 /opt/rust

RUN apt-get install -y sudo
RUN apt-get install -y bc

RUN echo "mysql ALL = NOPASSWD: /usr/sbin/service mysql start" | cat >> /etc/sudoers

lang.js

module.exports={
  "1":"c",
  "2":"c++",
  "3":"erlang",
  "4":"pyhton2.7",
  "5":"python3",
  "6":"nodejs"
}

方法 POST 路由 /compile

var compileController={}; 
var lang=require('./lang');

var random=require('./random')

compileController.compile=function(req,res){
  var lang_id=req.body.id
  console.log(lang_id);

  if(lang_id in  lang){
    console.log("i  ma here")
    console.log(req.body)
    let sandBoxpath='./'+lang[lang_id]+'/sandBox'
    let sandBox=require(sandBoxpath);

    var language = req.body.id;
    var code = req.body.code;
    var stdin = req.body.stdin;

    var folder= '../../temp/' + random(10); 
    var path=__dirname+"/";

    var vm_name='virtual_machine';
    var timeout_value=20;
    var sandboxType = new sandBox(timeout_value,path,folder,vm_name,code,stdin);

    sandboxType.run(function(data,exec_time,err){

        res.send({output:data, langid: language,code:code, errors:err, time:exec_time});
    });
}else{
    res.json({message:"language not supported" })
}

}

- 沙箱细节 实例化沙箱

var DockerSandbox = 
function(timeout_value,path,folder,vm_name,code,stdin_data){
    this.timeout_value=timeout_value;
    this.path=path;
    this.folder=folder;
    this.vm_name=vm_name;
    this.compiler_name="nodejs";
    this.file_name="file.js";
    this.code = code;
    this.langName="Nodejs";
    this.stdin_data=stdin_data;
}

创建文件夹并与容器共享代码

DockerSandbox.prototype.run = function(success){
   var sandbox = this;
   this.prepare( function(){
    sandbox.execute(success);
   });
}


DockerSandbox.prototype.prepare = function(success){
  var exec = require('child_process').exec;
  var fs = require('fs');
  var sandbox = this;

  exec("mkdir "+ this.path+this.folder+"&& chmod 777 "+this.path+this.folder,function(st){

     fs.writeFile(sandbox.path + sandbox.folder+"/" +sandbox.file_name, sandbox.code,function(err){

        if (err){
            console.log(err);
        }else{

            exec("chmod 777 \'"+sandbox.path+sandbox.folder+"/"+sandbox.file_name+"\'")
            fs.writeFile(sandbox.path + sandbox.folder+"/inputFile", sandbox.stdin_data,function(err){
                if (err){
                    console.log(err);
                }else{
                    success();
                } 
            });
        } 
    });

});
}

我正在尝试在 docker 中运行代码。尽管正在创建容器(可通过 docker stats 访问),但我无法在此方法中获取容器的 id **

代码

DockerSandbox.prototype.execute = function(success){
  console.log("getting printed")

  let exec = require('child_process').exec;
  exec('docker run -i --rm  -v foldepath:/code virtual_machine /bin/bash',function(error, stdout, stderr){
      console.log("Getting no data over here")

      console.log(error,stdout,stderr)
  })

}

 module.exports = DockerSandbox;
4

0 回答 0