This repository contains images for running codes in different languages against certain testcases to effectively sandbox the execution.
- Install the latest
stable
version of Docker. - Set the path to the code to be executed in an environment variable
export PATH_TO_CODE="<path_to_code>"
- Save each testcase in a separate file, all of which are present in one directory. Make sure that only the testcases for this code are present in that directory.
- Set the path to this directory containing all testcases in an environment variable
export PATH_TO_TESTCASES="<path_to_testcases_directory>"
There are separate configurations for each supported language in the docker
directory. The instructions present in the respective README can be followed to build and run the containers.
To support other languages follow these steps:
-
Create a
Dockerfile
. Replace<image>
with iron/LANGUAGE:dev.
(Example -iron/gcc:dev
oriron/java:1.8-dev
)FROM <image> RUN apk add --no-cache bash WORKDIR /sandbox COPY docker-entrypoint.sh . ENTRYPOINT ["bash", "docker-entrypoint.sh"]
Refer iron-io for more details.
-
Now, create a
docker-entrypoint.sh
with the following content.
(Replace<extension>
with the appropriate extension for the language.)#!/bin/bash submission_directory="/sandbox/submission" testcases_directory="/sandbox/testcases" mkdir -p $submission_directory/output $submission_directory/error cd $submission_directory mv code code.<extension>
-
Once this is done write further code to compile the program
code.<extension>
and redirect output as follows:
(Redirection here handles compilation output.)<Bash command for compiling "code.<extension>"> 2>"$submission_directory/compilation_error.err" if [[ $? -ne 0 ]]; then exit 145 # Compilation error. Terminate. fi
-
Once compiled run the program and redirect the output as follows:
(Redirection here handles output and runtime errors if any.)for testcase in $(ls $testcases_directory); do <code to run the program> 0<"$testcases_directory/$testcase" \ 1>"$submission_directory/output/$testcase" \ 2>"$submission_directory/error/$testcase" done
-
Final file structure should look like this.
- /sandbox
- /docker
- /<language>
- /docker-entrypoint.sh
- /Dockerfile
- /README.md (Optional)
- /docker-entrypoint.sh
- /<language>
- /docker
- /sandbox
-
Now to build a image with this docker file run the command from sandbox folder.
docker build -t cpjudge/<language> ./docker/<language>`
Note: Here, we explicitly name the image built as
cpjudge/<language>
.
This is essential becausesandbox.go
(Sandbox service) expects the image to be named in this format.