From 407cdc77c496e7bcfd820f7f245e4cf7f057e4b2 Mon Sep 17 00:00:00 2001 From: CJ Keeney Date: Sat, 22 Oct 2022 12:35:09 -0700 Subject: [PATCH 1/4] add dockerfile and docs --- .dockerignore | 34 ++++++++++++++++++++++ .gitignore | 1 + Dockerfile | 37 ++++++++++++++++++++++++ README.md | 9 ++++++ docs/docker.md | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docs/docker.md diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..db4c651 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,34 @@ +.DS_Store +node_modules +/dist + +config/config.js +config/internalConfig.json +config/sources/ +config/user/ + +logs/ + +docker/.env +docker/volumes/ + +# local env files +.env +.env.local +.env.*.local +.envrc + +# Log files +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* + +# Editor directories and files +.idea +.vscode +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/.gitignore b/.gitignore index 6bdfe69..a8ab20a 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ docker/.env docker/volumes/ # local env files +.envrc .env .env.local .env.*.local diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5a25e34 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +# STAGE: INSTALL DEPENDENCIES +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 +COPY package.json package-lock.json ./ +RUN npm i + +# STAGE: BUILD ARTIFACTS +FROM node:16-alpine AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . +COPY config/config.example.js ./config/config.js +RUN npm run build + +# STAGE: RUN +FROM node:16-alpine AS runner +WORKDIR /app + +ENV NODE_ENV production + +#RUN addgroup --system --gid 5001 nodejs +#RUN adduser --system --uid 5001 nodejs +USER node + +COPY --from=builder /app/node_modules ./node_modules +COPY --from=builder /app/dist ./dist +COPY . . + + +VOLUME ['/app/logs', '/app/config'] +EXPOSE 3000 + +ENV PORT 3000 + +CMD ["node", "server/index.js"] diff --git a/README.md b/README.md index 52b48f2..d63e92d 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,16 @@ See all the cron schedules at once, which makes it easier to not overlap them: ![See all the cron schedules at once](https://user-images.githubusercontent.com/366538/141699270-975690d3-7bea-46d7-a8ad-83813a5298f4.png) ## How do I use it? +### Docker +You can run this in docker with +```shell +docker run --init -v ${PWD}/absolute/path/to/config.js:/app/config/config.js -p 4000:4000 ckeeney/openresync +``` + +See our [Docker guide](docs/docker.md) for a more information on running this using docker and docker-compose. + +### Natively Install the app, configure it, build it, start the back-end server (and optionally start the dev web server), and visit the local website that runs. These are described in the following steps. ### Installation diff --git a/docs/docker.md b/docs/docker.md new file mode 100644 index 0000000..ec7762d --- /dev/null +++ b/docs/docker.md @@ -0,0 +1,78 @@ +# Docker + +This project is published on Docker Hub at [`ckeeney/openresync`](https://hub.docker.com/repository/docker/ckeeney/openresync). + +A [config file](https://github.com/tylercollier/openresync#configuration) must be mounted into the container at `/app/config/config.js`. If you mount a whole directory at `/app/config/`, you will have easy access to the files written to `/app/config` by [`openresync`](https://github.com/tylercollier/openresync). Additionally, you can mount a directory at `/app/logs` to have easy access to the [`openresync`](https://github.com/tylercollier/openresync) logs. + +## Permissions +By default, the node process runs as the `node` user, with `UID = 1000` and `GID = 1000`. The container-user running the process must have write-access to the files and folders mounted into the container. This can be accomplished in at least two ways: + +### 1. Change user that runs the node process +Changing the user that runs the node process to match your host user ensures you can still easily edit the config file without juggling permissions. + +First find your own `UID` and `GID`: +```shell +echo $UID : $GID +1000 : 1001 +``` + +Tell docker or `docker-compose` to use these UID and GID to run the process. With docker, you can do this by adding `-u 1000:1001` to your run command. With `docker-compose`, add a `user: 1000:1001` property to the service. + +### 2. Change ownership on the host +On the host, you can run +```shell +chown -R ./volumes/openresync/config 1000:1000 +``` + +You may lose write access to the config file if you do this. + +If `UID` or `GID` 1000 exist on your host machine, you will see the username and groupname that have `ID = 1000`. + +If you do not have a user or group on your machine with `ID = 1000`, you will just see `1000:1000` as the owner of the files and directories. + + +## Example docker-compose +```yaml +version: '3.7' + openresync: + image: ckeeney/openresync + restart: always + + # properly handles kernel signals for faster restarts and shutdowns. + # see https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md#handling-kernel-signals + init: true + + # run as my UID/GID + user: 1000:1001 + + # expose web service + ports: + - 4000:4000 + # pass in some data to my own config file + environment: + TRESTLE_CLIENT_ID: + TRESTLE_CLIENT_SECRET: + DB_CONN_STRING_TRESTLE: mysql://devuser:devpass@mysql:3306/re-data + DB_CONN_STRING_STATS: mysql://devuser:devpass@mysql:3306/re-data + + # mount the directories + volumes: + - ./volumes/openresync/config:/app/config + - ./volumes/openresync/logs:/app/logs + depends_on: + - mysql + + # any db provider + mysql: + image: mysql:8.0 + restart: always + ports: + - 3306:3306 + environment: + MYSQL_RANDOM_ROOT_PASSWORD: yespls + MYSQL_DATABASE: re-data + MYSQL_USER: devuser + MYSQL_PASSWORD: devpass + volumes: + - ./volumes/mysql:/var/lib/mysql +``` From f3eefb3df3f08bd00ce9aaffeae389b6fdcd2c5a Mon Sep 17 00:00:00 2001 From: CJ Keeney Date: Sat, 22 Oct 2022 14:08:20 -0700 Subject: [PATCH 2/4] gitlab-ci default template --- .gitlab-ci.yml | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..996325d --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,38 @@ +# This file is a template, and might need editing before it works on your project. +# To contribute improvements to CI/CD templates, please follow the Development guide at: +# https://docs.gitlab.com/ee/development/cicd/templates.html +# This specific template is located at: +# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Docker.gitlab-ci.yml + +# Build a Docker image with CI/CD and push to the GitLab registry. +# Docker-in-Docker documentation: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html +# +# This template uses one generic job with conditional builds +# for the default branch and all other (MR) branches. + +docker-build: + # Use the official docker image. + image: docker:latest + stage: build + services: + - docker:dind + before_script: + - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY + # Default branch leaves tag empty (= latest tag) + # All other branches are tagged with the escaped branch name (commit ref slug) + script: + - | + if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then + tag="" + echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'" + else + tag=":$CI_COMMIT_REF_SLUG" + echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag" + fi + - docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" . + - docker push "$CI_REGISTRY_IMAGE${tag}" + # Run this job in a branch where a Dockerfile exists + rules: + - if: $CI_COMMIT_BRANCH + exists: + - Dockerfile From 719075a6220d80ebe3a421d02a5f9f3ff0bbc2e1 Mon Sep 17 00:00:00 2001 From: CJ Keeney Date: Sat, 22 Oct 2022 14:17:53 -0700 Subject: [PATCH 3/4] trigger build maybe --- noop | 1 + 1 file changed, 1 insertion(+) create mode 100644 noop diff --git a/noop b/noop new file mode 100644 index 0000000..88bc7d7 --- /dev/null +++ b/noop @@ -0,0 +1 @@ +just trigger a build maybe From 8e6abb52f581089700382c6a4151bc0456bfc997 Mon Sep 17 00:00:00 2001 From: CJ Keeney Date: Sat, 22 Oct 2022 14:54:35 -0700 Subject: [PATCH 4/4] delete noop file --- noop | 1 - 1 file changed, 1 deletion(-) delete mode 100644 noop diff --git a/noop b/noop deleted file mode 100644 index 88bc7d7..0000000 --- a/noop +++ /dev/null @@ -1 +0,0 @@ -just trigger a build maybe