Brendonovich / prisma-client-rust

Type-safe database access for Rust
https://prisma.brendonovich.dev
Apache License 2.0
1.84k stars 108 forks source link

Binary exits silently in Docker container when connecting to database #198

Closed affanshahid closed 1 year ago

affanshahid commented 1 year ago

So I am trying to deploy my app inside a Docker container and have come across a strange issue. The app exits silently (without any panics or errors) when connecting to the database. My connection string is correct since I can run the app locally with it.

I tried building in release mode locally and checking with that and it still worked fine.

I enabled trace logs and got the following:

api_1             | 2022-11-04T14:36:55.157393Z TRACE query_core::executor::loader: Loading Postgres query connector...
api_1             | 2022-11-04T14:36:55.157654Z  INFO quaint::pooled: Starting a postgresql pool with 9 connections.
api_1             | 2022-11-04T14:36:55.157764Z TRACE query_core::executor::loader: Loaded Postgres query connector.
api_1             | 2022-11-04T14:36:55.170918Z DEBUG mobc: creating new connection from manager

After that it just exits. I am not sure what the issue is and whether its just a container thing.

My Dockerfile's build section in case it sheds some light:

FROM rust:1.64-alpine as builder
RUN apk upgrade --update-cache --available
RUN apk add --no-cache musl-dev
RUN apk add pkgconfig
RUN apk add libressl-dev
RUN rm -rf /var/cache/apk/*
RUN rustup target add x86_64-unknown-linux-musl
WORKDIR /usr/src
RUN USER=root cargo new app
WORKDIR /usr/src/app
COPY Cargo.toml Cargo.lock ./
RUN cargo build --target x86_64-unknown-linux-musl --release
COPY src ./src
COPY prisma ./prisma
RUN touch ./src/main.rs
RUN cargo build --target x86_64-unknown-linux-musl --release --locked
Brendonovich commented 1 year ago

This is quite bizzare, can you make a reproduction repo that triggers this? Don't really want to scaffold my own example around a single dockerfile.

affanshahid commented 1 year ago

Here is a small reproduction repo: https://github.com/affanshahid/prisma-rust-silent-exit-reproduction. I checked the error code after the exit and it seems it is a SEGFAULT (139).

Brendonovich commented 1 year ago

Not sure why but I can't for the life of me get that example running. Have had to change x86_64 to aarch64 since I'm on M1 but I'm getting the following:

app_1  | exec /bin/prisma-rust-silent-exit-reproduction: no such file or directory
prisma-rust-silent-exit-reproduction_app_1 exited with code 1

I think the COPY is working but I'm not sure why it's failing to locate the built binary.

affanshahid commented 1 year ago

I googled around a bit and found out others have reported similar issues with alpine + rust, it might have to do with the MUSL libc, there are apparently ways to fix this but I just ended up changing my builder image to rust:1.64 and my final image to gcr.io/distroless/cc. Its a bit of an increase in size but I can live with it.

If you are still interested, you could probably just do cargo run ... somewhere in the builder image and you'd get the segfault.

The issue can be closed I think since it seems it doesn't have anything to do with the lib.

Brendonovich commented 1 year ago

Hmm ok. Alpine has always been an awkward situation with Prisma, and with PCR static linking instead of dynamically like Prisma JS I'm not sure what should be done.