Giter Site home page Giter Site logo

hello-rust's Introduction

Hello Rust!

This is an example app demonstrating how to deploy a Rust program on Fly.io

Structure

App

Warp-based "hello world" app, as simple as it can get.

Binding to all addresses

Noticed this line?

    warp::serve(routes)
        // ipv6 + ipv6 any addr
        .run(([0, 0, 0, 0, 0, 0, 0, 0], 8080))
        .await;

This listens to all addresses on both IPv4 and IPv6, on port 8080. It's important to do this because your app would otherwise need to know about the 172.19.0.0/16 IP it should bind to specifically. Binding to IPv6 is not required, but is likely a good idea going forward.

fly.toml

A fairly standard fly.toml configuration, except for the cmd override:

[experimental]
cmd = "./hello" # should be the name of the binary you want to run

Dockerfile

The most efficient way to create a Docker image for a Rust app is a simple Dockerfile.

Our Dockerfile is heavily commented, but here's a short rundown:

  • Copy Cargo.{toml,lock} and build dependencies
  • Copy whole project and cargo install it

.dockerignore

You definitely want to ignore /target since this can get pretty hefty, adding to the build context's size, and slow down builds significantly.

Deploy

fly launch

hello-rust's People

Contributors

aykxt avatar brleinad avatar jeromegn avatar ndarilek avatar patoconnor43 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

hello-rust's Issues

Axum server binary - Permission denied

Hi,

I'd like to deploy an Axum server app to fly.io. I'm currently having a problem with the Docker file and I'd love to get your insight, as I can't pinpoint where the issue is comming from.

In my Dockerfile:

# syntax = docker/dockerfile:1

FROM rust:latest as builder

WORKDIR /usr/src/app
COPY . .

RUN rustup target add wasm32-unknown-unknown

# Set environment variables
ENV PUBLIC_FIREBASE_PROJECT_ID=wavey-392112
ENV PUBLIC_FIREBASE_API_KEY=AIzaSyDJNzYvPmsMoh4aFAmFv88yygeWEh51JOY
ENV PUBLIC_FIREBASE_AUTH_DOMAIN=wavey-392112.firebaseapp.com
ENV PUBLIC_FIREBASE_STORAGE_BUCKET=wavey-392112.appspot.com
ENV PUBLIC_FIREBASE_MESSAGE_SENDER_ID=99407084173
ENV PUBLIC_FIREBASE_APP_ID=1:99407084173:web:dde5d073d1a90dca5a00c4

RUN --mount=type=cache,target=/usr/local/cargo,from=rust:latest,source=/usr/local/cargo \
    --mount=type=cache,target=target \
    --mount=type=secret,id=FIREBASE_ADMIN_PRIVATE_KEY \
    FIREBASE_ADMIN_PRIVATE_KEY="$(cat /run/secrets/FIREBASE_ADMIN_PRIVATE_KEY)" \
    PUBLIC_FIREBASE_PROJECT_ID=$PUBLIC_FIREBASE_PROJECT_ID \
    PUBLIC_FIREBASE_API_KEY=$PUBLIC_FIREBASE_API_KEY \
    PUBLIC_FIREBASE_AUTH_DOMAIN=$PUBLIC_FIREBASE_AUTH_DOMAIN \
    PUBLIC_FIREBASE_STORAGE_BUCKET=$PUBLIC_FIREBASE_STORAGE_BUCKET \
    PUBLIC_FIREBASE_MESSAGE_SENDER_ID=$PUBLIC_FIREBASE_MESSAGE_SENDER_ID \
    PUBLIC_FIREBASE_APP_ID=$PUBLIC_FIREBASE_APP_ID \
    cargo build --package=frontend --release --target wasm32-unknown-unknown && \
    cargo build --package=data-server --release && \
    mv ./target/release/data-server ./data-server

# Runtime image
FROM debian:bookworm-slim

# Run as "app" user
RUN useradd -ms /bin/bash app

USER app
WORKDIR /app

