From d98c31f7480cb5d71cece2090d11bec512a63b02 Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Sun, 1 May 2022 12:12:00 +0200 Subject: [PATCH] Improve project setup with Docker --- Dockerfile | 17 ++++++++++++++++- README.md | 38 ++++++++++++++------------------------ docker-compose.yml | 6 ++++-- entrypoint.sh | 35 +++++++++++++++++++++++++++++++++++ package.json | 2 +- 5 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 1352dbc..5d3229e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,14 @@ COPY . . COPY --from=deps /opt/app/node_modules ./node_modules RUN npm run build +FROM node:16-alpine AS script-builder +WORKDIR /opt/app +COPY .setup .setup +WORKDIR /opt/app/.setup +RUN npm install +RUN npm i -g @vercel/ncc +RUN ncc build index.ts + # Production image, copy all the files and run next FROM node:16-alpine AS runner WORKDIR /opt/app @@ -19,6 +27,13 @@ COPY --from=builder /opt/app/next.config.js ./ COPY --from=builder /opt/app/public ./public COPY --from=builder /opt/app/.next ./.next COPY --from=builder /opt/app/node_modules ./node_modules -COPY ./.env ./ +COPY --from=script-builder /opt/app/.setup/dist/index.js ./scripts/setup.js + +COPY .env.example .env +COPY entrypoint.sh . +RUN apk add --no-cache --upgrade bash +RUN ["chmod", "+x", "./entrypoint.sh"] +ENTRYPOINT ["./entrypoint.sh"] + EXPOSE 3000 CMD ["node_modules/.bin/next", "start"] \ No newline at end of file diff --git a/README.md b/README.md index a9a5e76..711b024 100644 --- a/README.md +++ b/README.md @@ -20,39 +20,29 @@ Pingvin Share uses Appwrite as backend. You have to install and setup Appwrite f 2. Create an Account on your Appwrite instance 3. Change the `_APP_STORAGE_LIMIT` variable in the `.env` file of Appwrite to your prefered max size limit per share -### 2. Setup script +### 2. Frontend -To setup the backend structure of Pingvin Share you have to run the setup script. +First of all you have to start the Docker container. -1. [Install Node](https://nodejs.org/en/download/) -2. Clone the repository with `git clone https://github.com/stonith404/pingvin-share` -3. Visit the repository directory with `cd pingvin-share` -4. Run `npm run init:appwrite` +1. Clone the `docker-compose.yml` file and the `.env.example` file from this repository +2. Rename the `.env.example` file to `.env` +3. Start the container with `docker-compose up -d` -### 3. Frontend +The container is now running. Now you have to setup the Appwrite structure, but no worries I made a setup script. -To set up the frontend of Pingvin Share follow these steps. +To start the script run `docker-compose exec pingvin-share node scripts/setup.js`. + +You're almost done, now you have to change your environment variables that they fit to your setup. 1. Go to your Appwrite console, visit "API Keys" and copy the "Functions API Key" secret to your clipboard. -2. Rename the `.env.example` file to `.env` -3. Paste the key in the `.env` file -4. Change `NEXT_PUBLIC_APPWRITE_HOST` in the `.env` file to the host where your Appwrite instance runs -5. Change `NEXT_PUBLIC_MAX_FILE_SIZE` in the `.env` file to the max file size limit you want - -Start the frontend: - -With docker: - -1. Run `docker-compose up -d --build` - -Without docker: - -1. Run `npm install` -2. Run `npm run build && npm run start` - +2. Paste the key to the `APPWRITE_FUNCTION_API_KEY` variable in the `.env` file +3. Change `NEXT_PUBLIC_APPWRITE_HOST` in the `.env` file to the host where your Appwrite instance runs +4. Change `NEXT_PUBLIC_MAX_FILE_SIZE` in the `.env` file to the max file size limit you want ## Known issues / Limitations + Pingvin Share is currently in beta and there are issues and limitations that should be fixed in the future. + - `DownloadAll` generates the zip file on the client side. This takes alot of time. Because of that I temporarily limited this function to maximal 150 MB. - If a user knows the share id, he can list and download the files directly from the Appwrite API even if the share is secured by a password or a visitor limit. diff --git a/docker-compose.yml b/docker-compose.yml index 6749469..73175a1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,5 +4,7 @@ services: ports: - '3000:3000' image: stonith404/pingvin-share - build: - context: ./ \ No newline at end of file + environment: + - APPWRITE_FUNCTION_API_KEY=${APPWRITE_FUNCTION_API_KEY} + - NEXT_PUBLIC_APPWRITE_HOST=${NEXT_PUBLIC_APPWRITE_HOST} + - NEXT_PUBLIC_MAX_FILE_SIZE=${NEXT_PUBLIC_MAX_FILE_SIZE} \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..93baeb8 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# This script is used to inject env variables in Docker after the build. +# Normally the env variables get injecten while the build. This means, you can't add env variables when you start the conatainer. +# Source of the script: https://raphaelpralat.medium.com/system-environment-variables-in-next-js-with-docker-1f0754e04cde + +set +x + +envFilename='.env' +nextFolder='./.next/' +function apply_path { + # read all config file + while read line; do + # no comment or not empty + if [ "${line:0:1}" == "#" ] || [ "${line}" == "" ]; then + continue + fi + + # split + configName="$(cut -d'=' -f1 <<<"$line")" + configValue="$(cut -d'=' -f2 <<<"$line")" + # get system env + envValue=$(env | grep "^$configName=" | grep -oe '[^=]*$'); + + # if config found + if [ -n "$configValue" ] && [ -n "$envValue" ]; then + # replace all + echo "Replace: ${configValue} with: ${envValue}" + find $nextFolder \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#$configValue#$envValue#g" + fi + done < $envFilename + } + +apply_path +echo "Starting Nextjs" +exec "$@" \ No newline at end of file diff --git a/package.json b/package.json index edee07d..b476306 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "start": "next start", "lint": "next lint", "init:appwrite": "cd .setup && npm install && npx ts-node index.ts", - "deploy": "docker build -t git.elias.li/stonith404/pingvin-share:latest . && docker push git.elias.li/stonith404/pingvin-share:latest" + "deploy": "docker buildx build -t stonith404/pingvin-share --platform linux/amd64,linux/arm64 --push ." }, "dependencies": { "@mantine/core": "^4.2.0",