Skip to content

Commit

Permalink
Packaging v0: Build and run server in a docker container (#139)
Browse files Browse the repository at this point in the history
* This is a nightmare

* Checkpointing.... getting closer

* First cut - packaging the server in a docker container

* Remove busted bundles

* Minify build

* Some common commands for building - we're going to look into proper monorepo solutions soon

* Remove dependency on serve-static

* Add web serving, full-stack docker target, and Nvidia container support

* Remove test action graph for now
  • Loading branch information
chrisbenincasa authored Mar 5, 2024
1 parent 1eb7091 commit 5570631
Show file tree
Hide file tree
Showing 157 changed files with 1,591 additions and 536 deletions.
9 changes: 6 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
node_modules
npm-debug.log
Dockerfile
*Dockerfile
.dockerignore
.git
.gitignore
bin
dist
*/*/dist*
*/*/build*

.pseudotv
.dizquetv
.dizquetv
.tunarr
69 changes: 69 additions & 0 deletions build.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
FROM node:20-alpine3.19 AS base

# Update
RUN apk add --no-cache libc6-compat
RUN apk update

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"

RUN corepack enable

FROM base as sources
WORKDIR /tunarr
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
COPY server/ ./server
COPY shared/ ./shared
COPY types ./types
COPY web2 ./web2

FROM sources AS prod-deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile

### Begin server build ###
FROM sources AS build-server
# Install deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
# Build common modules
RUN pnpm run --filter=types --filter=shared build
# Runs tsc --noEmit on the server to ensure the code builds
RUN pnpm run --filter=server typecheck
# Build ORM metadata cache using source files
RUN cd server && pnpm mikro-orm-esm cache:generate --combined --ts
# Replace the non-cached metadata config with the cache
RUN mv server/mikro-orm.prod.config.ts server/mikro-orm.config.ts
# Build and bundle the server
RUN pnpm run --filter=server bundle
### End server build ###

### Begin server web ###
FROM sources AS build-web
# Install deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
# Build common modules
RUN pnpm run --filter=types --filter=shared build
RUN pnpm run --filter=web build

### Experimental: Build a SEA
FROM build-server AS build-exec
COPY --from=build-server /tunarr/server/node_modules /tunarr/server/node_modules
COPY --from=build-server /tunarr/server/build /tunarr/server/build
RUN pnpm run --filter=server make-exec
###

### Begin server run ###
FROM base AS server
COPY --from=prod-deps /tunarr/node_modules /tunarr/node_modules
COPY --from=prod-deps /tunarr/server/node_modules /tunarr/server/node_modules
COPY --from=build-server /tunarr/types /tunarr/types
COPY --from=build-server /tunarr/shared /tunarr/shared
COPY --from=build-server /tunarr/server/package.json /tunarr/server/package.json
COPY --from=build-server /tunarr/server/build /tunarr/server/build
ENV TUNARR_BIND_ADDR=0.0.0.0
EXPOSE 8000
CMD [ "/tunarr/server/build/bundle.js" ]
### Begin server run

### Full stack ###
FROM server AS full-stack
COPY --from=build-web /tunarr/web2/dist /tunarr/server/build/web
77 changes: 77 additions & 0 deletions nvidia.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Setup a node + ffmpeg + nvidia base
FROM jrottenberg/ffmpeg:4.4.4-nvidia2204 AS ffmpeg-base
ENV NODE_MAJOR=20

# Install musl for native node bindings (sqlite)
RUN apt-get install -y musl-dev
RUN ln -s /usr/lib/x86_64-linux-musl/libc.so /lib/libc.musl-x86_64.so.1

# Install node
RUN <<EOF
apt-get update && apt-get install -y ca-certificates curl gnupg
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
apt-get update && apt-get install nodejs -y
EOF

# Install pnpm
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

EXPOSE 8000
RUN ln -s /usr/local/bin/ffmpeg /usr/bin/ffmpeg
ENTRYPOINT [ "node" ]

# Add Tunarr sources
FROM ffmpeg-base as sources
WORKDIR /tunarr
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
COPY server/ ./server
COPY shared/ ./shared
COPY types ./types
COPY web2 ./web2

FROM sources AS prod-deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile

### Begin server build ###
FROM sources AS build-server
# Install deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
# Build common modules
RUN pnpm run --filter=types --filter=shared build
# Runs tsc --noEmit on the server to ensure the code builds
RUN pnpm run --filter=server typecheck
# Build ORM metadata cache using source files
RUN cd server && pnpm mikro-orm-esm cache:generate --combined --ts
# Replace the non-cached metadata config with the cache
RUN mv server/mikro-orm.prod.config.ts server/mikro-orm.config.ts
# Build and bundle the server
RUN pnpm run --filter=server bundle
### End server build ###

### Begin server web ###
FROM sources AS build-web
# Install deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
# Build common modules
RUN pnpm run --filter=types --filter=shared build
RUN pnpm run --filter=web build

### Begin server run ###
FROM ffmpeg-base AS server
COPY --from=prod-deps /tunarr/node_modules /tunarr/node_modules
COPY --from=prod-deps /tunarr/server/node_modules /tunarr/server/node_modules
COPY --from=build-server /tunarr/types /tunarr/types
COPY --from=build-server /tunarr/shared /tunarr/shared
COPY --from=build-server /tunarr/server/package.json /tunarr/server/package.json
COPY --from=build-server /tunarr/server/build /tunarr/server/build
ENV TUNARR_BIND_ADDR=0.0.0.0
EXPOSE 8000
CMD [ "/tunarr/server/build/bundle.js" ]
### Begin server run

### Full stack ###
FROM server AS full-stack
COPY --from=build-web /tunarr/web2/dist /tunarr/server/build/web
Loading

0 comments on commit 5570631

Please sign in to comment.