Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self-host docker #353

Merged
merged 88 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 77 commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
047cbe5
create Docker builds and add support for runtime env config
jshimko Sep 18, 2024
1efcd23
Merge branch 'dev' into docker-builds
jshimko Sep 20, 2024
f7a2e24
Merge branch 'dev' into docker-builds
jshimko Sep 23, 2024
114ff41
[email protected]
jshimko Sep 23, 2024
083dfa3
remove logged default adin details from seed script
jshimko Sep 23, 2024
12efde1
build for both cpu platforms
jshimko Sep 23, 2024
04bae10
disable client team creation by default in seed
jshimko Sep 23, 2024
315e47e
Merge branch 'dev' into docker-builds
jshimko Sep 24, 2024
2ea9788
fix self host seed script
jshimko Sep 24, 2024
ccc2594
Merge branch 'dev' into docker-builds
jshimko Sep 24, 2024
896088d
prisma 5.20.0
jshimko Sep 24, 2024
1128361
disable arm builds for now
jshimko Sep 24, 2024
9b39752
use prisma directly in docker entrypoint
jshimko Sep 24, 2024
3e0538a
fix wrong dir
jshimko Sep 24, 2024
7f224a2
Merge branch 'dev' into docker-builds
jshimko Sep 25, 2024
d8a0426
env cleanup
jshimko Sep 25, 2024
12b4f2c
revert to previous prisma command in entrypoint script
jshimko Sep 25, 2024
3019f97
Merge branch 'dev' into docker-builds
jshimko Sep 26, 2024
8ebb72d
remove unused multi platform config
jshimko Sep 26, 2024
210afd3
move self-host seed script config to Docker env file
jshimko Sep 26, 2024
9e639b3
self host seed script cleanup
jshimko Sep 26, 2024
e27365b
seed script cleanup
jshimko Sep 26, 2024
656fd4f
db rename
jshimko Sep 26, 2024
7e3c062
only run docker build on dev and main
jshimko Sep 26, 2024
9d2251f
refactor the backend docker build to support NextJS standalone mode
jshimko Sep 26, 2024
2a12fb1
Merge branch 'dev' into docker-builds
jshimko Sep 26, 2024
f6b7de8
add more comments to Dockerfiles
jshimko Sep 26, 2024
44f3917
remove unused script
jshimko Sep 26, 2024
8c5e0e9
delete empty line
jshimko Sep 26, 2024
c6f3d49
remove unused pnpm in final docker build
jshimko Sep 26, 2024
9c46cbd
improve backend startup healthcheck in docker-compose
jshimko Sep 26, 2024
1baec26
lint cleanup
jshimko Sep 26, 2024
10c4ca7
remove unused script
jshimko Sep 26, 2024
016676f
Merge branch 'dev' into docker-builds
jshimko Sep 30, 2024
18b0675
Merge branch 'dev' into docker-builds
jshimko Oct 1, 2024
f85efa5
fix self host seed script after authMethod schema updates
jshimko Oct 1, 2024
5bde713
skip docker buildx check for SecretsUsedInArgOrEnv
jshimko Oct 1, 2024
80c2460
Merge branch 'dev' into docker-builds
jshimko Oct 2, 2024
fe3c631
remove exposed db ports
jshimko Oct 2, 2024
ca7954c
remove unnecessary EXPOSE directives
jshimko Oct 2, 2024
3fea967
Merge branch 'dev' into docker-builds
jshimko Oct 4, 2024
0551457
Merge branch 'dev' into docker-builds
jshimko Oct 7, 2024
2eceee7
[email protected]
jshimko Oct 7, 2024
cc2cee2
fix authMethodConfigs schema change in seed script
jshimko Oct 7, 2024
a69d738
ensure seed script admin user isn’t created because of code comments
jshimko Oct 7, 2024
78dbf23
fix self host seed script contactChannel auth option
jshimko Oct 7, 2024
acf5b94
Merge branch 'dev' into docker-builds
jshimko Oct 8, 2024
b1072aa
Merge branch 'dev' into docker-builds
jshimko Oct 22, 2024
f2c44e1
removed next runtime env
fomalhautb Nov 1, 2024
1f7c7ab
Merge branch 'dev' into docker-build
fomalhautb Nov 1, 2024
9861c00
fixed NEXT_PUBLIC_STACK_HEAD_TAGS
fomalhautb Nov 1, 2024
a6cd34e
fixed build with experimental build mode
fomalhautb Nov 1, 2024
1add8ab
fixed bug
fomalhautb Nov 1, 2024
3699961
added all-in-one
fomalhautb Nov 1, 2024
b241fcb
added example env vars
fomalhautb Nov 1, 2024
d7a5b9f
Merge branch 'dev' into docker-build
fomalhautb Nov 27, 2024
14e5a1e
fixed pnpm lock
fomalhautb Nov 27, 2024
1de7e83
fixed bugs
fomalhautb Nov 27, 2024
fb828b1
fixed bugs
fomalhautb Nov 27, 2024
ef1157c
fixed things
fomalhautb Nov 27, 2024
e5efada
removed unused
fomalhautb Nov 27, 2024
123f343
restructured code
fomalhautb Nov 27, 2024
943336b
added default admin config
fomalhautb Nov 27, 2024
06ee2a1
default admin internal access
fomalhautb Nov 27, 2024
00cd390
fixed problems, added more examples
fomalhautb Nov 27, 2024
33269de
added docs
fomalhautb Nov 28, 2024
8646e49
updated action
fomalhautb Nov 28, 2024
327057d
fixed bugs
fomalhautb Nov 28, 2024
ff41d6d
fixed bug
fomalhautb Nov 28, 2024
9f643ea
added dev branch back
fomalhautb Nov 28, 2024
d797893
updated docs
fomalhautb Nov 28, 2024
df88a99
removed docker build branch action
fomalhautb Nov 28, 2024
258c4ad
fixed comments
fomalhautb Nov 30, 2024
c0cdd69
fixed docker ignore
fomalhautb Nov 30, 2024
e0424b2
removed unused
fomalhautb Nov 30, 2024
891fd29
updated docs
fomalhautb Nov 30, 2024
b0915b4
Update self-host.mdx
N2D4 Nov 30, 2024
9d3de13
Merge branch 'dev' into docker-build
N2D4 Dec 1, 2024
1e91036
Add docker-build branch to GH Actions
N2D4 Dec 1, 2024
d66c969
Merge branch 'dev' into docker-build
N2D4 Dec 1, 2024
3e5cf8b
Update docs
N2D4 Dec 1, 2024
d0db25b
renamed NEXT_PUBLIC_STACK_URL abd STACK_BASE_URL to NEXT_PUBLIC_STACK…
fomalhautb Dec 1, 2024
f598576
fixed self-host script, added github action
fomalhautb Dec 1, 2024
0bbe14a
updated github event
fomalhautb Dec 1, 2024
f610693
updated seed script
fomalhautb Dec 1, 2024
0a6bd18
Merge branch 'dev' into docker-build
fomalhautb Dec 1, 2024
3e4004c
added docker build back
fomalhautb Dec 1, 2024
ddf9fd0
fixed docs
fomalhautb Dec 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions .dockerignore
fomalhautb marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Git ignore rules
*.untracked
*.untracked.*

