Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: using docker-sync to speed up local development #86

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .env.test
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
REDIS_URL=redis://redis:6379/1
REDIS_URL=redis://redis_test:6379/1
RDS_HOSTNAME=postgres_test
RDS_PORT=5432
RDS_DB_NAME=app
RDS_USERNAME=postgres
RDS_PASSWORD=password
APP_NAME=express-boilerplate
AUTH_API_KEY=b6cfcd8f-db8e-2cb5-cb34-e1a8900067fd
STAGE=development
STAGE=test
BITBUCKET_COMMIT=commit_sha
API_URL=http://localhost:1337
6 changes: 2 additions & 4 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: cp .env.dist .env
- run: cp docker-compose.override.yml.dist docker-compose.override.yml
- run: npm install
- run: npm run docker-build
- run: npm run lint
- run: npm run units
- run: npm run integration
- run: npm run units-ci
- run: npm run integration-ci
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ docker-compose.*yml
docker-compose.yml.*
!docker-compose.e2e.yml
!docker-compose.yml
!docker-compose.test.yml
!docker-compose.override.test.yml
.scannerwork
coverage
*.log
build
docker/dev/datastore
config/all.override.yaml
tmp
.docker-sync
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,43 @@ After checkout of a repository, please perform the following steps in exact sequ

3. Run `npm i`

4. Run `npm run docker-build`

5. Run watch - `npm run watch`

##

### Dev setup

