Cauen / prisma-generator-pothos-codegen

The fastest way to create a fully customizable CRUD Graphql API from Prisma Schema.
https://www.npmjs.com/package/prisma-generator-pothos-codegen
95 stars 16 forks source link

Docker Usage #32

Closed coopbri closed 1 year ago

coopbri commented 1 year ago

The Pothos Codegen Prisma generator works great for me in a "regular" environment, however I am unable to build a Docker container with it. Here's a minimal container file that I'm working with:

FROM node:16-alpine

ARG PORT=4000

WORKDIR /app
COPY package.json .

# install dependencies
RUN yarn

COPY . .

RUN npx prisma generate --schema src/lib/prisma/schema.prisma

RUN yarn build

EXPOSE ${PORT}

CMD ["yarn", "start"]

When I build the image, I get the following error:

Environment variables loaded from src/lib/prisma/.env
Prisma schema loaded from src/lib/prisma/schema.prisma
Error: Generator at prisma-generator-pothos-codegen could not start:

/bin/sh: prisma-generator-pothos-codegen: Permission denied

I have tried changing folder permissions within the container (particularly the path of the generated output), manually creating the output folder with a mkdir container RUN command, etc. I am sure this is just a Docker permission issue, however I'm unsure how to solve it, I am using other Prisma generators that write to the container filesystem and they work. The image builds perfectly when I disable this generator plugin/remove from schema.prisma.

Has anyone been able to get a working container setup with this generator?

coopbri commented 1 year ago

I was able to work around this issue by generating the artifacts outside of the container as normal, and then COPYing them in during build. Definitely open to ideas if there is a way to make it work within the container, but this works just fine so no big deal either way.

Another potential workaround might be to disable the generator via a custom env var, for example as prisma-erd-generator does: https://github.com/keonik/prisma-erd-generator#disabled

saphewilliam commented 1 year ago

Thanks for submitting this great bug report!

Something that seems off to me is that the permissions given to the js file after build do not include execution permissions. I've also listed the permissions given to index.js of the Prisma cli for reference: image

I'll have a look if I can reproduce the bug!

saphewilliam commented 1 year ago

Update: prisma seems to have a final build step where they run chmod on the executable, this tells me that this could be our issue image

What happens if you put RUN chmod +x node_modules/prisma-generator-pothos-codegen/src/bin.js in front of the generate command? If that doesn't fix it then I'm on the wrong track I think

coopbri commented 1 year ago

Wow, nice catch and what an easy fix. Adding RUN chmod +x node_modules/prisma-generator-pothos-codegen/src/bin.js to the Dockerfile fixed it for me. Thank you!!

I could submit a PR with documentation for Docker and/or an example Dockerfile, or we could close this issue now that it's documented and solved thanks to you. What do you think?

Cauen commented 1 year ago

If the other generators don't need this step, it might be a case of just uploading an executable version of the bin I believe the issue can remain open while we test this solution :D