Search code examples
bashdockersandbox

Sending a file as a parameter to a container in order to be compiled causes problems


I'm facing some problems when sending a file to a docker container in order to be compiled and executed.

In the folder that I'm running the command I have a "compiler" folder and a file.cpp(a basic hello world"). I'm using this command:

sudo docker run --rm -it -v /compiler/ dockcompiler file.cpp

And this is the output:

useradd: warning: the home directory already exists. Not copying any file from skel directory into it. chgrp: cannot access '/code': No such file or directory chmod: cannot access '/code': No such file or directory file does not exist

My Dockerfile:

FROM ubuntu

RUN apt-get update
RUN apt-get install -y gcc g++

ADD entrypoint.sh entrypoint.sh
ADD run-code.sh run-code.sh

ENTRYPOINT ["/bin/bash", "entrypoint.sh"] 

entrypoint.sh

prog=$1
uid=$2

if [ -z $1 ]; then
  echo "you must provide a file"
  exit 1
fi

if [ -z $uid ]; then
  uid=10000
fi

echo "127.0.0.1 $(hostname)" >> /etc/hosts

groupadd code
useradd -u "$uid" -G code -d "/compiler" -m codecube 
chgrp code /code
chmod 0775 /code
cd /compiler
sudo -u codecube /bin/bash /run-code.sh $prog

And the run-code.sh file(it works if I run it in terminal):

 prog=$1

if [ -z $1 ]; then
  echo "you must provide a file"
  exit 1
fi

if [ ! -f $1 ]; then
  echo "file does not exist"
  exit 1
fi

extension="${prog##*.}"
case "$extension" in
  "cpp")
    g++ $prog && ./a.out
    ;;
  "c")
    gcc $prog && ./a.out
    ;;
  "go")
    go run $prog
    ;;
  "pl")
    perl $prog
    ;;
  "py")
    python $prog
    ;;
  "rb")
    ruby $prog
    ;;
  *)
    echo "invalid language"
    exit 1
    ;;
esac

Solution

  • Updated:

    The problem is -m codecube where the it says codecube directory already exists. Also, the /code directory is not present.

    If the user directory is already in place on your docker image (assuming a modified image being used here). Then just skel files copy should do the trick and you can get away without doing useradd. A post here offers more info on this.

    #copy skel files 
    cp -r /etc/skel/. /codecube