diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2021-06-30 13:24:28 +0300 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2021-06-30 13:24:28 +0300 |
| commit | 752b96afdf2fa285492deaf3a676d2b2c2e47c9f (patch) | |
| tree | 2f871d262bd2ef87d52e30737b5ead2fc2cd738e | |
| parent | f021b52028f5965516671b7f53e7e71543a8ae52 (diff) | |
Dockerize
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | README.md | 24 | ||||
| -rw-r--r-- | backend/Dockerfile | 27 | ||||
| -rw-r--r-- | backend/config.ts | 7 | ||||
| -rw-r--r-- | backend/index.ts | 6 | ||||
| -rw-r--r-- | backend/shared-types.ts (renamed from shared-types.ts) | 0 | ||||
| -rw-r--r-- | docker-compose.yml | 18 | ||||
| -rw-r--r-- | frontend/Dockerfile | 27 | ||||
| -rw-r--r-- | frontend/package.json | 3 | ||||
| -rw-r--r-- | frontend/shared-types.ts | 3 |
10 files changed, 111 insertions, 5 deletions
@@ -35,3 +35,4 @@ yarn-error.log* .env* .next +out/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..d723d87 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# tomi.science + +## Running + +### Frontend + +Next.js + socket.io. + +- Install packages: `npm ci` +- In development: `npm run dev` +- In production: `npm run build && npm start` + +### Backend + +Express + socket.io + slonik. You need a Postgres db (you can use docker compose). + +- Install packages: `npm ci` +- Run migrations `npm run migrate -- up` +- In development: `npm run dev` +- In production `NODE_ENV=production npm start` + +## Author + +Jan Tuomi <<jans.tuomi@gmail.com>> diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..1301b25 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,27 @@ +# Install dependencies only when needed +FROM node:alpine AS deps +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. +RUN apk add --no-cache libc6-compat +WORKDIR /app +COPY package.json package-lock.json ./ +RUN npm ci --only-prod + +# Rebuild the source code only when needed +FROM node:alpine AS runner +WORKDIR /app + +ENV NODE_ENV production +ENV PORT 4000 + +RUN addgroup -g 1001 -S nodejs +RUN adduser -S server -u 1001 + +COPY --from=deps /app/package.json ./package.json +COPY --from=deps /app/node_modules ./node_modules + +USER server + +EXPOSE 4000 + +COPY *.ts *.json ./ +CMD ["npm", "start] diff --git a/backend/config.ts b/backend/config.ts index 2585736..ae2462e 100644 --- a/backend/config.ts +++ b/backend/config.ts @@ -3,4 +3,11 @@ if (!process.env.POSTGRES_CONNECTION_STRING) { } export const postgresConnectionString = process.env.POSTGRES_CONNECTION_STRING; + +if (!process.env.FRONTEND_URL) { + throw new Error("FRONTEND_URL env var is not defined!"); +} + +export const frontendUrl = process.env.FRONTEND_URL; + export const port = process.env.PORT || 4000; diff --git a/backend/index.ts b/backend/index.ts index 5a974d6..9efec62 100644 --- a/backend/index.ts +++ b/backend/index.ts @@ -8,15 +8,15 @@ import { sql, } from "slonik"; import * as config from "./config"; -import { ValueRecord } from "../shared-types"; -import { Server as SocketIOServer, Socket } from "socket.io"; +import { ValueRecord } from "./shared-types"; +import { Server as SocketIOServer } from "socket.io"; const app = express(); const httpServer = http.createServer(app); const io = new SocketIOServer(httpServer, { cors: { - origin: "*", + origin: config.frontendUrl, }, }); diff --git a/shared-types.ts b/backend/shared-types.ts index 51f3281..51f3281 100644 --- a/shared-types.ts +++ b/backend/shared-types.ts diff --git a/docker-compose.yml b/docker-compose.yml index 5b86be7..cef2a2f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,6 +7,22 @@ services: - "5432:5432" volumes: - dbdata:/var/lib/postgresql/data - + frontend: + build: + context: frontend + args: + NEXT_PUBLIC_BACKEND_URL: ${NEXT_PUBLIC_BACKEND_URL} + ports: + - "3000:80" + tty: true + backend: + build: + context: backend + environment: + POSTGRES_CONNECTION_STRING: ${POSTGRES_CONNECTION_STRING} + FRONTEND_URL: ${FRONTEND_URL} + PORT: 4000 + ports: + - "4000:4000" volumes: dbdata: diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 0000000..2a22355 --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,27 @@ +# Install dependencies only when needed +FROM node:alpine AS deps +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. +RUN apk add --no-cache libc6-compat +WORKDIR /app +COPY package.json package-lock.json ./ +RUN npm ci --only-prod + +# Rebuild the source code only when needed +FROM node:alpine AS builder + +ENV NODE_ENV production +ENV NEXT_TELEMETRY_DISABLED 1 +ARG NEXT_PUBLIC_BACKEND_URL + +WORKDIR /app +COPY . . +COPY --from=deps /app/node_modules ./node_modules +RUN printenv > .env.local +RUN npm run build && npm run export + +# Production image, copy all the files and run next +FROM sebp/lighttpd:latest AS runner + +WORKDIR /var/www/localhost/htdocs + +COPY --from=builder /app/out ./ diff --git a/frontend/package.json b/frontend/package.json index bb2e5b5..48fa29f 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -6,7 +6,8 @@ "dev": "next dev", "build": "next build", "start": "next start", - "lint": "next lint" + "lint": "next lint", + "export": "next export" }, "dependencies": { "next": "11.0.1", diff --git a/frontend/shared-types.ts b/frontend/shared-types.ts new file mode 100644 index 0000000..51f3281 --- /dev/null +++ b/frontend/shared-types.ts @@ -0,0 +1,3 @@ +export interface ValueRecord { + value: number; +} |
