From 0f472873beeb3fa1d26d8e398d113a6de272d749 Mon Sep 17 00:00:00 2001 From: Valentin Zickner Date: Thu, 21 Oct 2021 12:25:14 +0200 Subject: [PATCH] change to alpine image and reduce node_modules size --- Dockerfile | 34 ++++++++++++------- README.md | 10 +++--- angular.json | 1 + ...all.yml => docker-compose-build-local.yml} | 2 +- package.json | 3 ++ prisma/{seed.ts => seed.js} | 4 +-- 6 files changed, 34 insertions(+), 20 deletions(-) rename docker/{docker-compose-all.yml => docker-compose-build-local.yml} (93%) rename prisma/{seed.ts => seed.js} (99%) diff --git a/Dockerfile b/Dockerfile index 45595fbdc..a6a49d7cd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,14 @@ -FROM node:14 as builder - -WORKDIR /app - +FROM node:14-alpine as builder +# build application and add additional files +WORKDIR /ghostfolio +# only add basic files without application itself to avoid rebuilding layers when package.json, etc. did not change COPY ./package.json package.json COPY ./yarn.lock yarn.lock COPY ./prisma/schema.prisma prisma/schema.prisma RUN yarn +# see https://github.com/nrwl/nx/issues/6586 for further details COPY ./decorate-angular-cli.js decorate-angular-cli.js RUN node decorate-angular-cli.js @@ -22,14 +23,21 @@ COPY ./apps apps RUN yarn build:all -COPY ./prisma/seed.ts prisma/seed.ts +# Prepare dist image with additional node_modules +WORKDIR /ghostfolio/dist/apps/api +# package.json was generated by build process, however the yarn.lock need to be used from the original to ensure same versions +COPY ./yarn.lock /ghostfolio/dist/apps/api/yarn.lock + +RUN yarn +COPY prisma /ghostfolio/dist/apps/api/prisma + +# Overwrite generated package.json with original to ensure we have all the scripts +COPY package.json /ghostfolio/dist/apps/api +RUN yarn database:generate-typings -FROM node:14 -COPY --from=builder /app/dist/apps /app/apps -COPY --from=builder /app/package.json /app/package.json -# todo: change build to ensure that node_modules folder isn't required to reduce image size -COPY --from=builder /app/node_modules /app/node_modules -COPY --from=builder /app/prisma /app/prisma -WORKDIR /app +# Image to start, copy everything needed from builder +FROM node:14-alpine +COPY --from=builder /ghostfolio/dist/apps /ghostfolio/apps +WORKDIR /ghostfolio/apps/api EXPOSE 3333 -CMD [ "npm", "run", "start:prod" ] +CMD [ "node", "main" ] diff --git a/README.md b/README.md index 64b91bc3d..119ec849c 100644 --- a/README.md +++ b/README.md @@ -92,15 +92,18 @@ The frontend is built with [Angular](https://angular.io) and uses [Angular Mater To run it with docker you can simply execute: ```bash -docker-compose -f docker/docker-compose-all.yml build -docker-compose -f docker/docker-compose-all.yml up +docker-compose -f docker/docker-compose-build-local.yml build +docker-compose -f docker/docker-compose-build-local.yml up ``` This will build a docker image of ghostfolio locally on your machine and start it up, including dependencies and the database. +### Initialize the Database + To initialize the database, you can run the following command once ghostfolio is started: + ```bash -docker-compose -f docker/docker-compose-all.yml exec ghostfolio yarn setup:database +docker-compose -f docker/docker-compose-build-local.yml exec ghostfolio yarn setup:database ``` After this, go to http://localhost:3333/ and follow those steps: @@ -109,7 +112,6 @@ After this, go to http://localhost:3333/ and follow those steps: 1. Go to the _Admin Control Panel_ and click _Gather All Data_ to fetch historical data 1. Click _Sign out_ and check out the _Live Demo_ - ## Development ### Prerequisites diff --git a/angular.json b/angular.json index c21159c7e..d26410104 100644 --- a/angular.json +++ b/angular.json @@ -35,6 +35,7 @@ }, "configurations": { "production": { + "generatePackageJson": true, "optimization": true, "extractLicenses": true, "inspect": false, diff --git a/docker/docker-compose-all.yml b/docker/docker-compose-build-local.yml similarity index 93% rename from docker/docker-compose-all.yml rename to docker/docker-compose-build-local.yml index 68a41853a..39d6fcf95 100644 --- a/docker/docker-compose-all.yml +++ b/docker/docker-compose-build-local.yml @@ -15,7 +15,7 @@ services: env_file: - ../.env environment: - REDIS_HOST: "redis" + REDIS_HOST: 'redis' DATABASE_URL: postgresql://user:password@postgres:5432/ghostfolio-db?sslmode=prefer ports: - 3333:3333 diff --git a/package.json b/package.json index 2e4e32357..ff873a3e2 100644 --- a/package.json +++ b/package.json @@ -175,5 +175,8 @@ "parser": "typescript", "style": "module" } + }, + "prisma": { + "seed": "node prisma/seed.js" } } diff --git a/prisma/seed.ts b/prisma/seed.js similarity index 99% rename from prisma/seed.ts rename to prisma/seed.js index 700808392..a5a789d59 100644 --- a/prisma/seed.ts +++ b/prisma/seed.js @@ -1,10 +1,10 @@ -import { +const { AccountType, DataSource, PrismaClient, Role, Type -} from '@prisma/client'; +} = require('@prisma/client'); const prisma = new PrismaClient(); async function main() {