From 76427c9f130adb90f90cccc84b92dd49f5147ed5 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 11 Jan 2024 15:25:08 -0500 Subject: Docker container version (#39) * + Dockerfile and compose file + Scripts dir and startup script + Build image npm script * * Moves env to file * + Tags image with info from package.json * Moves image creation to script * Updates README * Update README.md Co-authored-by: Sebastien Castiel --------- Co-authored-by: Maxime Jacob Co-authored-by: Sebastien Castiel --- .gitignore | 2 +- Dockerfile | 17 +++++++++++++++++ README.md | 8 +++++++- compose.yaml | 24 ++++++++++++++++++++++++ container.env.example | 7 +++++++ package.json | 4 +++- scripts/build-image.sh | 8 ++++++++ scripts/image-startup.sh | 5 +++++ scripts/start-local-db.sh | 11 +++++++++++ start-local-db.sh | 11 ----------- 10 files changed, 83 insertions(+), 14 deletions(-) create mode 100644 Dockerfile create mode 100644 compose.yaml create mode 100644 container.env.example create mode 100755 scripts/build-image.sh create mode 100755 scripts/image-startup.sh create mode 100755 scripts/start-local-db.sh delete mode 100755 start-local-db.sh diff --git a/.gitignore b/.gitignore index 26fe5e3..8d8a4fc 100644 --- a/.gitignore +++ b/.gitignore @@ -27,7 +27,7 @@ yarn-error.log* # local env files .env*.local -.env +*.env # vercel .vercel diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0966017 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM node:slim + +EXPOSE 3000/tcp +WORKDIR /usr/app +COPY ./ ./ + +SHELL ["/bin/bash", "-c"] + +RUN apt update && \ + apt install openssl -y && \ + apt clean && \ + apt autoclean && \ + apt autoremove && \ + npm install --ignore-scripts && \ + npm install -g prisma + +ENTRYPOINT ["/bin/bash", "-c", "scripts/image-startup.sh"] diff --git a/README.md b/README.md index 05bc64b..2301307 100644 --- a/README.md +++ b/README.md @@ -35,10 +35,16 @@ The project is open to contributions. Feel free to open an issue or even a pull- 1. Clone the repository (or fork it if you intend to contribute) 2. `npm install` -3. Start a PostgreSQL server. You can run `./start-local-db.sh` if you don’t have a server already. +3. Start a PostgreSQL server. You can run `./scripts/start-local-db.sh` if you don’t have a server already. 4. Copy the file `.env.example` as `.env` 5. `npm run dev` +## Run in a container +1. Run `npm run build-image` to build the docker image from the Dockerfile +2. Copy the file `container.env.example` as `container.env` +3. Run `npm run start-container` to start the postgres and the spliit2 containers +3. You can access the app by browsing to http://localhost:3000 + ## License MIT, see [LICENSE](./LICENSE). diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..86a4656 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,24 @@ +services: + app: + image: spliit2:latest + ports: + - 3000:3000 + env_file: + - container.env + depends_on: + db: + condition: service_healthy + + db: + image: postgres:latest + ports: + - 5432:5432 + env_file: + - container.env + volumes: + - /var/lib/postgresql/data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 5s + timeout: 5s + retries: 5 diff --git a/container.env.example b/container.env.example new file mode 100644 index 0000000..ecfc160 --- /dev/null +++ b/container.env.example @@ -0,0 +1,7 @@ +# db +POSTGRES_PASSWORD=1234 + +# app +POSTGRES_PRISMA_URL=postgresql://postgres:${POSTGRES_PASSWORD}@db +POSTGRES_URL_NON_POOLING=postgresql://postgres:${POSTGRES_PASSWORD}@db +NEXT_PUBLIC_BASE_URL=http://localhost:3000 diff --git a/package.json b/package.json index 23bd680..fb15f06 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,9 @@ "build": "next build", "start": "next start", "lint": "next lint", - "postinstall": "prisma migrate deploy && prisma generate" + "postinstall": "prisma migrate deploy && prisma generate", + "build-image": "./scripts/build-image.sh", + "start-container": "docker compose --env-file container.env up" }, "dependencies": { "@hookform/resolvers": "^3.3.2", diff --git a/scripts/build-image.sh b/scripts/build-image.sh new file mode 100755 index 0000000..55ef4a4 --- /dev/null +++ b/scripts/build-image.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +SPLIIT_APP_NAME=$(node -p -e "require('./package.json').name") +SPLIIT_VERSION=$(node -p -e "require('./package.json').version") + +docker buildx build --no-cache -t ${SPLIIT_APP_NAME}:${SPLIIT_VERSION} -t ${SPLIIT_APP_NAME}:latest . + +docker image prune -f diff --git a/scripts/image-startup.sh b/scripts/image-startup.sh new file mode 100755 index 0000000..8a3a90a --- /dev/null +++ b/scripts/image-startup.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +prisma migrate deploy +prisma generate +npm run dev diff --git a/scripts/start-local-db.sh b/scripts/start-local-db.sh new file mode 100755 index 0000000..c97173d --- /dev/null +++ b/scripts/start-local-db.sh @@ -0,0 +1,11 @@ +result=$(docker ps | grep postgres) +if [ $? -eq 0 ]; +then + echo "postgres is already running, doing nothing" +else + echo "postgres is not running, starting it" + docker rm postgres --force + mkdir -p postgres-data + docker run --name postgres -d -p 5432:5432 -e POSTGRES_PASSWORD=1234 -v ./postgres-data:/var/lib/postgresql/data postgres + sleep 5 # Wait for postgres to start +fi diff --git a/start-local-db.sh b/start-local-db.sh deleted file mode 100755 index c97173d..0000000 --- a/start-local-db.sh +++ /dev/null @@ -1,11 +0,0 @@ -result=$(docker ps | grep postgres) -if [ $? -eq 0 ]; -then - echo "postgres is already running, doing nothing" -else - echo "postgres is not running, starting it" - docker rm postgres --force - mkdir -p postgres-data - docker run --name postgres -d -p 5432:5432 -e POSTGRES_PASSWORD=1234 -v ./postgres-data:/var/lib/postgresql/data postgres - sleep 5 # Wait for postgres to start -fi -- cgit v1.3