From 6e14da811295a4d6138495661249a54014386a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20M=2E=20Thi=C3=A9ry?= <nthiery@users.sf.net> Date: Wed, 25 Nov 2015 17:20:23 +0100 Subject: [PATCH] Ajout limitation memoire, reduction a 10s de la duree d'attente de la commande principale du conteneur, correction redirection sortie standard, et en particulier non suppression du conteneur --- .../docker-compile-and-run.cpp | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/docker-compile-and-run/docker-compile-and-run.cpp b/docker-compile-and-run/docker-compile-and-run.cpp index 2c6970c..586193d 100644 --- a/docker-compile-and-run/docker-compile-and-run.cpp +++ b/docker-compile-and-run/docker-compile-and-run.cpp @@ -21,7 +21,7 @@ string compile_and_run = "compile-and-run.sh"; string bin_dir = "/home/wims/other/bin/"; //string bin_dir = ""; string docker = "/usr/bin/docker"; -bool verbose = false; +bool verbose = true; void usage () { cerr << "docker-compile-and-run [program.cpp]" << endl; @@ -63,7 +63,7 @@ int my_system(string cmd, const vector<string> args, const string in, const stri fclose(f); } if ( out != "" ) { - FILE* f = fopen(in.c_str(), "w"); + FILE* f = fopen(out.c_str(), "w"); dup2(fileno(f), STDOUT_FILENO); fclose(f); } @@ -120,7 +120,18 @@ std::string my_popen(const string cmd, vector<string> args) { } string docker_run(string container, string cmd, vector<string> args) { - vector<string> docker_args = {"run", "-d", container, cmd}; + // See http://docs.docker.com/engine/reference/run/#runtime-constraints-on-resources + // for the resource limitations + vector<string> docker_args = {"run", + "--detach=true", // Run in background + // With 10M, the compilation and run is much slower + "--memory", "100M", "--memory-swap", "-1", + // Not available with docker 1.7? + // "--kernel-memory", "50M", + // "--cpu-quota" "50000", // Allow for using max 50% of the CPU + // Incompatible with detach + // "--rm=true", // Automatically delete container when done with the command + container, cmd}; for (unsigned int i=0; i<args.size(); i++) docker_args.push_back(args[i]); string ID = my_popen(docker, docker_args); @@ -160,11 +171,16 @@ int main(int argc, char **argv) { } string program=argv[1]; + if (verbose) + cerr << "Compiling and running: " << program << endl; string program_in_docker="prog.cpp"; - string docker_id = docker_run("crosbymichael/build-essential", "sleep", vector<string>({"1000"})); - if (verbose) + string docker_id = docker_run("crosbymichael/build-essential", "sleep", vector<string>({"10"})); + if (verbose) { cerr << "docker_id: " << docker_id << endl; + cerr << "Current directory: "; + my_system("/bin/pwd", {}); + } docker_cp(docker_id, bin_dir+compile_and_run, compile_and_run); docker_cp(docker_id, program, program_in_docker); docker_exec(docker_id, vector<string>({ "chmod", "700", compile_and_run })); -- GitLab