Skip to content

Commit

Permalink
feat: create new postgres project
Browse files Browse the repository at this point in the history
This is generally an empty project in Nx's eyes, but it does exist for
the sole purpose of allowing me to only push up a new postgres image
whenever I modify the dockerfile for the unteris postgres image.

The reason for the image is to make use of the [ulid][ulid] extension
that actually has a true ulid type in postgres and can be translated
into timestamps by casting. They're also way faster, which is a big
bonus.

The dockerfiles have also been split apart again, which, while creating
some WET code in terms of the dockerfiles, it does allow for a better
separation of concerns of each file, and can probably be used to better
improve each file as necessary. The local docker compose file also now
uses the local postgres dockerfile instead of the common image, again
for the ulid implementation.

The docker build executor now takes in these dockerfiles by matching to
the project root and builds each image. Seems to be faster as well, but
only time will tell when it comes toactually publishing.

Speaking of which, I should start running the package non-publish
command on CI runs to ensure when I merge to main it will still work as
expected.

[ulid]: https://github.com/pksunkara/pgx_ulid/
  • Loading branch information
jmcdo29 committed Jul 22, 2023
1 parent e1ab8dc commit 6395cdd
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ apps/site/public/images

*.local
*.production
docker/
docker/*
!libs/docker/
*.log
dump
87 changes: 0 additions & 87 deletions Dockerfile

This file was deleted.

38 changes: 38 additions & 0 deletions apps/kysely-cli/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM node:20.2-alpine3.18 as unteris-node

RUN npm i -g [email protected] && \
apk add --no-cache \
dumb-init=1.2.5-r2

FROM unteris-node AS unteris-common

WORKDIR /src
RUN apk add --no-cache \
python3 \
make \
gcc \
g++
COPY package.json \
tsconfig* \
nx.json \
pnpm-lock.yaml \
./
ENV CYPRESS_INSTALL_BINARY=0
RUN pnpm i

FROM unteris-common AS migrations-build
COPY apps/kysely-cli ./apps/kysely-cli
COPY libs/server ./libs/server
COPY libs/db ./libs/db
COPY libs/shared ./libs/shared/
RUN pnpm nx run kysely-cli:build:production

FROM unteris-node AS migrations-prod
LABEL description="The image that runs migrations for the Unteris Database. This should be ran as one-off commands on demand"
USER node
WORKDIR /src
COPY --from=migrations-build --chown=node:node /src/dist ./dist
RUN cp ./dist/apps/kysely-cli/package.json ./package.json
ENV NODE_ENV=production
RUN pnpm i
CMD ["dumb-init", "node", "dist/apps/kysely-cli/main", "migrate"]
21 changes: 21 additions & 0 deletions apps/postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM postgres:15.3 as postgres-prod

LABEL description="A custom postgres image that has ulids built in as an extension with binary representation and the ability to retreive the timestamp from them."

RUN apt update

RUN apt install -y --no-install-recommends curl ca-certificates

RUN apt upgrade -y

RUN curl -OJL https://github.com/pksunkara/pgx_ulid/releases/download/v0.1.1/pgx_ulid-v0.1.1-pg15-$(dpkg --print-architecture)-linux-gnu.deb && \
apt install ./pgx_ulid-v0.1.1-pg15-$(dpkg --print-architecture)-linux-gnu.deb

WORKDIR /home/postgres

ENV HOME=/home/postgres
ENV PATH=/home/postgres/.cargo/bin:$PATH

RUN chown postgres:postgres /home/postgres

USER postgres
23 changes: 23 additions & 0 deletions apps/postgres/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "postgres",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/postgres",
"projectType": "application",
"targets": {
"lint": {
"executor": "nx:noop"
},
"package": {
"executor": "@unteris/plugin/docker:build",
"outputs": ["docker/cache/postgres"],
"opitons": {},
"inputs": ["{projectRoot}/Dockerfile"],

"configurations": {
"ci": {
"publish": true
}
}
}
}
}
37 changes: 37 additions & 0 deletions apps/server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM node:20.2-alpine3.18 as unteris-node


RUN npm i -g [email protected] && \
apk add --no-cache \
dumb-init=1.2.5-r2

FROM unteris-node AS unteris-common

WORKDIR /src
RUN apk add --no-cache \
python3 \
make \
gcc \
g++
COPY package.json \
tsconfig* \
nx.json \
pnpm-lock.yaml \
./
ENV CYPRESS_INSTALL_BINARY=0
RUN pnpm i

FROM unteris-common AS server-build
COPY apps/server ./apps/server/
COPY libs/server ./libs/server/
COPY libs/shared ./libs/shared/
RUN pnpm nx run server:build:production

FROM unteris-node AS server-prod
LABEL description="The server side code for the Unteris website. It runs a NestJS server and connects to a redis and postgres database"
USER node
WORKDIR /src
COPY --from=server-build --chown=node:node /src/dist/apps/server ./
ENV NODE_ENV=production
RUN pnpm i
CMD ["dumb-init", "node", "main.js"]
37 changes: 37 additions & 0 deletions apps/site/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM node:20.2-alpine3.18 as unteris-node


RUN npm i -g [email protected] && \
apk add --no-cache \
dumb-init=1.2.5-r2

FROM unteris-node AS unteris-common

WORKDIR /src
RUN apk add --no-cache \
python3 \
make \
gcc \
g++
COPY package.json \
tsconfig* \
nx.json \
pnpm-lock.yaml \
./
ENV CYPRESS_INSTALL_BINARY=0
RUN pnpm i

FROM unteris-common AS site-build
WORKDIR /src
COPY apps/site ./apps/site
COPY libs/ui ./libs/ui
COPY libs/shared ./libs/shared/
RUN VITE_SERVER_URL="https://api.unteris.com" pnpm nx run site:build:production

FROM caddy:2.6.4-alpine as site-prod
LABEL description="The Unteris website image, ran via a Caddy reverse proxy"
WORKDIR /src
COPY apps/site/robots.txt ./dist/apps/site/robots.txt
COPY --from=site-build /src/dist/apps/site/ ./dist/apps/site
COPY Caddyfile ./Caddyfile
CMD ["caddy", "run", "--config", "Caddyfile"]
4 changes: 1 addition & 3 deletions apps/site/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@
"package": {
"executor": "@unteris/plugin/docker:build",
"outputPath": ["docker/cache/site"],
"options": {
"verbose": true
},
"options": {},
"configurations": {
"ci": {
"publish": true
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ services:
- '/var/run/docker.sock:/var/run/docker.sock'
command: '--include-stopped --revive-stopped --schedule "0 0 * * * *" --label-enable'
postgres:
image: postgres
image: jmcdo29/unteris-postgres
env_file: .env
ports:
- '5433:5432'
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
version: '3'
services:
postgres:
image: postgres
build:
context: .
dockerfile: apps/postgres/Dockerfile
ports:
- '5432:5432'
environment:
Expand Down
12 changes: 0 additions & 12 deletions libs/docker/generators.json

This file was deleted.

12 changes: 11 additions & 1 deletion libs/docker/src/executors/build/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export default async function runExecutor(
options: BuildExecutorSchema,
context: ExecutorContext
) {
console.log(
context.projectsConfigurations!.projects[context.projectName!].root
);
return new Promise((resolve, reject) => {
const project = options.imageName ?? context.projectName;
const logger = new Ogma({
Expand Down Expand Up @@ -39,7 +42,14 @@ export default async function runExecutor(
const builder = options.builder ?? 'container';
logger.verbose(`Using doocker builder ${builder}`);
const publish = options.publish ?? false;
const commandString = `docker buildx build ${tags
const path =
options.path ??
`${
context.projectsConfigurations!.projects[context.projectName!].root
}/Dockerfile`;
const commandString = `docker buildx build ${
path ? '-f ' + path + ' ' : ''
}${tags
.map((t) => `-t ${t}`)
.join(
' '
Expand Down
1 change: 1 addition & 0 deletions libs/docker/src/executors/build/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export interface BuildExecutorSchema {
builder?: string;
dockerNamespace?: string;
publish?: boolean;
path?: string;
} // eslint-disable-line
4 changes: 4 additions & 0 deletions libs/docker/src/executors/build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"type": "string",
"description": "The target of the dockerfile to use"
},
"path": {
"type": "string",
"description": "The path to the dockerfile to make use of"
},
"cachePath": {
"type": "string",
"description": "The cache path to read from and write to"
Expand Down

0 comments on commit 6395cdd

Please sign in to comment.