From 43a8893689a5d865dd95752b677ba42ef4c63147 Mon Sep 17 00:00:00 2001 From: Moritz Reich Date: Mon, 16 Jan 2023 09:02:33 +0000 Subject: [PATCH 1/4] fixed and improved local start script Signed-off-by: Moritz Reich --- src/{.env => .env.template} | 10 ++- src/.gitignore | 1 + src/ReadMe.md | 27 ++++++- src/compose.yaml | 59 --------------- src/deskstar-backend/Deskstar/Dockerfile_dev | 20 +++++ src/deskstar-frontend/Dockerfile | 56 ++++++++++++-- src/deskstar-frontend/Dockerfile_production | 11 --- src/deskstar-frontend/next.config.js | 1 + src/deskstar-frontend/yarn.lock | 12 +++ src/docker-compose.yml | 78 ++++++++++++++++++++ src/start.sh | 7 +- 11 files changed, 202 insertions(+), 80 deletions(-) rename src/{.env => .env.template} (51%) create mode 100644 src/.gitignore delete mode 100644 src/compose.yaml create mode 100644 src/deskstar-backend/Deskstar/Dockerfile_dev delete mode 100644 src/deskstar-frontend/Dockerfile_production create mode 100644 src/docker-compose.yml diff --git a/src/.env b/src/.env.template similarity index 51% rename from src/.env rename to src/.env.template index 1280759d..3350b2ce 100644 --- a/src/.env +++ b/src/.env.template @@ -1,3 +1,11 @@ +# Copy this file into a .env.local + +BACKEND_URL=http://localhost:3001 +# Secret which is used for the jwt auth tokens in frontend +NEXTAUTH_SECRET=test1234 +# Must be the public url of the frontend +NEXTAUTH_URL=http://localhost:3000 + POSTGRES_DB=deskstar POSTGRES_USER=postgres POSTGRES_PASSWORD=root @@ -10,6 +18,6 @@ DB__PASSWORD=root EMAIL__HOST=smtp.strato.de EMAIL__PORT=587 EMAIL__ADDRESS=noreply@deskstar.de -EMAIL__PSW=123456 +EMAIL__PSW= ASPNETCORE_ENVIRONMENT=Development diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 00000000..2ad9294d --- /dev/null +++ b/src/.gitignore @@ -0,0 +1 @@ +*.local diff --git a/src/ReadMe.md b/src/ReadMe.md index 4f49f850..4866ff07 100644 --- a/src/ReadMe.md +++ b/src/ReadMe.md @@ -1,4 +1,29 @@ -# Local Dev Setup +# Local Setup + +## Local production run + +1. Copy the `.env.template` file to `.env.local`: +```bash +cp .env.template .env.local +``` + +2. Add the necessary settings to the `.env.local` or change it. The `EMAIL__PSW` is needed to run the backend. Please change the mail settings accordingly. + +3. Inside the `src` folder run: + +```bash +./start.sh +``` + +4. The frontend should now be reachable on `http://localhost:3000`. After that you should be able to register, login and use the software. + +5. OPTIONAL: If you want to fill the database with our dummy-data you can use the following command: +```bash +docker exec -i deskstar_postgres psql -U postgres deskstar < ./deskstar-db/dummy-data.sql +``` +The dummy data already contain some companies you can use and also some test users. You can find the login data at the bottom of this README. + +## Local Dev Setup ### 1. Build and open in Dev Container diff --git a/src/compose.yaml b/src/compose.yaml deleted file mode 100644 index 6c8adc4f..00000000 --- a/src/compose.yaml +++ /dev/null @@ -1,59 +0,0 @@ -services: - frontend: - container_name: frontend - user: "${USER_ID}:${GROUP_ID}" # prevent root files in host system - build: - context: ./deskstar-frontend - ports: - - 3000:3000 - volumes: - - ./deskstar-frontend:/app/code:cached - # prevent mounting node_modules of host system into container - - exclude:/app/code/node_modules:cached - networks: - - external - - internal - command: bash -c "rm -rf /app/code/node_modules/* && yarn run dev" # clean exclude volume on each startup - backend: - #TODO: import source code via bind mount during runtime - #TODO: exclude build files of container - #TODO: clear volumes on each startup - #TODO: add hot reload - container_name: backend - user: "${USER_ID}:${GROUP_ID}" - depends_on: - - "postgres" - build: - context: ./deskstar-backend/Deskstar - ports: - - 3001:80 - environment: - - ASPNETCORE_ENVIRONMENT - - DB__HOST - - DB__DATABASE - - DB__USERNAME - - DB__PASSWORD - networks: - - internal - postgres: - container_name: postgres - build: - context: ./deskstar-db - user: "postgres" - environment: - - POSTGRES_USER - - POSTGRES_DB - - POSTGRES_PASSWORD - volumes: - - ./deskstar-db/init:/docker-entrypoint-initdb.d:cached - ports: - - 5432:5432 - # reset postgres on each startup - networks: - - internal - command: bash -c "rm -rfd /var/lib/postgresql/data/* && docker-entrypoint.sh postgres" -volumes: - exclude: -networks: - internal: - external: \ No newline at end of file diff --git a/src/deskstar-backend/Deskstar/Dockerfile_dev b/src/deskstar-backend/Deskstar/Dockerfile_dev new file mode 100644 index 00000000..b8dae09b --- /dev/null +++ b/src/deskstar-backend/Deskstar/Dockerfile_dev @@ -0,0 +1,20 @@ +FROM mcr.microsoft.com/dotnet/sdk:6.0 + +RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ + && apt-get -y install --no-install-recommends \ + postgresql + +RUN dotnet tool install --global dotnet-ef + + +WORKDIR /Deskstar +# Copy everything +COPY . ./ +# Restore as distinct layers +RUN dotnet restore +# Build and publish a release +RUN dotnet publish -c Dev -o out + +ENV PATH="${PATH}:~/.dotnet/tools/" + +CMD ["dotnet", "bin/Dev/net6.0/Deskstar.dll"] diff --git a/src/deskstar-frontend/Dockerfile b/src/deskstar-frontend/Dockerfile index 12824a5e..667c9d62 100644 --- a/src/deskstar-frontend/Dockerfile +++ b/src/deskstar-frontend/Dockerfile @@ -1,11 +1,57 @@ -FROM node:16.18 +# Install dependencies only when needed +FROM node:16-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 + +# Install dependencies based on the preferred package manager +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ +RUN \ + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ + elif [ -f package-lock.json ]; then npm ci; \ + elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \ + else echo "Lockfile not found." && exit 1; \ + fi + + +# Rebuild the source code only when needed +FROM node:16-alpine AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +# Next.js collects completely anonymous telemetry data about general usage. +# Learn more here: https://nextjs.org/telemetry +# Uncomment the following line in case you want to disable telemetry during the build. +ENV NEXT_TELEMETRY_DISABLED 1 +RUN yarn build + +# If using npm comment out above and use below instead +# RUN npm run build + +# Production image, copy all the files and run next +FROM node:16-alpine AS runner WORKDIR /app -COPY ./package.json . +ENV NODE_ENV production +# Uncomment the following line in case you want to disable telemetry during runtime. +ENV NEXT_TELEMETRY_DISABLED 1 + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +COPY --from=builder /app/public ./public + +# Automatically leverage output traces to reduce image size +# https://nextjs.org/docs/advanced-features/output-file-tracing +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs -RUN ["yarn", "install"] +EXPOSE 3000 -ENV PATH=/app/node_modules/.bin:$PATH +ENV PORT 3000 -WORKDIR /app/code \ No newline at end of file +CMD ["node", "server.js"] diff --git a/src/deskstar-frontend/Dockerfile_production b/src/deskstar-frontend/Dockerfile_production deleted file mode 100644 index cac3e7b9..00000000 --- a/src/deskstar-frontend/Dockerfile_production +++ /dev/null @@ -1,11 +0,0 @@ -FROM node:16.18 - -WORKDIR /app - -COPY . . - -RUN ["yarn", "install"] - -RUN ["yarn", "run", "build"] - -CMD ["yarn", "run", "start"] \ No newline at end of file diff --git a/src/deskstar-frontend/next.config.js b/src/deskstar-frontend/next.config.js index ae887958..e7165694 100644 --- a/src/deskstar-frontend/next.config.js +++ b/src/deskstar-frontend/next.config.js @@ -2,6 +2,7 @@ const nextConfig = { reactStrictMode: true, swcMinify: true, + output: "standalone" } module.exports = nextConfig diff --git a/src/deskstar-frontend/yarn.lock b/src/deskstar-frontend/yarn.lock index 4070ea32..64b46856 100644 --- a/src/deskstar-frontend/yarn.lock +++ b/src/deskstar-frontend/yarn.lock @@ -496,6 +496,11 @@ client-only@0.0.1, client-only@^0.0.1: resolved "https://registry.yarnpkg.com/client-only/-/client-only-0.0.1.tgz#38bba5d403c41ab150bff64a95c85013cf73bca1" integrity sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA== +clsx@^1.1.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/clsx/-/clsx-1.2.1.tgz#0ddc4a20a549b59c93a4116bb26f5294ca17dc12" + integrity sha512-EcR6r5a8bj6pu3ycsa/E/cKVGuTgZJZdsyUYHOksG/UHIiKfjxzRxYJpyVBwYaQeOvghal9fcc4PidlgzugAQg== + color-convert@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-2.0.1.tgz#72d3a68d598c9bdb3af2ad1e84f21d896abd4de3" @@ -1822,6 +1827,13 @@ react-is@^16.13.1: resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4" integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ== +react-toastify@^9.1.1: + version "9.1.1" + resolved "https://registry.yarnpkg.com/react-toastify/-/react-toastify-9.1.1.tgz#9280caea4a13dc1739c350d90660a630807bf10b" + integrity sha512-pkFCla1z3ve045qvjEmn2xOJOy4ZciwRXm1oMPULVkELi5aJdHCN/FHnuqXq8IwGDLB7PPk2/J6uP9D8ejuiRw== + dependencies: + clsx "^1.1.1" + react@18.2.0: version "18.2.0" resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz#555bd98592883255fa00de14f1151a917b5d77d5" diff --git a/src/docker-compose.yml b/src/docker-compose.yml new file mode 100644 index 00000000..cec03588 --- /dev/null +++ b/src/docker-compose.yml @@ -0,0 +1,78 @@ +services: + frontend: + container_name: deskstar_frontend + # user: "${USER_ID}:${GROUP_ID}" # prevent root files in host system + build: + context: ./deskstar-frontend + ports: + - 3000:3000 + environment: + - BACKEND_URL + - NEXTAUTH_SECRET + - NEXTAUTH_URL + # - HOST=0.0.0.0 + networks: + - external + - internal + backend: + container_name: deskstar_backend + # user: "${USER_ID}:${GROUP_ID}" + depends_on: + - "postgres" + build: + context: ./deskstar-backend/Deskstar + ports: + - 3001:80 + environment: + - ASPNETCORE_ENVIRONMENT + - DB__HOST + - DB__DATABASE + - DB__USERNAME + - DB__PASSWORD + # defined in .env.local (will no be pushed to git) + - EMAIL__HOST + - EMAIL__PORT + - EMAIL__ADDRESS + - EMAIL__PSW + networks: + - internal + dbpreparation: + container_name: deskstar_db_prep + build: + context: ./deskstar-backend/Deskstar + dockerfile: Dockerfile_dev + depends_on: + - "postgres" + environment: + # - ASPNETCORE_ENVIRONMENT + - DB__HOST + - DB__DATABASE + - DB__USERNAME + - DB__PASSWORD + # defined in .env.local (will no be pushed to git) + - EMAIL__HOST + - EMAIL__PORT + - EMAIL__ADDRESS + - EMAIL__PSW + networks: + - internal + command: dotnet ef database update + postgres: + container_name: deskstar_postgres + image: postgres + user: "postgres" + environment: + - POSTGRES_USER + - POSTGRES_DB + - POSTGRES_PASSWORD + # volumes: + # - ./deskstar-db/dummy-data.sql:/docker-entrypoint-initdb.d/dummy-data.sql + ports: + - 5433:5432 + networks: + - internal +volumes: + exclude: +networks: + internal: + external: diff --git a/src/start.sh b/src/start.sh index f42194c4..97fef4fa 100755 --- a/src/start.sh +++ b/src/start.sh @@ -1,3 +1,4 @@ -export USER_ID="$(id -u)" -export GROUP_ID="$(id -g)" -docker compose --env-file .env up --build \ No newline at end of file +export DOCKER_BUILDKIT=0 +export COMPOSE_DOCKER_CLI_BUILD=0 + +docker compose --env-file .env.local up --build From 3299113f7ab2af970e84ff4b0ca39f64b6b22e9c Mon Sep 17 00:00:00 2001 From: Moritz Reich Date: Mon, 16 Jan 2023 09:33:15 +0000 Subject: [PATCH 2/4] changed to a more secure way for email password Signed-off-by: Moritz Reich --- .devcontainer/docker-compose.yml | 2 +- .gitignore | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml index ebb7e7b8..081bbd0c 100644 --- a/.devcontainer/docker-compose.yml +++ b/.devcontainer/docker-compose.yml @@ -23,7 +23,7 @@ services: EMAIL__HOST: smtp.strato.de EMAIL__PORT: 587 EMAIL__ADDRESS: noreply@deskstar.de - EMAIL__PSW: 123456 + EMAIL__PSW: ${EMAIL__PSW} ASPNETCORE_ENVIRONMENT: Development # Uncomment the next line to use a non-root user for all processes. diff --git a/.gitignore b/.gitignore index 17c5bbcd..689fb0be 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.devcontainer/.env + # Node logs npm-debug.log* From 01bafd76496a61e6aa9706a3abd8ba199637e1a5 Mon Sep 17 00:00:00 2001 From: Moritz Reich Date: Mon, 16 Jan 2023 09:34:02 +0000 Subject: [PATCH 3/4] removed comments Signed-off-by: Moritz Reich --- src/docker-compose.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/docker-compose.yml b/src/docker-compose.yml index cec03588..8a8328e0 100644 --- a/src/docker-compose.yml +++ b/src/docker-compose.yml @@ -1,7 +1,6 @@ services: frontend: container_name: deskstar_frontend - # user: "${USER_ID}:${GROUP_ID}" # prevent root files in host system build: context: ./deskstar-frontend ports: @@ -10,13 +9,11 @@ services: - BACKEND_URL - NEXTAUTH_SECRET - NEXTAUTH_URL - # - HOST=0.0.0.0 networks: - external - internal backend: container_name: deskstar_backend - # user: "${USER_ID}:${GROUP_ID}" depends_on: - "postgres" build: @@ -65,8 +62,6 @@ services: - POSTGRES_USER - POSTGRES_DB - POSTGRES_PASSWORD - # volumes: - # - ./deskstar-db/dummy-data.sql:/docker-entrypoint-initdb.d/dummy-data.sql ports: - 5433:5432 networks: From 476105c3c1b68ebb15f224c75406fa2899d4c20a Mon Sep 17 00:00:00 2001 From: xilef45 Date: Tue, 17 Jan 2023 15:19:56 +0100 Subject: [PATCH 4/4] Update Dockerfile Co-Authored-By: Moritz Reich <16562869+n3rdc4ptn@users.noreply.github.com> --- src/deskstar-frontend/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/deskstar-frontend/Dockerfile b/src/deskstar-frontend/Dockerfile index 667c9d62..e6884972 100644 --- a/src/deskstar-frontend/Dockerfile +++ b/src/deskstar-frontend/Dockerfile @@ -17,8 +17,8 @@ RUN \ # Rebuild the source code only when needed FROM node:16-alpine AS builder WORKDIR /app -COPY --from=deps /app/node_modules ./node_modules COPY . . +COPY --from=deps /app/node_modules ./node_modules # Next.js collects completely anonymous telemetry data about general usage. # Learn more here: https://nextjs.org/telemetry