.vercel

# Misc
.DS_Store
.eslintcache
.env.local
.env.*.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log
ui-debug.log
.pnpm-debug.log
.husky
tmp

vitest.config.ts.timestamp-*
tsup.config.bundled_*

# Dependencies
node_modules

# Build dirs
.next
build
dist

# Generated files
.docusaurus
.cache-loader
**.tsbuildinfo

.xata*

# VS
/.vs/slnx.sqlite-journal
/.vs/slnx.sqlite
/.vs
.vscode/generated*

# Jetbrains
.idea

# GitHub Actions runner
/actions-runner
/_work

# DB
dev.db*
packages/adapter-prisma/prisma/dev.db
packages/adapter-prisma/prisma/migrations
db.sqlite
packages/adapter-supabase/supabase/.branches
packages/adapter-drizzle/.drizzle

# Tests
coverage
dynamodblocal-bin
firestore-debug.log
test.schema.gql
test-results
playwright-report
blob-report
playwright/.cache

# Turborepo
.turbo

# docusaurus
docs/.docusaurus
docs/manifest.mjs

# Core
packages/core/src/providers/oauth-types.ts
packages/core/lib
packages/core/providers
docs/docs/reference/core

# Next.js
docs/docs/reference/nextjs
next-env.d.ts

# SvelteKit
packages/frameworks-sveltekit/index.*
packages/frameworks-sveltekit/client.*
packages/frameworks-sveltekit/.svelte-kit
packages/frameworks-sveltekit/package
packages/frameworks-sveltekit/vite.config.js.timestamp-*
packages/frameworks-sveltekit/vite.config.ts.timestamp-*
docs/docs/reference/sveltekit

