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

Use node-alpine in docker #212

Closed
marcjulian opened this issue Mar 7, 2020 · 12 comments
Closed

Use node-alpine in docker #212

marcjulian opened this issue Mar 7, 2020 · 12 comments

Comments

@marcjulian
Copy link
Member

Use node-alpine as docker image if prisma2 provides a binaries for Alpine Linux prisma/prisma#702

@marcjulian
Copy link
Member Author

Ideas to improve docker images https://www.docker.com/blog/intro-guide-to-dockerfile-best-practices/

@Jolg42
Copy link

Jolg42 commented Jun 24, 2020

Hello @marcjulian Prisma 2.0 now provides binaries for Alpine 🎊

@marcjulian
Copy link
Member Author

@Jolg42 perfect! Thanks for letting me know 👍 will do this when I have the time!!

@leohxj
Copy link
Contributor

leohxj commented Jul 8, 2020

Hello @marcjulian Prisma 2.0 now provides binaries for Alpine 🎊

I run it with multi-stage docker builds, the builder is node:12, and run in node:12-alpine, it come with errors:

UnhandledPromiseRejectionWarning: Error: Query engine binary for current platform "linux-musl" could not be found.

does it need to build and run in same base image? could you please give a Dockerfile example

@Jolg42
Copy link

Jolg42 commented Jul 8, 2020

Hello @leohxj

Could you share your Dockerfile?

This works for me for example

FROM node:12-alpine
RUN npm install -g --unsafe-perm @prisma/cli

RUN mkdir /app
WORKDIR /app

COPY package*.json ./
COPY prisma ./prisma/
COPY src ./src/

RUN npm install
RUN prisma generate

EXPOSE 3000
CMD [ "npm", "start" ]

@leohxj
Copy link
Contributor

leohxj commented Jul 9, 2020

@Jolg42
I use mutil stage docker build. And use node:12 as Builder, then use node:12-alpine as run image. Dockerfile is:

FROM node:12 AS builder
# Install necessary tools for bcrypt to run in docker before npm install
# RUN apt-get update \
#     && apt-get install -y build-essential python
# Create app directory
WORKDIR /app
COPY ./prisma/schema.prisma ./
RUN yarn global add @prisma/cli
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json yarn.lock ./
# Install app dependencies
RUN yarn install --frozen-lockfile --verbose
COPY tsconfig*.json ./
COPY src ./src
RUN yarn prisma:generate
RUN yarn run build
# TODO use node-alpine when supported by prisma2 https://github.com/prisma/prisma2/issues/702
FROM node:12-alpine
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/dist ./dist
EXPOSE 3000
CMD [ "npm", "run", "start:prod" ]

I also change schema.prisma add below code:

datasource db {
  provider = "postgresql"
  url      = env("POSTGRESQL_URL")
  binaryTargets = ["linux-musl"]
}

but it run come with error:

To solve this problem, add the platform "linux-musl" to the "generator" block in the "schema.prisma" file:
backend     | generator client {
backend     |   provider      = "prisma-client-js"
backend     |   binaryTargets = ["native"]
backend     | }
backend     |
backend     | Then run "prisma generate" for your changes to take effect.

@leohxj
Copy link
Contributor

leohxj commented Jul 9, 2020

Prisma version is 2.1.3.

@Jolg42
Copy link

Jolg42 commented Jul 9, 2020

@leohxj Oh got it!

So what happens is during the build the binaries downloaded are the ones that are matching the current OS by default, node:12 is Debian. And when it's executed in node:12-alpine the binaries for Alpine are missing.

So what you can do is either to use the same image for both steps or to change the binaryTarget like this for Alpine

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["linux-musl"]
  // binaryTargets = ["native", "linux-musl"] // or both if you need the binaries in Debian & Alpine
}

See https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/generators#binary-targets for the documentation.

@leohxj
Copy link
Contributor

leohxj commented Jul 9, 2020

thanks @Jolg42
I am using same image now (alpine)

@marcjulian
Copy link
Member Author

@Jolg42 @leohxj thank your both for your input on how the Dockerfile for node:12-alpine should look like.

I have updated the Dockerfile and added a new Dockerfile.alpine to the project.

Dockerfile.alpine looks like:

FROM node:12-alpine AS builder

# Create app directory
WORKDIR /app

RUN npm install -g @prisma/cli --unsafe-perm

# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
COPY prisma ./prisma/

# Install app dependencies
RUN npm install
# Required if not done in postinstall
# RUN prisma generate

COPY tsconfig*.json ./
COPY src ./src
# Copy for swagger and graphql plugin
COPY nest-cli.json ./nest-cli.json

RUN npm run build

FROM node:12-alpine

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/dist ./dist

EXPOSE 3000
CMD [ "npm", "run", "start:prod" ]

@marcjulian
Copy link
Member Author

@Jolg42 is it still necessary to install the prisma cli in a separate step?

RUN npm install -g --unsafe-perm @prisma/cli

It does not take the version into account for @prisma/cli which is set in the package.json. I tried the docker build without the extra step of installing the prisma cli and it still works.

@Jolg42
Copy link

Jolg42 commented Aug 3, 2020

@marcjulian So I got a word from my colleague Tim that it was needed because by default npm’s permissions on linux are broken for global packages - global packages can’t write in their own directory during install.
However, as soon, as the package is installed, the package can write in its own folder.

@prisma/cli package needs to write in its own folder because it needs to download the engines binaries.

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

No branches or pull requests

3 participants