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

Dockerize Rust Server for Production #8

Closed
5-pebbles opened this issue Mar 19, 2024 · 1 comment · Fixed by #10
Closed

Dockerize Rust Server for Production #8

5-pebbles opened this issue Mar 19, 2024 · 1 comment · Fixed by #10
Assignees

Comments

@5-pebbles
Copy link
Owner

I think I can just make a two-stage Docker build.

I can compile in a rust:alpine image (alpine will ensure dependencies are statically linked) then copy the binary to a scratch image.

The only issue (yah right) might be the ssl certs but I can just copy all of /etc/ssl from the build image to the release image.
Its a few extra kilobytes but I don't think that's an issue...

@5-pebbles 5-pebbles self-assigned this Mar 19, 2024
@5-pebbles
Copy link
Owner Author

I think this is done it could use some tests but I will create a PR after #9

The branch with changes is: dockerize

5-pebbles added a commit that referenced this issue May 2, 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:
```Dockerfile
# 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.
```
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 a pull request may close this issue.

1 participant