Skip to content

Commit

Permalink
chore: setup prod and dev docker with compose
Browse files Browse the repository at this point in the history
ref #229
  • Loading branch information
ygrishajev committed Jun 17, 2024
1 parent 9fa3060 commit b56cd48
Show file tree
Hide file tree
Showing 40 changed files with 374 additions and 659 deletions.
20 changes: 20 additions & 0 deletions .env.sandbox.docker-compose-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Common
AkashSandboxDatabaseCS=postgres://postgres:password@db:5432/cloudmos-akash-sandbox
UserDatabaseCS=postgres://postgres:password@db:5432/cloudmos-users
Network=sandbox
ActiveChain=akashSandbox

# Deploy Web
API_BASE_URL: http://api:3080
PROVIDER_PROXY_URL: http://provider-proxy:3040

# Stats Web
API_MAINNET_BASE_URL: http://api:3080
API_TESTNET_BASE_URL: http://api:3080
API_SANDBOX_BASE_URL: http://api:3080

# DB
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DBS_FOR_IMPORT: cloudmos-akash-sandbox
POSTGRES_USERS_DB: cloudmos-users
2 changes: 1 addition & 1 deletion .github/workflows/docker-build-deploy-web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
- name: Build the Docker image
if: steps.filter.outputs.deploy-web == 'true'
run: docker build -f docker/Dockerfile.deploy-web -t console-deploy-web .
run: npm run dc:build -- deploy-web
2 changes: 1 addition & 1 deletion .github/workflows/docker-build-indexer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
- name: Build the Docker image
if: steps.filter.outputs.indexer == 'true'
run: docker build -f docker/Dockerfile.indexer -t console-indexer .
run: npm run dc:build -- indexer
2 changes: 1 addition & 1 deletion .github/workflows/docker-build-landing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
- name: Build the Docker image for LANDING
if: steps.filter.outputs.landing == 'true'
run: docker build -f docker/Dockerfile.landing -t console-landing .
run: npm run dc:build -- landing
2 changes: 1 addition & 1 deletion .github/workflows/docker-build-provider-proxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
- name: Build the Docker image
if: steps.filter.outputs.provider-proxy == 'true'
run: docker build -f docker/Dockerfile.provider-proxy -t console-provider-proxy .
run: npm run dc:build -- provider-proxy
2 changes: 1 addition & 1 deletion .github/workflows/docker-build-stats-web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
- name: Build the Docker image
if: steps.filter.outputs.stats-web == 'true'
run: docker build -f docker/Dockerfile.stats-web -t console-stats-web .
run: npm run dc:build -- stats-web
2 changes: 1 addition & 1 deletion .github/workflows/validate-n-build-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ jobs:

- name: Build the Docker image for API
if: steps.filter.outputs.api == 'true'
run: docker build -f docker/Dockerfile.api -t console-api .
run: npm run dc:build -- api
65 changes: 19 additions & 46 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,42 @@ pids
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
# Coverage directories and reports
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
bower_components

# TypeScript cache
*.tsbuildinfo
# Build directories and tools
build/Release
.grunt
.lock-wscript
.next
out
dist
build.ps1

# Optional npm cache directory
# Caches
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
.turbo
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
*.tsbuildinfo

# Optional REPL history
.node_repl_history
Expand All @@ -79,27 +69,10 @@ web_modules/
.env.production.local
.env.local

# Next.js build output
.next
out
dist

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# IDE files
.idea
.vscode

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Scripts
build.ps1
.vscode-test

# Data Folder
data
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,28 @@ See [Example_Queries.md](./doc/Example_Queries.md)

This document provides instructions on how to set up and run the application, including steps for manual database restoration and using Docker Compose for ease of setup.

### Using Docker and Docker Compose
This project's service are deployed using Docker and Docker Compose. The following sections provide instructions for setting up and running the application using Docker Compose.
All the Dockerfiles are using multi-stage builds to optimize the image build processes. Same files are used to build both development and production images. There are 3 docker-compose files:
- **docker-compose.build.yml:** Base file solely building production images for the services. It can be used to verify the same build process as in CICD.
- **docker-compose.prod.yml:** This file is used to run the services in production mode. It also includes the database service which would fetch a remote backup and import it on init.
- **docker-compose.yml:** The default file to run all the services in development mode with features like hot-reload.

Some commands are added to package.json for convenience.

```shell
npm run dc:build # Build the production images
npm run dc:up:prod # Run the services in production mode
npm run dc:up:dev # Run the services in development mode
npm run dc:down # Stop the services referencing any possible service
```

Note: you may pass any `docker compose` related arguments to the above commands. E.g. to only start `deploy-web` service in development mode:
```shell
npm run dc:up:dev -- deploy-web
```
This would also properly spin up all the dependencies like the `api`.

## Manual Database Restoration

