Skip to content

Commit

Permalink
Building Rust in Docker
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyabo committed Sep 2, 2024
1 parent 52bbbfa commit 7faec16
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
22 changes: 18 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@
ARG ELIXIR_VERSION=1.15.7
ARG OTP_VERSION=26.2
ARG DEBIAN_VERSION=bullseye-20231009-slim

ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"
ARG RUST_IMAGE="rust:1.70"

# Build the Rust NIF in a separate stage
# What we did here is build the library in its own Docker builder context,
# so it runs in parallel with the rest of our Docker steps and can be cache’d easily.
# Then we told Rustler to skip compiling and to load it directly from our where we put it.
# See config/prod.exs
# See https://fly.io/phoenix-files/elixir-and-rust-is-a-good-mix/
FROM ${RUST_IMAGE} as rust_builder
WORKDIR /app
# Copy the Rust source code
COPY native/yscrdt ./
#RUN cargo build --release
RUN cargo rustc --release

FROM ${BUILDER_IMAGE} as builder

Expand All @@ -32,9 +45,6 @@ RUN apt-get install -y nodejs
# Install Yarn
RUN npm install -g yarn

# Install Rust and Cargo
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y \
&& source $HOME/.cargo/env

# Prepare build directory
WORKDIR /app
Expand Down Expand Up @@ -64,9 +74,13 @@ COPY lib lib
COPY assets assets
RUN cd assets && yarn install && cd ..


# Compile assets
RUN mix assets.deploy

#NEW STUFF
COPY --from=rust_builder /app/target/release/libyscrdt.* priv/native/

# Compile the release
RUN mix compile

Expand Down
9 changes: 9 additions & 0 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ config :logger, level: :info

# Runtime production configuration, including reading
# of environment variables, is done on config/runtime.exs.

# What we did here is build the library in its own Docker builder context,
# so it runs in parallel with the rest of our Docker steps and can be cache’d easily.
# Then we told Rustler to skip compiling and to load it directly from our where we put it.
# See https://fly.io/phoenix-files/elixir-and-rust-is-a-good-mix/
config :collaborative_drawing, YsCrdt,
crate: :yscrdt,
skip_compilation?: true,
load_from: {:collaborative_drawing, "priv/native/libyscrdt"}

0 comments on commit 7faec16

Please sign in to comment.