This app is fully dockerized, so in order to use it you have to have docker and docker-compose installed. What's more you need to have npm in order to run npm scripts.
This app is fully dockerized, so in order to use it you have to have docker, [docker-sync](https://docker-sync.readthedocs.io/en/latest/index.html) and docker-compose installed.

1. In order to run the whole app type:
1. In order to watch files for dev purpose type:

```
npm run start
npm run watch
```

2. In order to watch files for dev purpose type:
1. In a separate terminal run docker-sync:

```
npm run watch
npm run sync-start
```

3. If you need to close all containers run:
1. To start the app run:

```
npm run down
npm start
```

4. To get into Docker container's shell:
1. To stop docker-sync exit with Ctrl+C and then run:

```
npm run shell
npm run sync-clean
```
to clean everything up.

1. After making changes in package.json (especially when adding new dependencies) it might be necessary to rebuild the image. To do that run:

```
npm run docker-build
npm run docker-build-test
```
##

### Code generation
Expand Down
3 changes: 1 addition & 2 deletions bitbucket-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ pipelines:
- docker
deployment: test
script:
- npm run docker-build
- npm run lint
- npm run units
- npm run units-ci

custom:
create-staging:
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.override.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.4'

services:
boilerplate_api_test:
volumes:
- boilerplate_api-sync:/app/build:nocopy

volumes:
boilerplate_api-sync:
external: true
23 changes: 6 additions & 17 deletions docker-compose.override.yml.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
version: '3.4'

x-common:
volumes: &volumes
- build:/app/build/
- .:/app:delegated
- ./.env.dist:/app/build/.env.dist
- ./.env:/app/build/.env

services:
builder:
volumes: *volumes

js:
volumes: *volumes

boilerplate_api:
volumes: *volumes
depends_on:
- adminer
- postgres
volumes:
- boilerplate_api-sync:/app/build:nocopy

adminer:
image: adminer
Expand All @@ -29,3 +14,7 @@ services:
- 8080:8080
networks:
- app

volumes:
boilerplate_api-sync:
external: true
35 changes: 35 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: '3.4'

services:
boilerplate_api_test:
build:
context: .
dockerfile: ./docker/dev/Dockerfile
working_dir: /app/build
container_name: boilerplate_api_test
hostname: boilerplate_api_test
env_file:
- .env.test
depends_on:
- postgres_test
- redis_test
networks:
- app_test

postgres_test:
image: postgres:14-alpine
environment:
POSTGRES_PASSWORD: password
POSTGRES_USERNAME: postgres
POSTGRES_DB: app
networks:
- app_test

redis_test:
container_name: redis_test
image: 'redis'
networks:
- app_test

networks:
app_test:
62 changes: 4 additions & 58 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,58 +1,16 @@
version: '3.4'

x-common: &common
image: app:local
labels:
com.app: app
networks:
- app
volumes:
- build:/app/build/

services:
builder:
volumes:
- build:/app/build/
- .:/app:delegated
<<: *common
boilerplate_api:
build:
context: .
dockerfile: ./docker/dev/Dockerfile
cache_from:
- 'app:local'
- 'app:${BRANCH_NAME:-ignore}'
container_name: app-builder
working_dir: /app
command: [sh, -c, 'rm -rf ./build/* && ./node_modules/.bin/tsc -w --pretty --skipLibCheck --outDir build/src']
#FOR SWC TRANSPILATION
# command: [sh, -c, 'rm -rf ./build/src/* && ./node_modules/.bin/swc src --out-dir build/src -w --sync & ./node_modules/.bin/tsc -w --pretty --skipLibCheck --noEmit']

js:
image: app:local
env_file:
- .env.dist
depends_on:
- postgres
- redis
networks:
- app

js_test:
image: app:local
env_file:
- .env.test
depends_on:
- postgres_test
- redis
networks:
- app

boilerplate_api:
<<: *common
working_dir: /app/build
container_name: boilerplate_api
command: [sh, -c, 'node --watch --inspect=0.0.0.0 -r source-map-support/register ./src/index.js']
command: [sh, -c, 'nodemon -L --inspect=0.0.0.0 -r source-map-support/register ./src/index.js']
hostname: boilerplate_api
env_file:
- .env
depends_on:
- postgres
- redis
Expand All @@ -72,15 +30,6 @@ services:
networks:
- app

postgres_test:
image: postgres:14-alpine
environment:
POSTGRES_PASSWORD: password
POSTGRES_USERNAME: postgres
POSTGRES_DB: app
networks:
- app

redis:
container_name: redis
image: 'redis'
Expand All @@ -91,6 +40,3 @@ services:

networks:
app:

volumes:
build:
8 changes: 8 additions & 0 deletions docker-sync.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "2"
syncs:
boilerplate_api-sync:
src: './build'
compose-file-path:
- 'docker-compose.yml'
compose-dev-file-path:
- 'docker-compose.override.yml'
5 changes: 3 additions & 2 deletions docker/dev/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ WORKDIR /app
RUN apk add --no-cache bash curl git py-pip make && \
npx node-prune && \
npm cache clean --force

FROM builder

COPY docker ./docker
Expand All @@ -18,7 +18,8 @@ RUN chmod +x ./docker/node-modules-clean.sh
RUN npm ci && npm cache clean --force && \
./docker/node-modules-clean.sh && \
npm dedupe && \
rm -f .npmrc
rm -f .npmrc && \
npm i -g nodemon

COPY . .
RUN npm run build
Expand Down
34 changes: 19 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@
"start": "docker-compose up boilerplate_api",
"build": "tsc --skipLibCheck --outDir build/src",
"build-swc": "swc src --out-dir build/src --sync && tsc --skipLibCheck --noEmit",
"watch": "docker-compose up builder",
"docker-build": "BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD | sed 's/[/]/-/g'); docker-compose build builder && docker tag app:local app:${BRANCH_NAME}",
"watch": "rm -rf ./build/* && tsc -w --pretty --skipLibCheck --outDir build/src",
"docker-build": "docker compose build boilerplate_api",
"docker-build-test": "docker compose -f docker-compose.test.yml build boilerplate_api_test",
"docker-build-prod": "docker build -t boilerplate-api:latest -f ./docker/prod/Dockerfile .",
"down": "docker-compose down -v",
"format": "docker-compose run --rm js ./node_modules/.bin/prettier --write \"**/**/*.ts\" \"!**/**/*.d.ts\"",
"lint": "docker-compose run --rm js ./node_modules/.bin/eslint --ext .ts ./",
"lint-fix": "docker-compose run --rm js ./node_modules/.bin/eslint --ext .ts --fix ./",
"sync-start": "docker-sync-stack start",
"sync-clean": "docker-sync-stack clean",
"format": "./node_modules/.bin/prettier --write \"**/**/*.ts\" \"!**/**/*.d.ts\"",
"lint": "./node_modules/.bin/eslint --ext .ts ./",
"lint-fix": "./node_modules/.bin/eslint --ext .ts --fix ./",
"integration-tests": "cd build && node ../node_modules/.bin/mocha \"src/tests/**/*.spec.js\" --file=\"src/tests/bootstrap.js\" --exit",
"services-units": "cd build && node ../node_modules/.bin/mocha \"src/{,!(tests)/**}/*.spec.js\"",
"services-units-coverage": "nyc npm run services-units",
"integration": "docker-compose run --rm js_test npm run integration-tests",
"units": "docker-compose run --rm js npm run services-units-coverage",
"shell": "docker-compose run --rm js sh",
"generate-migration": "docker-compose run --rm js ./node_modules/.bin/typeorm migration:generate ./src/migrations/migration -d ./build/src/config/db",
"run-migrations": "docker-compose run --rm js ./node_modules/.bin/typeorm migration:run -d ./build/src/config/db",
"revert-migration": "docker-compose run --rm js ./node_modules/.bin/typeorm migration:revert -d ./build/src/config/db",
"create-migration": "docker-compose run --rm js ./node_modules/.bin/typeorm migration:create ./src/migrations/migration",
"integration-ci": "docker compose -f docker-compose.test.yml run --rm boilerplate_api_test npm run integration-tests && docker compose -f docker-compose.test.yml down",
"integration": "docker compose -f docker-compose.test.yml -f docker-compose.override.test.yml run --rm boilerplate_api_test npm run integration-tests && docker compose -f docker-compose.test.yml down",
"units-ci": "docker compose -f docker-compose.test.yml run --rm boilerplate_api_test npm run services-units-coverage && docker compose -f docker-compose.test.yml down",
"units": "npm run services-units-coverage",
"generate-migration": "./node_modules/.bin/typeorm migration:generate ./src/migrations/migration -d ./build/src/config/db",
"run-migrations": "./node_modules/.bin/typeorm migration:run -d ./build/src/config/db",
"revert-migration": "./node_modules/.bin/typeorm migration:revert -d ./build/src/config/db",
"create-migration": "./node_modules/.bin/typeorm migration:create ./src/migrations/migration",
"plop": "plop",
"generate-schema": "docker-compose run --rm js ./node_modules/.bin/graphql-codegen",
"generate-schema": "./node_modules/.bin/graphql-codegen",
"prepare": "husky install"
},
"keywords": [],
Expand Down Expand Up @@ -68,22 +72,22 @@
"yamljs": "^0.3.0"
},
"devDependencies": {
"@types/body-parser": "^1.19.3",
"@types/node": "^20.8.4",
"@commitlint/cli": "^17.7.2",
"@commitlint/config-conventional": "^17.7.0",
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/typescript": "^4.0.1",
"@graphql-codegen/typescript-resolvers": "^4.0.1",
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.92",
"@types/body-parser": "^1.19.3",
"@types/chai": "^4.3.7",
"@types/chai-as-promised": "^7.1.6",
"@types/cors": "^2.8.14",
"@types/express": "^4.17.19",
"@types/express-serve-static-core": "^4.17.37",
"@types/jsonwebtoken": "^9.0.3",
"@types/mocha": "^10.0.2",
"@types/node": "^20.8.4",
"@types/supertest": "^2.0.14",
"@types/swagger-ui-express": "^4.1.4",
"@types/uuid": "^9.0.5",
Expand Down
Loading