From 88608f2c5151e8629725c4e9468a27ceed15c916 Mon Sep 17 00:00:00 2001 From: n9mi Date: Thu, 12 Sep 2024 01:34:40 +0700 Subject: [PATCH] fix: docker --- .env.example | 17 +++++++++++++++++ .github/workflows/publish.yml | 27 ++++++++++++++++++++++++++- .gitignore | 2 ++ Dockerfile | 26 ++++++++++++++++++++++++++ docker-compose.yml | 25 ++++++++++++++++++++++++- package.json | 5 +++-- prisma/schema.prisma | 1 + 7 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 .env.example create mode 100644 Dockerfile diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..f3142fb --- /dev/null +++ b/.env.example @@ -0,0 +1,17 @@ +# Environment variables declared in this file are automatically made available to Prisma. +# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema + +# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. +# See the documentation for all the connection string options: https://pris.ly/d/connection-strings + +DATABASE_URL="postgresql://postgres:postgre@db:5432/contact_db?schema=public&connect_timeout=300" + +DB_DATABASE=contact_db +DB_USERNAME=postgres +DB_PASSWORD=postgre + +PORT=3000 +BASE_URL_PATH=/api/v1 + +JWT_SECRET=secret-key +JWT_EXPIRED_IN_MINUTES=100c \ No newline at end of file diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 9c6ca4c..51ad837 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -60,4 +60,29 @@ jobs: run: npm run db-migrate - name: Run test - run: npm test \ No newline at end of file + run: npm test + + push_to_Docker_Hub: + runs-on: ubuntu-latest + needs: [test] + + steps: + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v2 + with: + context: ./ + file: ./Dockerfile + push: true + tags: ${{ secrets.DOCKERHUB_USERNAME }}/contact-api:latest \ No newline at end of file diff --git a/.gitignore b/.gitignore index 11ddd8d..b752f9d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules # Keep environment variables out of version control .env + +dist \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ef86413 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM node:20 AS builder + +# Create app directory +WORKDIR /app + +# A wildcard is used to ensure both package.json AND package-lock.json are copied +COPY package*.json ./ +COPY prisma ./prisma/ + +# Install app dependencies +RUN npm install + +COPY . . + +RUN npm run build + +FROM node:20 + +COPY --from=builder /app/node_modules ./node_modules +COPY --from=builder /app/package*.json ./ +COPY --from=builder /app/dist ./dist +COPY --from=builder /app/prisma ./prisma + +EXPOSE 3000 + +CMD [ "npm", "run", "start:migrate:prod" ] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 656e9c3..a05fa83 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,29 @@ services: - POSTGRES_DB=${DB_DATABASE} - POSTGRES_USER=${DB_USERNAME} - POSTGRES_PASSWORD=${DB_PASSWORD} + networks: + - contact_api_network + contact-api: + environment: + - DATABASE_URL=${DATABASE_URL} + - PORT=${PORT} + - BASE_URL_PATH=${BASE_URL_PATH} + - JWT_SECRET=${JWT_SECRET} + - JWT_EXPIRED_IN_MINUTES=${JWT_EXPIRED_IN_MINUTES} + build: . + image: contact-api + ports: + - '3000:3000' + depends_on: + - db + volumes: + - .:/usr/src/node-app + networks: + - contact_api_network volumes: - postgres-db: \ No newline at end of file + postgres-db: + +networks: + contact_api_network: + driver: bridge \ No newline at end of file diff --git a/package.json b/package.json index d49684d..ab61893 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,11 @@ "main": "index.js", "scripts": { "dev": "concurrently \"npx tsc --watch\" \"nodemon -q src/main.ts\"", - "start": "node dist/src/main.js", + "start": "node dist/main.js", "build": "tsc", "test": "jest --runInBand", - "db-migrate": "npx prisma migrate deploy" + "migrate": "prisma migrate deploy", + "start:migrate:prod": "prisma migrate deploy && node dist/main.js" }, "jest": { "transform": { diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 8ef343d..082b6af 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -6,6 +6,7 @@ generator client { provider = "prisma-client-js" + binaryTargets = [ "native", "linux-musl-openssl-3.0.x" ] } datasource db {