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 13, 2024
1 parent bd79006 commit f00c727
Show file tree
Hide file tree
Showing 19 changed files with 298 additions and 248 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
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.
1 change: 1 addition & 0 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 Down
2 changes: 1 addition & 1 deletion 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
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: 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
27 changes: 27 additions & 0 deletions docker-compose.build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
services:
api:
build:
dockerfile: docker/Dockerfile.api
target: production

indexer:
build:
dockerfile: docker/Dockerfile.indexer
target: production

provider-proxy:
build:
dockerfile: docker/Dockerfile.provider-proxy

deploy-web:
build:
dockerfile: docker/Dockerfile.deploy-web
target: production

stats-web:
build:
dockerfile: docker/Dockerfile.stats-web

landing:
build:
dockerfile: docker/Dockerfile.landing
64 changes: 64 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
services:
api:
restart: always
env_file:
- .env.sandbox.docker-compose-dev
ports:
- '3080:3080'
depends_on:
- db

indexer:
restart: always
env_file:
- .env.sandbox.docker-compose-dev
depends_on:
- db

provider-proxy:
restart: always
ports:
- '3040:3040'

deploy-web:
restart: always
environment:
API_BASE_URL: http://api:3080
PROVIDER_PROXY_URL: http://provider-proxy:3040
env_file:
- apps/deploy-web/.env.local
ports:
- '3000:3000'
depends_on:
- api
- provider-proxy

stats-web:
restart: always
env_file:
- .env.sandbox.docker-compose-dev
ports:
- '3001:3000'
depends_on:
- api

landing:
restart: always
ports:
- '3002:3000'

db:
build:
context: docker
dockerfile: Dockerfile.db
env_file:
- .env.sandbox.docker-compose-dev
restart: always
ports:
- '5432:5432'
volumes:
- postgres_data:/var/lib/postgresql/data

volumes:
postgres_data:
driver: local
Loading

0 comments on commit f00c727

Please sign in to comment.