# SolidStart
docs/docs/reference/solidstart

# Express
docs/docs/reference/express

# Adapters
docs/docs/reference/adapter

## Drizzle migration folder
.drizzle

# Sentry Config File
.sentryclirc

# Python
__pycache__/
.venv/

# Docker ignore rules
.changeset
.git
.github
.turbo
**/.turbo
.vscode

.env
.env.*
**/.env
**/.env.*
**/.next

**/dist

examples

node_modules
**/node_modules

deploy
!deploy/docker/**/entrypoint.sh
docker-compose.yaml
48 changes: 48 additions & 0 deletions .github/workflows/docker-build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Docker Build and Push

on:
push:
branches:
- main
- dev
N2D4 marked this conversation as resolved.
Show resolved Hide resolved
tags:
- "*.*.*"

jobs:
build-server:
name: Docker Build and Push Server
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ secrets.DOCKER_REPO }}/server
tags: |
type=ref,event=branch
type=sha,prefix=
type=match,pattern=\d.\d.\d

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ./docker/server/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ docs/docs/reference/adapter
# Sentry Config File
.sentryclirc

# python
# Python
__pycache__/
.venv/
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"pageview",
"pkcco",
"PKCE",
"pooler",
"posthog",
"Proxied",
"psql",
Expand Down
4 changes: 4 additions & 0 deletions apps/backend/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const withConfiguredSentryConfig = (nextConfig) =>

/** @type {import('next').NextConfig} */
const nextConfig = {
// optionally set output to "standalone" for Docker builds
// https://nextjs.org/docs/pages/api-reference/next-config-js/output
output: process.env.NEXT_CONFIG_OUTPUT,
N2D4 marked this conversation as resolved.
Show resolved Hide resolved

// we're open-source, so we can provide source maps
productionBrowserSourceMaps: true,
poweredByHeader: false,
Expand Down
5 changes: 4 additions & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"with-env:prod": "dotenv -c --",
"dev": "concurrently -n \"dev,codegen,prisma-studio\" -k \"next dev --port 8102\" \"pnpm run codegen:watch\" \"pnpm run prisma-studio\"",
"build": "pnpm run codegen && next build",
"docker-build": "pnpm run codegen && next build --experimental-build-mode compile",
"self-host-seed-script": "tsup --config prisma/tsup.config.ts",
"analyze-bundle": "ANALYZE_BUNDLE=1 pnpm run build",
"start": "next start --port 8102",
"codegen-prisma": "pnpm run prisma generate",
Expand Down Expand Up @@ -76,8 +78,9 @@
"@types/semver": "^7.5.8",
"concurrently": "^8.2.2",
"glob": "^10.4.1",
"prisma": "^5.9.1",
"prisma": "^5.20.0",
"rimraf": "^5.0.5",
"tsup": "^8.3.0",
"tsx": "^4.7.2"
}
}
135 changes: 135 additions & 0 deletions apps/backend/prisma/seed-self-host.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a different script from the usual seed script?

Copy link
Contributor Author

@fomalhautb fomalhautb Nov 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The local script sets up a project optimized for testing, and the self-host script sets up a project for prod use. We can merge the two scripts later to reduce code repetition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference? Should be the same IMO (except that testing should have some envvars set to insecure values by default), else our testing env deviates from prod

Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/* eslint-disable no-restricted-syntax */
import { PrismaClient } from '@prisma/client';
import { hashPassword } from "@stackframe/stack-shared/dist/utils/hashes";

const prisma = new PrismaClient();

