srcbookdev / srcbook

TypeScript notebook for rapid prototyping
https://srcbook.com
Apache License 2.0
1.9k stars 48 forks source link

Docker support #223

Open aliuq opened 2 weeks ago

aliuq commented 2 weeks ago

srcbook is a great project

The current api and shared build methods will cause the image size to be too large, it contains some devDependencies for start works, I don’t know if there is any reason for this design.

Some thoughts on containers, there should be two images, one web and one api. After the web image is built, use nginx/apache to run it. After the api image is built, use node to run it.

Useful library building tools,tsup and unbuild

benjreinhart commented 2 weeks ago

Hey @aliuq thanks for this. We may support docker eventually but it's not a priority at this time.

Are you interested in docker for running this locally, developing against the repo, or are you trying to host srcbook?

aliuq commented 2 weeks ago

@benjreinhart Hi, I tried a rebuild and during the process I found that the @srcbook/api package has a few packages that cannot be packaged into the esm output type, and also after the build, when accessing the portal using the separate SRCBOOK_CONFIG.api.host, there is a websocket connectivity error, so it is a bit tricky to split the web and api into two images at the moment.

On the contrary, building the container with the official npm package is much easier, the only problem is that some extra files are bundled with the container, resulting in a large image size.

I've built an image aliuq/srcbook, which is not fully tested for functionality and may be missing relevant dependencies.

Dockerfile

FROM node:22.7.0-alpine3.20 AS builder
WORKDIR /app

RUN yarn add srcbook && \
  mv node_modules/srcbook . && \
  rm node_modules yarn.lock package.json -rf && \
  mv srcbook/* . && \
  rm node_modules -rf && \
  yarn install --prod && \
  find ./node_modules -type f \( -name "*.d.ts" -o -name "*.log" -o -name "*.bak" -o -name "DS_Store" -o -name "LICENSE" -o -name "*.d.mts" -o -name "*.txt" \) -delete

FROM node:22.7.0-alpine3.20 AS runner
WORKDIR /app

COPY --from=builder /app .
CMD [ "node", "bin/cli.mjs", "start" ]

EXPOSE 2150
VOLUME /root/.srcbook
VOLUME /root/.npm

Usage

# Start
docker run -p 2150:2150 -v $PWD/srcbookData:/root/.srcbook --name srcbook aliuq/srcbook

# Stop
docker rm srcbook -f

# Exec
docker exec -it srcbook /bin/sh

It would be nice to start building from web and api!