Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
Rust MUSL support (#526)
Browse files Browse the repository at this point in the history
* Rust MUSL support

Signed-off-by: Francesco Guardiani <[email protected]>

* Changed version

Signed-off-by: Francesco Guardiani <[email protected]>

* Added sccache usage to speedup builds

Signed-off-by: Francesco Guardiani <[email protected]>

* Modified path in launch.json

Signed-off-by: Francesco Guardiani <[email protected]>

* Changed APPSODY_RUN_ON_CHANGE

Signed-off-by: Francesco Guardiani <[email protected]>

* Production image uses user nobody

Signed-off-by: Francesco Guardiani <[email protected]>

Co-authored-by: Sandy Koh <[email protected]>
  • Loading branch information
slinkydeveloper and skoh7645 authored May 4, 2020
1 parent c269961 commit 23fea14
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
4 changes: 3 additions & 1 deletion experimental/rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

The Rust stack provides a consistent way of developing [Rust](https://rust-lang.org/) applications.

This stack is based on the `Rust v1.37` runtime and allows you to develop new or existing Rust applications using Appsody.
This stack is based on the `Rust v1.39` runtime and allows you to develop new or existing Rust applications using Appsody.

The Rust stack uses [Rust MUSL support](https://doc.rust-lang.org/edition-guide/rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.html) to build fully static binaries with statically linked libc, making the built images really small.

## Templates

Expand Down
16 changes: 10 additions & 6 deletions experimental/rust/image/Dockerfile-stack
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
FROM rust:1.37
FROM rust:1.39

RUN apt-get update && apt-get install -y lldb
RUN apt-get update && apt-get install -y lldb musl-tools
RUN rustup target add x86_64-unknown-linux-musl
RUN cargo install sccache

ENV CARGO_HOME=/usr/local/cargo/deps
ENV SCCACHE_DIR=/project/sccache
ENV RUSTC_WRAPPER=sccache

ENV APPSODY_MOUNTS=.:/project/user-app
ENV APPSODY_MOUNTS=.:/project/user-app;~/.cache/sccache:/project/sccache
ENV APPSODY_DEPS=$CARGO_HOME

ENV APPSODY_WATCH_DIR="/project/user-app"
ENV APPSODY_WATCH_REGEX="^(Cargo.toml|.*.rs)$"

ENV APPSODY_RUN="cargo run"
ENV APPSODY_RUN_ON_CHANGE="cargo run"
ENV APPSODY_RUN="cargo run --target x86_64-unknown-linux-musl"
ENV APPSODY_RUN_ON_CHANGE="$APPSODY_RUN"
ENV APPSODY_RUN_KILL=true

ENV APPSODY_TEST="cargo test"

ENV APPSODY_DEBUG="cargo build && lldb-server platform --listen '*:1234' --min-gdbserver-port 5000 --max-gdbserver-port 5001 --server"
ENV APPSODY_DEBUG="cargo build --target x86_64-unknown-linux-musl && lldb-server platform --listen '*:1234' --min-gdbserver-port 5000 --max-gdbserver-port 5001 --server"
ENV APPSODY_DEBUG_ON_CHANGE="$APPSODY_DEBUG"
ENV APPSODY_DEBUG_KILL=true

Expand Down
30 changes: 18 additions & 12 deletions experimental/rust/image/project/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
FROM rust:1.37 as builder
FROM rust:1.39 as builder

# Install musl
RUN apt-get update && apt-get install -y musl-tools
RUN rustup target add x86_64-unknown-linux-musl

WORKDIR "/project/user-app"

Expand All @@ -12,25 +16,27 @@ RUN cargo fetch
COPY /user-app/src ./src

# build for release
RUN cargo build --release \
&& echo "#!/bin/bash" > run.sh \
&& bin=$(find ./target/release -maxdepth 1 -perm -111 -type f| head -n 1) \
&& echo ./${bin##*/} >> run.sh \
&& chmod 755 run.sh
RUN cargo build --release --target x86_64-unknown-linux-musl \
&& cp $(find ./target/x86_64-unknown-linux-musl/release -maxdepth 1 -perm -111 -type f| head -n 1) ./target/app \
&& chmod 755 ./target/app

FROM rust:1.37-slim-stretch
# Create a "nobody" non-root user for the next image by crafting an /etc/passwd
# file that the next image can copy in. This is necessary since the next image
# is based on scratch, which doesn't have adduser, cat, echo, or even sh.
RUN echo "nobody:x:65534:65534:Nobody:/:" > /etc_passwd

RUN useradd rust
FROM scratch

WORKDIR "/project/user-app"

# get files and built binary from previous image
COPY --from=builder /project/user-app/run.sh /project/user-app/Cargo.toml /project/user-app/target/release/ ./

USER rust
COPY --from=builder /project/user-app/target/app ./
COPY --from=builder /etc_passwd /etc/passwd

ENV PORT 8000

USER nobody

EXPOSE 8000

CMD ["./run.sh"]
CMD ["./app"]
2 changes: 1 addition & 1 deletion experimental/rust/stack.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Rust
version: 0.1.6
version: 0.2.0
description: Runtime for Rust applications
license: Apache-2.0
language: rust
Expand Down
2 changes: 1 addition & 1 deletion experimental/rust/templates/simple/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"initCommands": [
"platform select remote-linux",
"platform connect connect://localhost:1234",
"platform settings -w /project/user-app/target/debug"
"platform settings -w /project/user-app/target/x86_64-unknown-linux-musl/debug"
],
"targetCreateCommands": [
"file rust-simple"
Expand Down

0 comments on commit 23fea14

Please sign in to comment.