From 2f966d324e00df75deb574636e9f970a7fa682fb Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Tue, 20 Feb 2024 21:23:28 +0000 Subject: [PATCH] docker: Add a docker-based dev environment --- .github/workflows/docker.yml | 5 +- .github/workflows/main.yml | 1 + README.md | 13 +++++ Dockerfile => docker/Dockerfile | 8 +-- docker/Dockerfile.dev | 11 ++++ docker/docker-compose.dev.yml | 58 +++++++++++++++++++ .../docker-compose.yml | 1 + docker/start-chrome.sh | 7 +++ docker/yarn.lock | 4 ++ 9 files changed, 100 insertions(+), 8 deletions(-) rename Dockerfile => docker/Dockerfile (88%) create mode 100644 docker/Dockerfile.dev create mode 100644 docker/docker-compose.dev.yml rename docker-compose.yml => docker/docker-compose.yml (97%) create mode 100644 docker/start-chrome.sh create mode 100644 docker/yarn.lock diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 907bb4bc..7c0fa6c7 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -14,9 +14,10 @@ jobs: - uses: actions/checkout@v4 - name: Build image + working-directory: ./docker run: | - docker build . --target workers --tag hoarder-workers - docker build . --target web --tag hoarder-web + docker build .. --target workers --tag hoarder-workers -f Dockerfile + docker build .. --target web --tag hoarder-web -f Dockerfile - name: Log in to registry # This is where you will update the PAT to GITHUB_TOKEN diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 44c238f4..28bb13fb 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -3,6 +3,7 @@ on: push: branches: - main + pull_request: jobs: build-lint: name: Build and Lint diff --git a/README.md b/README.md index 4a33cb9e..a32fa90f 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,15 @@ I'm a systems engineer in my day job (and have been for the past 7 years). I did ## Development +### Docker + +You can turnup the whole development environment with: +`docker compose -f docker/docker-compose.dev.yml up` + +### Manual + +Or if you have nodejs installed locally, you can do: + - `yarn install` in the root of the repo. - `yarn prisma:migrate:dev` then `yarn prisma:generate` to generate prisma's data and run the database migrations. - `yarn web` to start the web app. @@ -75,3 +84,7 @@ I'm a systems engineer in my day job (and have been for the past 7 years). I did - `packages/shared`: Shared utilities and code between the workers and the web app. - `packages/web`: Where the nextjs based web app lives. - `packages/workers`: Where the background job workers (crawler and openai as of now) run. + +### Submitting PRs + +- Before submitting PRs, you'll want to run `yarn format` and include its changes in the commit. Also make sure `yarn lint` is successful. diff --git a/Dockerfile b/docker/Dockerfile similarity index 88% rename from Dockerfile rename to docker/Dockerfile index fc1950b4..1e3bcfc4 100644 --- a/Dockerfile +++ b/docker/Dockerfile @@ -82,12 +82,8 @@ ENV CHROME_PATH "/usr/bin/chromium-browser" ENV BROWSER_EXECUTABLE_PATH "/app/start-chrome.sh" ENV BROWSER_USER_DATA_DIR="/tmp/chrome" -RUN echo '#!/bin/sh' >> /app/start-chrome.sh && \ - echo 'set -x;' >> /app/start-chrome.sh && \ - echo 'id -u chrome &>/dev/null || adduser -S chrome' >> /app/start-chrome.sh && \ - echo 'mkdir -p /tmp/chrome && chown chrome /tmp/chrome' >> /app/start-chrome.sh && \ - echo 'runuser -u chrome -- $CHROME_PATH $@' >> /app/start-chrome.sh && \ - chmod +x /app/start-chrome.sh +ADD start-chrome.sh . +RUN chmod +x /app/start-chrome.sh COPY packages/db packages/db COPY packages/shared packages/shared diff --git a/docker/Dockerfile.dev b/docker/Dockerfile.dev new file mode 100644 index 00000000..9a8de32b --- /dev/null +++ b/docker/Dockerfile.dev @@ -0,0 +1,11 @@ +FROM node:21-alpine + +RUN apk add --no-cache libc6-compat chromium runuser make g++ py3-pip linux-headers +ENV PUPPETEER_SKIP_DOWNLOAD true +ENV CHROME_PATH "/usr/bin/chromium-browser" +ENV BROWSER_EXECUTABLE_PATH "/bin/start-chrome.sh" +ENV BROWSER_USER_DATA_DIR="/tmp/chrome" + +WORKDIR /app +ADD start-chrome.sh /bin +RUN chmod +x /bin/start-chrome.sh diff --git a/docker/docker-compose.dev.yml b/docker/docker-compose.dev.yml new file mode 100644 index 00000000..a6510939 --- /dev/null +++ b/docker/docker-compose.dev.yml @@ -0,0 +1,58 @@ +version: "3.8" +services: + web: + build: + dockerfile: Dockerfile.dev + volumes: + - data:/data + - ..:/app + ports: + - 3000:3000 + environment: + REDIS_HOST: redis + DATABASE_URL: "file:/data/db.db" + command: + - yarn + - web + working_dir: /app + depends_on: + prep: + condition: service_completed_successfully + redis: + image: redis:7.2-alpine + volumes: + - redis:/data + workers: + build: + dockerfile: Dockerfile.dev + volumes: + - data:/data + - ..:/app + working_dir: /app + environment: + REDIS_HOST: redis + DATABASE_URL: "file:/data/db.db" + # OPENAI_API_KEY: ... + command: + - yarn + - workers + depends_on: + prep: + condition: service_completed_successfully + prep: + build: + dockerfile: Dockerfile.dev + working_dir: /app + environment: + DATABASE_URL: "file:/data/db.db" + volumes: + - data:/data + - ..:/app + command: + - /bin/sh + - -c + - "corepack enable && yarn install --immutable && yarn prisma:generate && yarn prisma:migrate:dev" + +volumes: + redis: + data: diff --git a/docker-compose.yml b/docker/docker-compose.yml similarity index 97% rename from docker-compose.yml rename to docker/docker-compose.yml index 69c5fead..a0cc7366 100644 --- a/docker-compose.yml +++ b/docker/docker-compose.yml @@ -23,6 +23,7 @@ services: environment: REDIS_HOST: redis DATABASE_URL: "file:/data/db.db" + # OPENAI_API_KEY: ... depends_on: migration: condition: service_completed_successfully diff --git a/docker/start-chrome.sh b/docker/start-chrome.sh new file mode 100644 index 00000000..9f715906 --- /dev/null +++ b/docker/start-chrome.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +set -x; +id -u chrome &>/dev/null || adduser -S chrome; +mkdir -p $BROWSER_USER_DATA_DIR; +chown chrome $BROWSER_USER_DATA_DIR; +runuser -u chrome -- $CHROME_PATH --no-sandbox $@; diff --git a/docker/yarn.lock b/docker/yarn.lock new file mode 100644 index 00000000..fb57ccd1 --- /dev/null +++ b/docker/yarn.lock @@ -0,0 +1,4 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + +