Skip to content

Commit ff3fbef

Browse files
committed
fix: Fix shell commands on alpine linux and add instructions to build runner from docker container
1 parent 5b9def9 commit ff3fbef

5 files changed

Lines changed: 85 additions & 7 deletions

File tree

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

Dockerfile

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# https://shaneutt.com/blog/rust-fast-small-docker-image-builds/
2+
3+
FROM rustlang/rust:nightly-slim as cargo-build
4+
5+
RUN apt-get update
6+
7+
RUN apt-get install musl-tools -y
8+
9+
RUN rustup target add x86_64-unknown-linux-musl
10+
11+
WORKDIR /usr/src/myapp
12+
13+
COPY Cargo.toml Cargo.toml
14+
15+
RUN mkdir src/
16+
17+
RUN echo "fn main() {println!(\"if you see this, the build broke\")}" > src/main.rs
18+
19+
RUN RUSTFLAGS=-Clinker=musl-gcc cargo build --release --target=x86_64-unknown-linux-musl
20+
21+
RUN rm -f target/x86_64-unknown-linux-musl/release/deps/myapp*
22+
23+
COPY . .
24+
25+
RUN RUSTFLAGS=-Clinker=musl-gcc cargo build --release --target=x86_64-unknown-linux-musl
26+
27+
# ------------------------------------------------------------------------------
28+
# Final Stage
29+
# ------------------------------------------------------------------------------
30+
31+
FROM mhart/alpine-node:latest
32+
33+
RUN addgroup -g 1000 myapp
34+
35+
RUN adduser -D -s /bin/sh -u 1000 -G myapp myapp
36+
37+
WORKDIR /home/myapp/bin/
38+
39+
COPY --from=cargo-build /usr/src/myapp/target/x86_64-unknown-linux-musl/release/code-runner-rust .
40+
41+
RUN chown myapp:myapp -R code-runner-rust && chmod 777 /tmp
42+
43+
USER myapp
44+
45+
CMD ["./code-runner-rust"]

build.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Build code-runner-rust
2+
3+
docker build -t code-runner-rust .
4+
docker exec -it --rm code-runner-rust:latest sh
5+
6+
// https://www.shellhacks.com/docker-cp-command-copy-file-to-from-container/
7+
8+
docker cp <container-id>:/home/myapp/bin/code-runner-rust runner-alpine
9+
docker cp f5c291c14c6b:/home/myapp/bin/code-runner-rust runner-alpine
10+
11+
echo '{ "language":"javascript", "stdin": "1|2", "command":"ls", "files": [{"name": "main.js", "content": "const fn"},{"name": "fn.js","content": "const fn"}]}' | ./code-runner-rust
12+
13+
echo '{ "language":"javascript", "stdin": "1|2", "command":"", "files": [{"name": "main.js", "content": "const fn"},{"name": "fn.js","content": "const fn"}]}' | ./code-runner-rust

build_runner.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
# cargo build --target x86_64-unknown-linux-musl --release
4+
5+
docker build -t code-runner-rust .
6+
7+
# need to create container and extract runner from it
8+
# docker exec -it --rm code-runner-rust:latest sh
9+
# docker cp <container-id>:/home/myapp/bin/code-runner-rust runner-alpine

src/executor.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::io::{Write, Error};
1+
use std::io::{Write, Error, ErrorKind};
22
use std::process::{Command, Stdio};
33
use std::result::Result;
44
use types::ExecutorResult;
@@ -13,15 +13,15 @@ pub fn run_stdin(work_dir: &str, stdin: &str, args: &[&str]) -> Result<ExecutorR
1313
.stderr(Stdio::piped());
1414

1515
let mut child = command
16-
.spawn()
17-
.expect("Failed to spawn child process");
16+
.spawn()?;
17+
//.expect("Failed to spawn child process");
1818

1919
{
20-
let stdin_stream = child.stdin.as_mut().expect("Failed to open stdin");
21-
stdin_stream.write_all(stdin.as_bytes()).expect("Failed to write to stdin");
20+
let stdin_stream = child.stdin.as_mut().ok_or(Error::new(ErrorKind::Other, "Failed to open stdin"))?;//.expect("Failed to open stdin");
21+
stdin_stream.write_all(stdin.as_bytes())?;//.expect("Failed to write to stdin");
2222
}
2323

24-
let output = child.wait_with_output().expect("Failed to read stdout");
24+
let output = child.wait_with_output()?;//.expect("Failed to read stdout");
2525

2626
let code = match output.status.code() {
2727
Some(code) => code,
@@ -40,7 +40,17 @@ pub fn run(work_dir: &str, args: &[&str]) -> Result<ExecutorResult, Error>{
4040
}
4141

4242
pub fn run_bash_stdin(work_dir: &str, command: &str, stdin: &str) -> Result<ExecutorResult, Error> {
43-
run_stdin(work_dir, stdin, &["bash", "-c", command])
43+
match run_stdin(work_dir, stdin, &["bash", "-c", command]) {
44+
Ok(executor_result) => Ok(executor_result),
45+
Err(e) => {
46+
if let ErrorKind::NotFound = e.kind() {
47+
println!("aqui");
48+
run_stdin(work_dir, stdin, &["sh", "-c", command])
49+
} else {
50+
Err(e)
51+
}
52+
}
53+
}
4454
}
4555

4656
/* pub fn run_bash(work_dir: &str, command: &str) -> Result<ExecutorResult, Error> {

0 commit comments

Comments
 (0)