Docker
This guide assumes you have knowledge of the following topics:
Why Docker?
Docker provides a consistent environment that ensures the proper functioning of any bot in both development and production, as well as simplifying the integration with CI/CD tools.
With Docker scaling a bot horizontally is simple, allowing workloads to be distributed across multiple instances as needed.
What files does a Seyfert project require?
package.json
seyfert.config.mjs
/node_modules
/src
or/dist
Deploying with Node.js
Container file for a TypeScript project
A basic container file looks as follows:
FROM node:<VERSION_TAG>
WORKDIR /bot
COPY package*.json ./
RUN npm install --production
RUN npm i -g typescript
COPY tsconfig.json seyfert.config.mjs ./COPY /src ./src
# Build typescript filesRUN tsc --project tsconfig.json
ENV NODE_ENV=production
ENTRYPOINT ["node", "dist/index.js"]
You must replace <VERSION_TAG>
with the Node.js version you want to use.
Container file with multi-stage builds
Although the previous container image is simple and practical, it can still be improved by adding multi-stage builds and other best practices:
# [ base ] #FROM node:<VERSION_TAG>-alpine AS base
ENV DIR /botWORKDIR $DIR
# [ OS packages ] #FROM base AS pkg
RUN apk update && apk add --no-cache dumb-init
# [ project builder ] #FROM base AS build
COPY package*.json ./
## Ref: https://docs.npmjs.com/cli/v10/commands/npm-ciRUN npm ci## Ref: https://docs.npmjs.com/cli/v10/commands/npm-pruneRUN npm prune --production
RUN npm i -g typescript
COPY tsconfig.json seyfert.config.mjs ./COPY /src ./src
## Build typescriptRUN tsc --project tsconfig.json
# [ production ready ] #FROM base AS production
# Joining stages## PackagesCOPY --from=pkg /usr/bin/dumb-init /usr/bin/dumb-init## DependenciesCOPY --from=build $DIR/node_modules ./node_modules## BuilderCOPY --from=build $DIR/dist ./distCOPY --from=build $DIR/package.json ./package.jsonCOPY --from=build $DIR/seyfert.config.js ./seyfert.config.js
# Environment permissionsENV NODE_ENV production## Remove if your project needs root permissionsENV USER nodeUSER $USER
# Run the applicationENTRYPOINT ["dumb-init", "node", "dist/index.js"]