# Get compiled binaries from builder's cargo install directory
COPY --from=builder /usr/src/app/data-server /app/data-server

# USER root
# RUN ["chmod", "a+x", "./data-server"]

# Run the app
USER app
CMD ./data-server

When running the container, I get /bin/sh: 1: ./data-server: Permission denied and I can't figure out why I'd be lacking permissions for executing the binary. I've tried changing the USER to root and setting the file permissions with chmod but that did not do anything.

It's possible that the error is obvious but I don't use Docker often and I'm not exactly comfortable with it.

I'd appreciate any guidance. Thanks!

appears to be recompiling all dependencies on every push?

Not sure if i've done something wrong - i seem to get 3 minutes of compiling even when i've only made a trivial text change. Any ideas?

Thanks for the template, btw. This was really helpful in getting started.

=> [internal] load remote build context                                  0.0s
 => copy /context /                                                       0.1s
 => [internal] load metadata for docker.io/library/debian:bullseye-slim   2.1s
 => [internal] load metadata for docker.io/library/rust:latest            0.0s
 => [stage-1 1/4] FROM docker.io/library/debian:bullseye-slim@sha256:06a  0.0s
 => [builder 1/8] FROM docker.io/library/rust:latest                      0.0s
 => CACHED [builder 2/8] RUN USER=root cargo new app                      0.0s
 => CACHED [builder 3/8] WORKDIR /usr/src/app                             0.0s
 => CACHED [builder 4/8] COPY Cargo.toml Cargo.lock ./                    0.0s
 => CACHED [builder 5/8] RUN mkdir src && echo "fn main(){}" > src/main.  0.0s
 => CACHED [builder 6/8] RUN --mount=type=cache,target=/usr/local/cargo/  0.0s
 => [builder 7/8] COPY . .                                                0.0s
 => [builder 8/8] RUN cargo install --path .                            157.0s
 => => #    Compiling dioxus-web v0.2.1
 => => #    Compiling dioxus-router v0.2.3
 => => #    Compiling dioxus-liveview v0.1.0

Not using the official docker image

Is there a reason why this example doesn't use the official rust docker image?
I ran into issues with the Dockerfile in this example because liuchong/rustup:stable is not up to date and was getting a

error: failed to parse manifest at `Cargo.toml`

Caused by:
  feature `edition2021` is required

My cargo file had something like

[package]
edition = "2021"

And I had to change it to edition = "2018", to work with the Dockerfile in this example, even though the latest stable rust release supports edition 2021.

I can look into opening a PR for this if this is something that could be helpful for others.

Improve install performance

I was running into a situation where I felt like I was having to re-fetch my dependencies using the example dockerfile. I think this issue actually talks about a similar issue #4

It seems like the cache used for the build command should also be used for the install command. I'm specifically suggesting that this line be changed to:

RUN --mount=type=cache,target=/usr/local/cargo/registry \
    --mount=type=cache,target=/usr/src/app/target \
    cargo install --path .

Here are some examples after making a one line change in src/main.rs:

Without the change (180 seconds)

