Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge(dockerize): dockerize app & release profile Rocket.toml; #10

Merged
merged 2 commits into from
May 2, 2024

Conversation

5-pebbles
Copy link
Owner

@5-pebbles 5-pebbles commented Mar 21, 2024

Closes #8

Everything should now work in a docker container.

Added a release profile to the Rocket.toml so it listens on the external IP of the container to allow port forwarding.

I documented everything else in the dockerfile so here it is:

# First Stage
FROM rust:alpine as build

WORKDIR /src

# ca-certificates so we can copy /etc/ssl to the release stage
RUN apk update && apk add --no-cache \
    ca-certificates \
    build-base \
    pkgconfig \
    libressl-dev \
    musl-dev \
    perl

RUN rustup target add x86_64-unknown-linux-musl


# Build & Cache Dependencies
RUN USER=root cargo new tuna

WORKDIR /src/tuna

COPY Cargo.toml Cargo.lock ./

RUN cargo build --target x86_64-unknown-linux-musl --release


# Build the App
COPY ./src ./src

# Refinery embeds the migrations at build time
COPY ./migrations ./migrations

# Cargo checks if it needs to rebuild based on the mtime of the files...
# The copied main.rs retains the original timestamp, so the dummy looks newer -_-
RUN touch src/main.rs
# It was consensual I swear!

RUN cargo build --target x86_64-unknown-linux-musl --release


# Second Stage
FROM scratch as release

# We can use the ssl certs from the build stage
COPY --from=build /etc/ssl /etc/ssl

# Copy the binary from build stage
COPY --from=build /src/tuna/target/x86_64-unknown-linux-musl/release/tuna .

# Runtime config files
COPY ./Rocket.toml /Rocket.toml

# blast off
CMD ["/tuna"]
# Notes:
# - rusqlite has a feature bundled which automatically compiles and links SQLite.
# - you need to add volumes for the database directory or the data won't be persistent.

@5-pebbles 5-pebbles self-assigned this Mar 21, 2024
@5-pebbles 5-pebbles merged commit 98a9971 into main May 2, 2024
1 check passed
@5-pebbles 5-pebbles deleted the dockerize branch May 20, 2024 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dockerize Rust Server for Production
1 participant