diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e69de29 diff --git a/.gitignore b/.gitignore index df4fb7c..63c1643 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ postgres-data/ node_modules/ .idea/ .DS_Store -/.env \ No newline at end of file +.env \ No newline at end of file diff --git a/backend/Dockerfile.prod b/backend/Dockerfile.prod new file mode 100644 index 0000000..1cf7e5a --- /dev/null +++ b/backend/Dockerfile.prod @@ -0,0 +1,27 @@ +FROM node:lts-alpine as builder + +WORKDIR /app + +COPY tsconfig.json tsconfig.json +COPY package.json package.json + +RUN npm install +RUN npm install -g typescript + +COPY src src + +RUN tsc + +FROM node:lts-alpine + +RUN apk --no-cache add curl + +WORKDIR /app + +COPY --from=builder /app/dist /app/dist + +COPY package.json package.json + +RUN npm install --production + +CMD npm run startprod \ No newline at end of file diff --git a/backend/package.json b/backend/package.json index 4b250a2..835bc1c 100644 --- a/backend/package.json +++ b/backend/package.json @@ -5,7 +5,8 @@ "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "start": "ts-node-dev --poll ./src/index.ts" + "start": "ts-node-dev --poll ./src/index.ts", + "startprod": "node ./dist/index.js" }, "author": "", "license": "ISC", diff --git a/backend/src/config/db.ts b/backend/src/config/db.ts index 682ceb7..3e10c65 100644 --- a/backend/src/config/db.ts +++ b/backend/src/config/db.ts @@ -14,7 +14,7 @@ export const dataSource = new DataSource({ port: 5432, username: "postgres", password: "postgres", - database: "wild-transfer", + database: "postgres", synchronize: true, logging: ["error", "query", "schema"], entities: [ diff --git a/docker-compose.build.prod.yml b/docker-compose.build.prod.yml new file mode 100644 index 0000000..7aaa1d9 --- /dev/null +++ b/docker-compose.build.prod.yml @@ -0,0 +1,65 @@ +services: + backend: + build: + context: ./backend + dockerfile: Dockerfile.prod + env_file: + - .env.production + healthcheck: + test: 'curl --fail --request POST --header ''content-type: application/json'' --url ''http://localhost:4000/graphql'' --data ''{"query":"query { __typename }"}'' || exit 1' + interval: 1s + timeout: 2s + retries: 100 + depends_on: + db: + condition: service_healthy + frontend: + build: ./frontend + depends_on: + backend: + condition: service_healthy + healthcheck: + test: "curl --fail --request GET --url 'http://localhost:5173' || exit 1" + interval: 1s + timeout: 2s + retries: 100 + db: + image: postgres + healthcheck: + test: ["CMD-SHELL", "pg_isready -d postgres -U postgres"] + interval: 1s + timeout: 2s + retries: 100 + environment: + POSTGRES_PASSWORD: postgres + volumes: + - ./init.sql:/docker-entrypoint-initdb.d/init.sql + - ./postgres-data/data:/var/lib/postgresql/data + adminer: + image: adminer + ports: + - "8080:8080" + files: + build: + context: ./files + dockerfile: Dockerfile.prod + healthcheck: + test: "curl --fail --request GET --url 'http://localhost:3000' || exit 1" + interval: 1s + timeout: 2s + retries: 100 + env_file: + - ./files/.env + apigateway: + image: nginx + volumes: + - ./nginx.conf:/etc/nginx/nginx.conf + depends_on: + backend: + condition: service_healthy + frontend: + condition: service_healthy + files: + condition: service_healthy + ports: + - "7002:80" diff --git a/files/Dockerfile.prod b/files/Dockerfile.prod new file mode 100644 index 0000000..faedebe --- /dev/null +++ b/files/Dockerfile.prod @@ -0,0 +1,31 @@ +FROM node:lts-alpine as builder + +RUN apk --no-cache add curl + +WORKDIR /app +RUN mkdir UPLOADS_DIR + +COPY tsconfig.json tsconfig.json + +COPY package.json package.json + +RUN npm i +RUN npm install -g typescript + +COPY src src + +RUN tsc + +FROM node:lts-alpine + +RUN apk --no-cache add curl + +WORKDIR /app + +COPY --from=builder /app/dist /app/dist + +COPY package.json package.json + +RUN npm install --production + +CMD npm run startprod \ No newline at end of file diff --git a/files/package.json b/files/package.json index 370de79..85346f9 100644 --- a/files/package.json +++ b/files/package.json @@ -5,7 +5,8 @@ "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "start": "ts-node-dev ./src/index.ts" + "start": "ts-node-dev ./src/index.ts", + "startprod": "node ./dist/index.js" }, "keywords": [], "author": "", diff --git a/files/tsconfig.json b/files/tsconfig.json index 1982893..616f0ea 100644 --- a/files/tsconfig.json +++ b/files/tsconfig.json @@ -52,7 +52,7 @@ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ - // "outDir": "./", /* Specify an output folder for all emitted files. */ + "outDir": "./dist", /* Specify an output folder for all emitted files. */ // "removeComments": true, /* Disable emitting comments. */ // "noEmit": true, /* Disable emitting files from a compilation. */ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ diff --git a/package.json b/package.json index 86bf928..dff7c2f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "index.js", "scripts": { "start": "docker compose up --build", - "e2e": "docker compose -f docker-compose.e2e.yml up --build --exit-code-from e2e" + "e2e": "docker compose -f docker-compose.e2e.yml up --build --exit-code-from e2e", + "build_prod_and_start": "docker compose -f docker-compose.build.prod.yml up --build" }, "author": "", "license": "ISC",