最近准备做一些test case generation方面的研究,所以研究了一下沙箱,发现字节的这款软件还可以,所以分享一下搭建过程:
bash
docker build -f ./scripts/Dockerfile.base -t code_sandbox:base .
# change the base image in Dockerfile.server
sed -i '1s/.*/FROM code_sandbox:base/' ./scripts/Dockerfile.server
docker build -f ./scripts/Dockerfile.server -t code_sandbox:server .
docker run -d --rm -p 8080:8080 code_sandbox:server make run-online
其中因为网络的原因,install-python-runtime做了如下的更改:
bash
#!/bin/bash
set -o errexit
USE_OFFICIAL_SOURCE=0
for arg in "$@"
do
if [ "$arg" = "us" ]; then
USE_OFFICIAL_SOURCE=1
fi
done
rm -f ~/.condarc
conda create -n sandbox-runtime -y python=3.11
source activate sandbox-runtime
if [ $USE_OFFICIAL_SOURCE -eq 0 ]; then
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
fi
pip install -r ./requirements.txt --ignore-requires-python
# for NaturalCodeBench python problem 29
python -c "import nltk; nltk.download('punkt')"
# for CIBench nltk problems
python -c "import nltk; nltk.download('stopwords')"
pip cache purge
conda clean --all -y
Dockerfile.base也做了一定的修改:
bash
FROM vemlp-cn-beijing.cr.volces.com/preset-images/mirror:ubuntu-focal-20231211
COPY ./scripts/tuna-apt.sh ./scripts/tuna-apt.sh
ENV DEBIAN_FRONTEND=noninteractive
RUN bash ./scripts/tuna-apt.sh 20.04 \
&& apt-get update && apt-get install -y curl npm git nano wget vim unzip sudo cgroup-tools iproute2 iptables \
# iverilog build deps
autoconf gperf flex bison \
# bash scripting utils
bc \
&& mkdir -p /workspace/download
# python 3.11 & poetry
COPY ./scripts/install-miniconda.sh ./scripts/install-miniconda.sh
RUN bash ./scripts/install-miniconda.sh 3.11
ENV PATH="/root/miniconda3/bin:${PATH}"
RUN wget https://veml.tos-cn-beijing.volces.com/condarc -O ~/.condarc && \
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
RUN curl -sSL https://install.python-poetry.org | POETRY_VERSION=1.7.0 python3 -
ENV PATH=/root/.local/bin:$PATH
# golang 1.23.3
RUN curl -o /workspace/download/go.tar.gz -SL https://go.dev/dl/go1.23.3.linux-amd64.tar.gz \
&& tar -zxf /workspace/download/go.tar.gz -C /usr/local && rm /workspace/download/go.tar.gz
ENV PATH=/bin:/usr/local/go/bin:$PATH
# nodejs 20.11.0
RUN curl -o /workspace/download/node.tar.gz -SL https://nodejs.org/dist/v20.11.0/node-v20.11.0-linux-x64.tar.gz \
&& mkdir -p /usr/local/lib/nodejs && tar -zxf /workspace/download/node.tar.gz -C /usr/local/lib/nodejs && mv /usr/local/lib/nodejs/node-v20.11.0-linux-x64 /usr/local/lib/nodejs/node \
&& rm /workspace/download/node.tar.gz
ENV PATH=/usr/local/lib/nodejs/node/bin:$PATH
ENV NODE_PATH=/usr/local/lib/node_modules
RUN npm install -g typescript@5.3.3 tsx@4.7.1
# gcc 9
RUN apt-get update && apt-get install -y build-essential g++ libboost-all-dev
RUN curl -o /workspace/download/openssl.tar.gz -SL https://www.openssl.org/source/old/3.0/openssl-3.0.11.tar.gz \
&& tar -zxf /workspace/download/openssl.tar.gz && cd openssl-3.0.11 && ./Configure && make && make install \
&& rm /workspace/download/openssl.tar.gz && cd .. && rm -r openssl-3.0.11
ENV PATH=/usr/bin/openssl:$PATH
# jdk 21
RUN curl -o /workspace/download/jdk.tar.gz -SL https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz \
&& mkdir /usr/java && tar -zxf /workspace/download/jdk.tar.gz -C /usr/java && rm /workspace/download/jdk.tar.gz \
&& java_path=`ls /usr/java/${path}` && echo "export JAVA_HOME=/usr/java/${java_path}" >> ~/.profile
# dotnet 8.0
RUN wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb \
&& dpkg -i packages-microsoft-prod.deb && rm packages-microsoft-prod.deb \
&& apt-get update \
&& apt-get install -y dotnet-sdk-8.0
# php 7.4.3
RUN apt-get install -y php-cli=2:7.4+75
# rust
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.76.0
ENV PATH="/root/.cargo/bin:${PATH}"
# R, lua, ruby, julia, perl, scala
RUN apt-get install -y r-base lua5.2 luarocks ruby-full julia scala \
&& luarocks install luaunit \
&& PERL_MM_USE_DEFAULT=1 cpan Test::Deep Data::Compare
# D
RUN wget https://master.dl.sourceforge.net/project/d-apt/files/d-apt.list -O /etc/apt/sources.list.d/d-apt.list \
&& apt-get update --allow-insecure-repositories -y \
&& apt-get -y --allow-unauthenticated install --reinstall d-apt-keyring \
&& apt-get update && apt-get install -y dmd-compiler dub
# kotlin
RUN curl -o /tmp/kotlin-compiler.zip -SL https://github.com/JetBrains/kotlin/releases/download/v2.0.0/kotlin-compiler-2.0.0.zip \
&& mkdir /usr/local/kotlin && unzip /tmp/kotlin-compiler.zip -d /usr/local/kotlin \
&& rm -f /tmp/kotlin-compiler.zip
ENV PATH=/usr/local/kotlin/kotlinc/bin:$PATH
# iverilog && verilog-eval (TODO: remove verilog-eval)
RUN cd /workspace \
&& git clone https://github.com/steveicarus/iverilog.git && cd iverilog \
&& git checkout 01441687235135d1c12eeef920f75d97995da333 \
&& sh ./autoconf.sh && ./configure && make -j4 \
&& make install \
&& cd /workspace \
&& git clone https://github.com/NVlabs/verilog-eval \
&& cd verilog-eval && git checkout 4b9b16e92f1d9cc520afbfa3ecd5a2f20a350fd5 \
&& sed -i '79d;112d' ./verilog_eval/execution.py \
&& cd ../ && pip install -e verilog-eval
# lean 4
RUN curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | sh -s -- -y --default-toolchain leanprover/lean4:v4.10.0-rc2
ENV PATH=/root/.elan/bin:$PATH
# Racket
RUN apt-get update --allow-insecure-repositories -y && apt-get install -y racket
# Swift
RUN curl -o /workspace/download/swift.tar.gz -SL https://download.swift.org/swift-5.10.1-release/ubuntu2004/swift-5.10.1-RELEASE/swift-5.10.1-RELEASE-ubuntu20.04.tar.gz \
&& cd /workspace/download \
&& tar zxf swift.tar.gz \
&& mkdir /usr/local/swift \
&& mv swift-5.10.1-RELEASE-ubuntu20.04/usr/* /usr/local/swift \
&& rm -f /workspace/download/swift.tar.gz
ENV PATH=/usr/local/swift/bin:$PATH
SHELL ["sh", "-lc"]
RUN update-alternatives --install /usr/bin/java java $JAVA_HOME/bin/java 20000 \
&& update-alternatives --install /usr/bin/javac javac $JAVA_HOME/bin/javac 20000 \
&& rm -r /workspace/download \
&& env
然后按照docker命令执行,就成功了,下面是测试脚本:
bash
curl 'http://localhost:8080/run_code' \
-H 'Content-Type: application/json' \
--data-raw '{"code": "print(\"Hello, world!\")", "language": "python"}'
参考文献
1. https://blog.csdn.net/Datawhale/article/details/144279429