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* 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..e6884972 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 . . +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 +# 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/docker-compose.yml b/src/docker-compose.yml new file mode 100644 index 00000000..8a8328e0 --- /dev/null +++ b/src/docker-compose.yml @@ -0,0 +1,73 @@ +services: + frontend: + container_name: deskstar_frontend + build: + context: ./deskstar-frontend + ports: + - 3000:3000 + environment: + - BACKEND_URL + - NEXTAUTH_SECRET + - NEXTAUTH_URL + networks: + - external + - internal + backend: + container_name: deskstar_backend + 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 + 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