feature: Adds docker support.

This commit is contained in:
greysoh 2024-04-28 09:42:02 -04:00
parent 6ff28de463
commit a03f3ef344
No known key found for this signature in database
GPG key ID: FE0F173B8FC01571
7 changed files with 81 additions and 2 deletions

16
Dockerfile Normal file
View file

@ -0,0 +1,16 @@
FROM node:20.11.1-bookworm
WORKDIR /app/
COPY src /app/src
COPY prisma /app/prisma
COPY docker-entrypoint.sh /app/
COPY tsconfig.json /app/
COPY package.json /app/
COPY package-lock.json /app/
COPY srcpatch.sh /app/
RUN sh srcpatch.sh
RUN npm install --save-dev
RUN npm run build
RUN rm srcpatch.sh out/**/*.ts out/**/*.map
RUN rm -rf src
RUN npm prune --production
ENTRYPOINT sh docker-entrypoint.sh

View file

@ -17,5 +17,21 @@
1. First, check if you have a working Nix environment if you're using Nix.
2. Run `nix-shell`, or alternatively `source init.sh` if you're not using Nix.
3. After that, run the project in development mode: `npm run dev`.
4. If you want to explore your database, run `npx prisma studio` to open the database editor.
4. If you want to explore your database, run `npx prisma studio` to open the database editor.
<h2 align="center">Production Deployment</h2>
> [!WARNING]
> Deploying using docker compose is the only officially supported deployment method. Here be dragons!
1. Copy and change the default password (or username & db name too) from the template file `prod-docker.env`:
```bash
sed "s/POSTGRES_PASSWORD=nextnet/POSTGRES_PASSWORD=$(head -c 500 /dev/random | sha512sum | cut -d " " -f 1)/g" prod-docker.env > .env
```
2. Build the dockerfile: `docker compose --env-file .env build`
3. Build the docker stack: `docker compose --env-file .env up`

27
docker-compose.yml Normal file
View file

@ -0,0 +1,27 @@
services:
nextnet:
build:
context: "."
dockerfile: Dockerfile
image: nextnet/latest
container_name: nextnet-api
restart: always
ports:
- 3000:3000
environment:
NODE_ENV: production
DATABASE_URL: "postgresql://${POSTGRES_USERNAME}:${POSTGRES_PASSWORD}@nextnet-postgres:5432/${POSTGRES_DB}?schema=nextnet"
postgres:
image: "postgres:15.4"
container_name: nextnet-postgres
restart: unless-stopped
environment:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_USER: ${POSTGRES_USERNAME}
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:

6
docker-entrypoint.sh Normal file
View file

@ -0,0 +1,6 @@
#!/bin/bash
echo "Welcome to NextNet."
echo "Running database migrations..."
npx prisma migrate deploy
echo "Starting application..."
npm start

5
prod-docker.env Normal file
View file

@ -0,0 +1,5 @@
# These are default values, please change these!
POSTGRES_USERNAME=nextnet
POSTGRES_PASSWORD=nextnet
POSTGRES_DB=nextnet

View file

@ -93,7 +93,10 @@ userLogin(routeOptions);
// Run the server!
try {
await fastify.listen({ port: 3000 });
await fastify.listen({
port: 3000,
host: process.env.NODE_ENV == "production" ? "0.0.0.0" : "127.0.0.1"
});
} catch (err) {
fastify.log.error(err);
process.exit(1);

6
srcpatch.sh Executable file
View file

@ -0,0 +1,6 @@
# !-- DO NOT USE THIS FOR DEVELOPMENT --!
# This is only to source patch files in production deployments, if prisma isn't configured already.
printf "//@ts-nocheck\n$(cat src/routes/backends/lookup.ts)" > src/routes/backends/lookup.ts
printf "//@ts-nocheck\n$(cat src/routes/forward/lookup.ts)" > src/routes/forward/lookup.ts
printf "//@ts-nocheck\n$(cat src/routes/user/lookup.ts)" > src/routes/user/lookup.ts
printf "//@ts-nocheck\n$(cat src/routes/getPermissions.ts)" > src/routes/getPermissions.ts