I'm trying to create a server that compiles and runs code submitted by the user. How can I run the executable output (compiled from code, unreliable) on the server-side, without affecting my codebase (binary executable itself)?
Is docker somehow going to be useful here? If "yes", how?
FYI, I'm using a microservices architecture. And Go for the server-side development of this service (code-runner).
The best way to run an unreliable code is isolating it, we do it a lot when reverse engineering malware. Since you are using Docker there are some precautions you would need to take that are not needed when using virtual machines.
The safest way to do it would be having another Docker container only for compiling, executing and performing any other operation needed.
Let's take the example of an application which the user sends the code and we run it for him comparing it with the expected output, as in programming contests like Codeforces.
diff output expected_output
This way all the stuff related to the code sent by the user is isolated so it is safer.
Some notes:
[What is the] Best way to run an untrusted executable file from an user on a server?
Not at all.
Sandboxing is hard. Try using the Playground which does exactly what you need.
Or: Reimplement the playground according to your needs. https://blog.golang.org/playground Might help you get started.
Just remember to disallow dangerous packages like unsafe, runtime, os/exec and everything from net.