Skip to content

Commit

Permalink
fix: docker
Browse files Browse the repository at this point in the history
  • Loading branch information
n9mi committed Sep 11, 2024
1 parent a6298a7 commit 88608f2
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 4 deletions.
17 changes: 17 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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
27 changes: 26 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,29 @@ jobs:
run: npm run db-migrate

- name: Run test
run: npm test
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
# Keep environment variables out of version control
.env

dist
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
25 changes: 24 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
postgres-db:

networks:
contact_api_network:
driver: bridge
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

generator client {
provider = "prisma-client-js"
binaryTargets = [ "native", "linux-musl-openssl-3.0.x" ]
}

datasource db {
Expand Down

0 comments on commit 88608f2

Please sign in to comment.