From 0e7b0a0a3eb07e8b4084ab2f8df4f45cedab12dc Mon Sep 17 00:00:00 2001 From: Nicolas Meienberger Date: Mon, 16 May 2022 23:30:08 +0200 Subject: [PATCH] WIP: System-api in docker container --- .github/workflows/build-images.yml | 34 +++++++++++++++++ .gitignore | 1 + docker-compose.yml | 30 ++++++++++----- package.json | 4 +- packages/dashboard/.eslintignore | 3 +- packages/dashboard/Dockerfile | 3 -- packages/dashboard/next.config.js | 2 +- packages/dashboard/src/core/api.ts | 2 +- packages/system-api/.dockerignore | 2 + packages/system-api/Dockerfile | 38 +++++++++++++++++++ packages/system-api/package.json | 4 +- packages/system-api/src/config/config.ts | 14 ++----- .../auth/__tests__/auth.service.test.ts | 1 - .../src/modules/auth/auth.service.ts | 2 +- 14 files changed, 108 insertions(+), 32 deletions(-) create mode 100644 .github/workflows/build-images.yml create mode 100644 packages/system-api/.dockerignore create mode 100644 packages/system-api/Dockerfile diff --git a/.github/workflows/build-images.yml b/.github/workflows/build-images.yml new file mode 100644 index 00000000..88f1a977 --- /dev/null +++ b/.github/workflows/build-images.yml @@ -0,0 +1,34 @@ +name: Docker build + +on: + push: + branches: + - 'master' + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v3 + - + name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - + name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - + name: Build and push + uses: docker/build-push-action@v2 + with: + context: ./packages/dashboard + platforms: linux/amd64,linux/arm64 + push: true + tags: meienberger/tipi-dashboard:latest \ No newline at end of file diff --git a/.gitignore b/.gitignore index fc86472f..5cd5feea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .pnpm-debug.log .env .env* +github.secrets node_modules/ nginx/* letsencrypt/* diff --git a/docker-compose.yml b/docker-compose.yml index 21d9e1f8..e8cd112e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,21 +13,33 @@ services: - /var/run/docker.sock:/var/run/docker.sock:ro - ${PWD}/traefik:/root/.config networks: - - tipi_main_network + tipi_main_network: + ipv4_address: 10.21.21.2 + + + api: + image: meienberger/tipi-api:latest + container_name: api + ports: + - 3001:3001 + volumes: + ## Docker sock + - /var/run/docker.sock:/var/run/docker.sock:ro + - ${PWD}:/tipi + networks: + tipi_main_network: + ipv4_address: 10.21.21.3 dashboard: - build: - context: ./packages/dashboard - dockerfile: Dockerfile - args: - INTERNAL_IP_ARG: ${INTERNAL_IP} + image: meienberger/tipi-dashboard:latest container_name: dashboard - volumes: - - ${PWD}/state:/app/state ports: - 3000:3000 networks: - - tipi_main_network + tipi_main_network: + ipv4_address: 10.21.21.4 + environment: + - INTERNAL_IP=${INTERNAL_IP} labels: traefik.enable: true traefik.http.routers.dashboard.rule: PathPrefix("/") # Host(`tipi.local`) && diff --git a/package.json b/package.json index 6cba46f3..bba4eaa7 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "description": "A homeserver for everyone", "scripts": { "prepare": "husky install", - "act": "act --container-architecture linux/amd64 -j test-install" + "act:test-install": "act --container-architecture linux/amd64 -j test-install", + "act:docker": "act --container-architecture linux/amd64 --secret-file github.secrets -j docker", + "docker:build": "docker build ./packages/system-api/ -t meienberger/tipi-api:latest && docker build ./packages/dashboard/ -t meienberger/tipi-dashboard:latest" }, "dependencies": { "eslint": "^8.15.0", diff --git a/packages/dashboard/.eslintignore b/packages/dashboard/.eslintignore index 0dd2adbe..549416b1 100644 --- a/packages/dashboard/.eslintignore +++ b/packages/dashboard/.eslintignore @@ -1,2 +1,3 @@ *.config.js -.eslintrc.js \ No newline at end of file +.eslintrc.js +next.config.js \ No newline at end of file diff --git a/packages/dashboard/Dockerfile b/packages/dashboard/Dockerfile index 32fef079..4e714c86 100644 --- a/packages/dashboard/Dockerfile +++ b/packages/dashboard/Dockerfile @@ -8,9 +8,6 @@ RUN yarn COPY ./ ./ -ARG INTERNAL_IP_ARG -ENV INTERNAL_IP $INTERNAL_IP_ARG - RUN yarn build CMD ["yarn", "start"] \ No newline at end of file diff --git a/packages/dashboard/next.config.js b/packages/dashboard/next.config.js index 7dca23ec..bb37a1c8 100644 --- a/packages/dashboard/next.config.js +++ b/packages/dashboard/next.config.js @@ -2,7 +2,7 @@ const { NODE_ENV, INTERNAL_IP } = process.env; const nextConfig = { - reactStrictMode: true, + reactStrictMode: false, env: { INTERNAL_IP: NODE_ENV === 'development' ? 'localhost' : INTERNAL_IP, }, diff --git a/packages/dashboard/src/core/api.ts b/packages/dashboard/src/core/api.ts index 58ee2511..49ef37b2 100644 --- a/packages/dashboard/src/core/api.ts +++ b/packages/dashboard/src/core/api.ts @@ -1,6 +1,6 @@ import axios, { Method } from 'axios'; -export const BASE_URL = `http://${process.env.INTERNAL_IP}:3001`; +export const BASE_URL = 'http://localhost:3001'; interface IFetchParams { endpoint: string; diff --git a/packages/system-api/.dockerignore b/packages/system-api/.dockerignore new file mode 100644 index 00000000..04c01ba7 --- /dev/null +++ b/packages/system-api/.dockerignore @@ -0,0 +1,2 @@ +node_modules/ +dist/ \ No newline at end of file diff --git a/packages/system-api/Dockerfile b/packages/system-api/Dockerfile new file mode 100644 index 00000000..1f101cdb --- /dev/null +++ b/packages/system-api/Dockerfile @@ -0,0 +1,38 @@ +FROM ubuntu:20.04 +ARG DEBIAN_FRONTEND=noninteractive + +WORKDIR /app + +# Install docker +RUN apt-get update && apt-get install -y \ + ca-certificates \ + curl \ + gnupg \ + lsb-release + +RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg + +RUN echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ + $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null + +RUN apt-get update +RUN apt-get install -y docker-ce docker-ce-cli containerd.io + +# Install node +RUN curl -sL https://deb.nodesource.com/setup_14.x | bash - +RUN apt-get install -y nodejs + +# Install docker-compose +RUN curl -L "https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose +RUN chmod +x /usr/local/bin/docker-compose + +COPY ./package.json ./ + +RUN npm install + +COPY ./ ./ + +RUN npm run build + +CMD ["npm", "run", "start"] \ No newline at end of file diff --git a/packages/system-api/package.json b/packages/system-api/package.json index e009c594..fac3b81b 100644 --- a/packages/system-api/package.json +++ b/packages/system-api/package.json @@ -12,7 +12,7 @@ "lint": "eslint . --ext .ts", "test": "jest", "test:watch": "jest --watch", - "build-prod": "esbuild --bundle src/server.ts --outdir=dist --allow-overwrite --sourcemap --platform=node --minify --analyze=verbose --external:./node_modules/* --format=esm", + "build": "esbuild --bundle src/server.ts --outdir=dist --allow-overwrite --sourcemap --platform=node --minify --analyze=verbose --external:./node_modules/* --format=esm", "build:watch": "esbuild --bundle src/server.ts --outdir=dist --allow-overwrite --sourcemap --platform=node --external:./node_modules/* --format=esm --watch", "start:dev": "NODE_ENV=development nodemon --trace-deprecation --trace-warnings --watch dist dist/server.js", "dev": "concurrently \"yarn build:watch\" \"yarn start:dev\"", @@ -22,7 +22,6 @@ "license": "ISC", "dependencies": { "argon2": "^0.28.5", - "bcrypt": "^5.0.1", "compression": "^1.7.4", "cookie-parser": "^1.4.6", "cors": "^2.8.5", @@ -44,7 +43,6 @@ "tcp-port-used": "^1.0.2" }, "devDependencies": { - "@types/bcrypt": "^5.0.0", "@types/compression": "^1.7.2", "@types/cookie-parser": "^1.4.3", "@types/cors": "^2.8.12", diff --git a/packages/system-api/src/config/config.ts b/packages/system-api/src/config/config.ts index 338e4c5f..d330c29c 100644 --- a/packages/system-api/src/config/config.ts +++ b/packages/system-api/src/config/config.ts @@ -9,21 +9,13 @@ interface IConfig { dotenv.config(); -const { NODE_ENV = 'development', ROOT_FOLDER = '', JWT_SECRET = '', INTERNAL_IP = '' } = process.env; - -const missing = []; - -if (!ROOT_FOLDER) missing.push('ROOT_FOLDER'); - -if (missing.length > 0) { - throw new Error(`Missing environment variables: ${missing.join(', ')}`); -} +const { NODE_ENV = 'development', JWT_SECRET = '' } = process.env; const config: IConfig = { NODE_ENV, - ROOT_FOLDER, + ROOT_FOLDER: '/tipi', JWT_SECRET, - CLIENT_URLS: ['http://locahost:3000', `http://${INTERNAL_IP}`, `http://${INTERNAL_IP}:3000`], + CLIENT_URLS: ['http://locahost:3000', 'http://10.21.21.4', 'http://10.21.21.4:3000'], }; export default config; diff --git a/packages/system-api/src/modules/auth/__tests__/auth.service.test.ts b/packages/system-api/src/modules/auth/__tests__/auth.service.test.ts index 7d13f897..b22720cf 100644 --- a/packages/system-api/src/modules/auth/__tests__/auth.service.test.ts +++ b/packages/system-api/src/modules/auth/__tests__/auth.service.test.ts @@ -1,5 +1,4 @@ import fs from 'fs'; -// import bcrypt from 'bcrypt'; import jsonwebtoken from 'jsonwebtoken'; import * as argon2 from 'argon2'; import config from '../../../config'; diff --git a/packages/system-api/src/modules/auth/auth.service.ts b/packages/system-api/src/modules/auth/auth.service.ts index c5f81957..0d91f09b 100644 --- a/packages/system-api/src/modules/auth/auth.service.ts +++ b/packages/system-api/src/modules/auth/auth.service.ts @@ -30,7 +30,7 @@ const register = async (email: string, password: string, name: string) => { throw new Error('User already exists'); } - const hash = await argon2.hash(password); // bcrypt.hash(password, 10); + const hash = await argon2.hash(password); const newuser: IUser = { email, name, password: hash }; const token = await AuthHelpers.getJwtToken(newuser, password);