[+] Building 180.6s (18/18) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                     0.0s
 => => transferring dockerfile: 902B                                                                                                                                     0.0s
 => [internal] load .dockerignore                                                                                                                                        0.0s
 => => transferring context: 34B                                                                                                                                         0.0s
 => [internal] load metadata for docker.io/library/debian:bullseye-slim                                                                                                  0.4s
 => [internal] load metadata for docker.io/library/rust:latest                                                                                                           0.0s
 => [builder 1/8] FROM docker.io/library/rust:latest                                                                                                                     0.0s
 => [internal] load build context                                                                                                                                        0.0s
 => => transferring context: 13.50kB                                                                                                                                     0.0s
 => [stage-1 1/4] FROM docker.io/library/debian:bullseye-slim@sha256:a42bb0c298cc798f1d3a6c3ee942c54db6919373c88250255ff66aed2fdb7e41                                    0.0s
 => CACHED [builder 2/8] RUN USER=root cargo new app                                                                                                                     0.0s
 => CACHED [builder 3/8] WORKDIR /usr/src/app                                                                                                                            0.0s
 => CACHED [builder 4/8] COPY Cargo.toml Cargo.lock ./                                                                                                                   0.0s
 => CACHED [builder 5/8] RUN mkdir src && echo "fn main(){}" > src/main.rs                                                                                               0.0s
 => CACHED [builder 6/8] RUN --mount=type=cache,target=/usr/local/cargo/registry     --mount=type=cache,target=/usr/src/app/target     cargo build --release             0.0s
 => [builder 7/8] COPY . .                                                                                                                                               0.1s
 => [builder 8/8] RUN cargo install --path .                                                                                                                           179.9s
 => CACHED [stage-1 2/4] RUN useradd -ms /bin/bash app                                                                                                                   0.0s
 => CACHED [stage-1 3/4] WORKDIR /app                                                                                                                                    0.0s
 => CACHED [stage-1 4/4] COPY --from=builder /usr/local/cargo/bin/xn_5bicd /app/xn_5bicd                                                                                 0.0s
 => exporting to image                                                                                                                                                   0.0s
 => => exporting layers                                                                                                                                                  0.0s
 => => writing image sha256:c3ff171d2c58983744166774e4ef67c4311a6790406bc58225ed1710fd3a12b9                                                                             0.0

With the change (9 seconds)

[+] Building 8.3s (18/18) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                     0.0s
 => => transferring dockerfile: 1.01kB                                                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                                        0.0s
 => => transferring context: 34B                                                                                                                                         0.0s
 => [internal] load metadata for docker.io/library/debian:bullseye-slim                                                                                                  0.7s
 => [internal] load metadata for docker.io/library/rust:latest                                                                                                           0.0s
 => [builder 1/8] FROM docker.io/library/rust:latest                                                                                                                     0.0s
 => [internal] load build context                                                                                                                                        0.0s
 => => transferring context: 13.58kB                                                                                                                                     0.0s
 => [stage-1 1/4] FROM docker.io/library/debian:bullseye-slim@sha256:a42bb0c298cc798f1d3a6c3ee942c54db6919373c88250255ff66aed2fdb7e41                                    0.0s
 => CACHED [builder 2/8] RUN USER=root cargo new app                                                                                                                     0.0s
 => CACHED [builder 3/8] WORKDIR /usr/src/app                                                                                                                            0.0s
 => CACHED [builder 4/8] COPY Cargo.toml Cargo.lock ./                                                                                                                   0.0s
 => CACHED [builder 5/8] RUN mkdir src && echo "fn main(){}" > src/main.rs                                                                                               0.0s
 => CACHED [builder 6/8] RUN --mount=type=cache,target=/usr/local/cargo/registry     --mount=type=cache,target=/usr/src/app/target     cargo build --release             0.0s
 => [builder 7/8] COPY . .                                                                                                                                               0.1s
 => [builder 8/8] RUN --mount=type=cache,target=/usr/local/cargo/registry     --mount=type=cache,target=/usr/src/app/target     cargo install --path .                   7.4s
 => CACHED [stage-1 2/4] RUN useradd -ms /bin/bash app                                                                                                                   0.0s
 => CACHED [stage-1 3/4] WORKDIR /app                                                                                                                                    0.0s
 => CACHED [stage-1 4/4] COPY --from=builder /usr/local/cargo/bin/xn_5bicd /app/xn_5bicd                                                                                 0.0s
 => exporting to image                                                                                                                                                   0.0s
 => => exporting layers                                                                                                                                                  0.0s
 => => writing image sha256:cc17b67794a028d3e52e8749c06c2468c42203ec3c8aad4e233dcefea0cfc86c                                                                             0.0s

I'll admit I'm definitely not an expert on cargo or the docker buildkit functionality so maybe there's a reason that cache isn't specified with the install command. I'd thought I'd ask though. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.