Skip to content

Commit

Permalink
Add Docker config for production (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
amantri authored Jun 9, 2023
1 parent 1272d3c commit 48c72ab
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.env
.env*
.git
node_modules
.next
.nsm
.env.local
**/.terraform
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ build
.terraform*
terraform.tfstate*

# Ignore all .env files except .env.example
.env*
!.env.example

# misc
.DS_Store
.env*.local
.vscode

npm-debug.log*
Expand Down
43 changes: 43 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Dockerfile to build an optimized version of the Compass server.
#
# To run the production image locally, create a .env.production file with the
# appropriate env vars (a copy of .env.local works - see NOTE below).
#
# Command to build and run a Docker image for production:
# % docker build . -t compass && docker run -p=3000:3000 --name=compass --rm --env-file=.env.production compass
#
# If the target platform is Apple M1 or other ARM platform, use this command to build:
# % docker build --platform=linux/arm64 .
#
# NOTE: If you have the references to `localhost` in the .env file for other
# services like postgres, replace `localhost` with either `host.docker.internal`
# or the local IP address (`% ipconfig getifaddr en0`). This is needed as the
# docker image runs in its own VM and `localhost` resolves to is own VM, not
# the host.


# Start with the latest Node.js LTS release
FROM --platform=linux/amd64 node:18-bullseye-slim

# Set env variables
ENV NODE_ENV production
ENV APP_HOME=/opt/node/app

USER node:node
WORKDIR $APP_HOME

COPY --chown=node:node package*.json .

# We install Husky via a "prepare" lifecycle script - disable it in prod
# See: https://typicode.github.io/husky/guide.html#disable-husky-in-ci-docker-prod
RUN npm pkg delete scripts.prepare
RUN npm ci

# Copy the project files into the app directory
COPY --chown=node:node . $APP_HOME

# Build the optimized version of the app
RUN npm run build

# Always the run the database migrations to make prod maintenance easier
CMD npm run db:migrate && npm run start
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ There are two ways to run Compass locally:
2. Start services in Docker & reset the database

```sh
docker compose up -d # start the services in the background
docker compose -f supporting_services/docker-compose.yml up -d # start the services in the background
npm run db:reset # reset and migrate the database
```

Expand Down
26 changes: 15 additions & 11 deletions src/backend/db/lib/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parse } from "pg-connection-string";
import * as postgresMigrations from "postgres-migrations";
import * as zg from "zapatos/generate";
import { Pool } from "pg";
import path from "node:path";
import { logger } from "backend/lib";

Expand All @@ -23,16 +23,20 @@ export const migrate = async (
logger.info("Migrating database...");
}

const pool = new Pool({
connectionString: databaseUrl,
});
await postgresMigrations.migrate(
{
client: pool,
},
migrationsDirectory
);
await pool.end();
const connectionConfig = parse(databaseUrl);
const dbConfig = {
user: connectionConfig.user ?? "",
password: connectionConfig.password ?? "",
host: connectionConfig.host ?? "",
port: connectionConfig.port ? parseInt(connectionConfig.port, 10) : 5432,

database: connectionConfig.database ?? "",
ensureDatabaseExists: true,
defaultDatabase: "postgres",
};

// Run migrations. Creates database if needed.
await postgresMigrations.migrate(dbConfig, migrationsDirectory);

if (shouldGenerateTypes) {
if (!silent) {
Expand Down
3 changes: 0 additions & 3 deletions src/backend/db/lib/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ export const reset = async (databaseUrl: string) => {

throw error;
});

logger.info(`Creating database ${targetDatabaseName}...`);
await client.query(`CREATE DATABASE ${targetDatabaseName}`);
await client.end();

logger.info("Running migrations...");
Expand Down
File renamed without changes.

0 comments on commit 48c72ab

Please sign in to comment.