async function seed() {
console.log('Seeding database...');

// Optional default admin user
const adminEmail = process.env.STACK_DEFAULT_DASHBOARD_USER_EMAIL;
const adminPassword = process.env.STACK_DEFAULT_DASHBOARD_USER_PASSWORD;
const adminInternalAccess = process.env.STACK_DEFAULT_DASHBOARD_USER_INTERNAL_ACCESS === 'true';

// Optionally disable sign up for "internal" project
const signUpEnabled = process.env.STACK_INTERNAL_SIGN_UP_ENABLED === 'true';

const existingProject = await prisma.project.findUnique({
where: {
id: 'internal',
},
});

if (existingProject) {
console.log('Internal project already exists, skipping seed script');
return;
}

await prisma.$transaction(async (tx) => {
const createdProject = await tx.project.create({
data: {
id: 'internal',
displayName: 'Stack Dashboard',
description: 'Stack\'s admin dashboard',
isProductionMode: false,
apiKeySets: {
create: [{
description: "Internal API key set",
// These keys must match the values used in the Stack dashboard env to be able to login via the UI.
publishableClientKey: process.env.NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY,
secretServerKey: process.env.STACK_SECRET_SERVER_KEY,
superSecretAdminKey: process.env.STACK_SUPER_SECRET_ADMIN_KEY,
expiresAt: new Date('2099-12-31T23:59:59Z'),
}],
},
config: {
create: {
allowLocalhost: true,
signUpEnabled, // see STACK_SIGN_UP_DISABLED var above
emailServiceConfig: {
create: {
proxiedEmailServiceConfig: {
create: {}
}
}
},
createTeamOnSignUp: false,
clientTeamCreationEnabled: false,
authMethodConfigs: {
create: [
{
passwordConfig: {
create: {},
}
},
],
}
}
}
},
});

console.log('Internal project created');

// Create optional default admin user if credentials are provided.
// This user will be able to login to the dashboard with both email/password and magic link.
if (adminEmail && adminPassword) {
const newUser = await tx.projectUser.create({
data: {
projectId: 'internal',
serverMetadata: adminInternalAccess
? { managedProjectIds: ['internal'] }
: undefined,
}
});

await tx.contactChannel.create({
data: {
projectUserId: newUser.projectUserId,
projectId: 'internal',
type: 'EMAIL' as const,
value: adminEmail as string,
isVerified: false,
isPrimary: 'TRUE',
usedForAuth: 'TRUE',
}
});

const passwordConfig = await tx.passwordAuthMethodConfig.findFirstOrThrow({
where: {
projectConfigId: createdProject.configId
},
include: {
authMethodConfig: true,
}
});

await tx.authMethod.create({
data: {
projectId: 'internal',
projectConfigId: createdProject.configId,
projectUserId: newUser.projectUserId,
authMethodConfigId: passwordConfig.authMethodConfigId,
passwordAuthMethod: {
create: {
passwordHash: await hashPassword(adminPassword),
projectUserId: newUser.projectUserId,
}
}
}
});

console.log('Initial admin user created: ', adminEmail);
}
});

console.log('Seeding complete!');
}

seed().catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
// eslint-disable-next-line @typescript-eslint/no-misused-promises
}).finally(async () => await prisma.$disconnect());
13 changes: 13 additions & 0 deletions apps/backend/prisma/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from 'tsup';

// tsup config to build the self-hosting seed script so it can be
// run in the Docker container with no extra dependencies.
export default defineConfig({
entry: ['prisma/seed-self-host.ts'],
format: ['cjs'],
outDir: 'dist',
target: 'node22',
platform: 'node',
noExternal: ['@stackframe/stack-shared', '@prisma/client'],
clean: true
});
4 changes: 4 additions & 0 deletions apps/dashboard/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const withConfiguredSentryConfig = (nextConfig) =>

/** @type {import('next').NextConfig} */
const nextConfig = {
// optionally set output to "standalone" for Docker builds
// https://nextjs.org/docs/pages/api-reference/next-config-js/output
output: process.env.NEXT_CONFIG_OUTPUT,

N2D4 marked this conversation as resolved.
Show resolved Hide resolved
pageExtensions: ["js", "jsx", "mdx", "ts", "tsx"],

// we're open-source, so we can provide source maps
Expand Down
Loading