Due to the extensive time required to index Akash from block #1, it's recommended to initialize your database using an existing backup for efficiency. This approach is particularly beneficial for development purposes.
Expand All @@ -108,19 +130,6 @@ gunzip -c /path/to/cloudmos-akash-sandbox.sql.gz | psql --host "localhost" --por
```
After restoring the database, you can proceed with the specific project's README instructions for further setup and running the application.

### Using Docker Compose
For convenience, a Docker Compose configuration is provided to automate the database setup, download, and data import process.

#### Configuration
1. Ensure environment variables are set in .postgres.local.env. This file configures PostgreSQL and import settings.
2. The `POSTGRES_DBS_FOR_IMPORT` variable should be updated with the databases you wish to import. This is a comma-separated list with potential values including `cloudmos-akash-sandbox` (default) and `cloudmos-akash-2` (mainnet). Leave this variable empty if no import is desired.

#### Running Docker Compose
```sh
docker-compose up db
```
This command spins up the database service and automatically handles the downloading and importing of the specified data.

# Database Structure

The database schemas is defined using [sequelize-typescript](https://github.com/sequelize/sequelize-typescript) in [/shared/dbSchemas/](./shared/dbSchemas/). Models are separated into the following folders:
Expand Down
Empty file added apps/api/package-lock.json
Empty file.
3 changes: 1 addition & 2 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"format": "prettier --write ./*.{js,json} **/*.{ts,js,json}",
"lint": "eslint .",
"start": "webpack --config webpack.dev.js --watch",
"dev": "npm run start",
"test": "jest --selectProjects unit functional",
"test:cov": "jest --selectProjects unit functional --coverage",
"test:functional": "jest --selectProjects functional",
Expand All @@ -40,7 +41,6 @@
"@octokit/rest": "^18.12.0",
"@sentry/node": "^7.55.2",
"axios": "^0.27.2",
"bufferutil": "^4.0.8",
"cosmjs-types": "^0.5.0",
"date-fns": "^2.29.2",
"date-fns-tz": "^1.3.6",
Expand All @@ -59,7 +59,6 @@
"sequelize": "^6.21.3",
"sequelize-typescript": "^2.1.5",
"stripe": "^10.14.0",
"utf-8-validate": "^5.0.10",
"uuid": "^9.0.1",
"zod": "^3.22.4"
},
Expand Down
6 changes: 3 additions & 3 deletions apps/api/webpack.dev.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const path = require("path");
const { NODE_ENV = "development" } = process.env;
const NodemonPlugin = require("nodemon-webpack-plugin");
const nodeExternals = require("webpack-node-externals");
const hq = require("alias-hq");
const webpack = require("webpack");

const { NODE_ENV = "development" } = process.env;

module.exports = {
entry: "./src/index.ts",
mode: NODE_ENV,
Expand All @@ -24,8 +25,7 @@ module.exports = {
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
loader: "ts-loader",
options: { configFile: "tsconfig.json" }
loader: "ts-loader"
}
]
},
Expand Down
11 changes: 7 additions & 4 deletions apps/api/webpack.prod.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const path = require("path");
const { NODE_ENV = "production" } = process.env;
const webpack = require("webpack");
const nodeExternals = require("webpack-node-externals");
const hq = require("alias-hq");
const webpack = require("webpack");

const { NODE_ENV = "production" } = process.env;

module.exports = {
entry: "./src/index.ts",
Expand All @@ -22,11 +23,13 @@ module.exports = {
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
loader: "ts-loader",
options: { configFile: "tsconfig.json" }
loader: "ts-loader"
}
]
},
optimization: {
minimize: false
},
plugins: [new webpack.IgnorePlugin({ resourceRegExp: /^pg-native$/ })],
node: {
__dirname: true
Expand Down
34 changes: 0 additions & 34 deletions apps/deploy-web/nginx.conf

This file was deleted.

3 changes: 1 addition & 2 deletions apps/deploy-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"build": "next build",
"build-analyze": "set ANALYZE=true&& next build",
"dev": "next -p 3000",
"dev": "next",
"format": "prettier --write ./*.{ts,js,json} **/*.{ts,tsx,js,json}",
"lint": "eslint .",
"start": "next start",
Expand Down Expand Up @@ -144,7 +144,6 @@
"eslint": "^8.57.0",
"eslint-config-next": "^14.2.3",
"eslint-plugin-simple-import-sort": "^12.1.0",
"patch-package": "^8.0.0",
"postcss": "^8.4.31",
"postcss-nesting": "^12.0.2",
"prettier": "^3.3.0",
Expand Down
1 change: 0 additions & 1 deletion apps/deploy-web/patches/placeholder.txt

This file was deleted.

1 change: 1 addition & 0 deletions apps/indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"format": "prettier --write ./*.{js,json} **/*.{ts,js,json}",
"lint": "eslint .",
"start": "webpack --mode development --config webpack.dev.js --watch",
"dev": "npm run start",
"test": "jest"
},
"dependencies": {
Expand Down
4 changes: 3 additions & 1 deletion apps/indexer/webpack.dev.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const path = require("path");
const { NODE_ENV = "development" } = process.env;
const NodemonPlugin = require("nodemon-webpack-plugin");
const nodeExternals = require("webpack-node-externals");
const hq = require("alias-hq");
const webpack = require("webpack");

const { NODE_ENV = "development" } = process.env;

module.exports = {
entry: "./src/index.ts",
mode: NODE_ENV,
target: "node",
devtool: "source-map",
output: {
path: path.resolve(__dirname, "dist"),
filename: "server.js"
Expand Down
3 changes: 2 additions & 1 deletion apps/indexer/webpack.prod.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const path = require("path");
const { NODE_ENV = "production" } = process.env;
const webpack = require("webpack");
const nodeExternals = require("webpack-node-externals");
const hq = require("alias-hq");

const { NODE_ENV = "production" } = process.env;

module.exports = {
entry: "./src/index.ts",
mode: NODE_ENV,
Expand Down
4 changes: 2 additions & 2 deletions apps/landing/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "cloudmos-block-explorer",
"name": "console-landing",
"version": "2.5.2",
"private": true,
"description": "Landing page for Cloudmos",
"license": "Apache-2.0",
"author": "Cloudmos",
"scripts": {
"build": "next build",
"dev": "next -p 3001",
"dev": "next",
"format": "prettier --write ./*.{ts,js,json} **/*.{ts,tsx,js,json}",
"lint": "eslint .",
"start": "next start",
Expand Down
Loading

0 comments on commit b56cd48

Please sign in to comment.