From aa6f3f51de01407b2702488409c17cd38d9a4e79 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 15 Nov 2021 22:47:41 +0100 Subject: [PATCH] Fix backend production deploy and database connection - Fixed database URL locally and in production - Set separate DB user just to be safe - Migrate DB schema on each launch - Use node user during dev but use root during deploy - Fix output of the dev container (tsc-watch --noClear) --- .gitignore | 2 ++ backend/Dockerfile | 2 ++ backend/Dockerfile-prod | 1 - backend/docker.sh | 1 + backend/package.json | 2 +- backend/src/datasources/database.datasource.ts | 2 +- backend/src/index.ts | 1 + deploy-backend.sh | 2 +- docker-compose.yml | 8 ++++++-- frontend/.dockerignore | 5 +++++ 10 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 frontend/.dockerignore diff --git a/.gitignore b/.gitignore index 3eb038c..3f02730 100644 --- a/.gitignore +++ b/.gitignore @@ -46,3 +46,5 @@ testem.log # System Files .DS_Store Thumbs.db + +.firebase/ diff --git a/backend/Dockerfile b/backend/Dockerfile index 146baf5..39c3f77 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -23,6 +23,8 @@ RUN apt-get update RUN apt-get install -y docker-ce docker-ce-cli containerd.io # Install Heroku CLI RUN curl https://cli-assets.heroku.com/install-ubuntu.sh | sh +# Install wait-for-it +RUN apt-get install -y wait-for-it ENTRYPOINT [ "/docker.sh" ] CMD [ "run" ] diff --git a/backend/Dockerfile-prod b/backend/Dockerfile-prod index 19b7c1f..2a852d0 100644 --- a/backend/Dockerfile-prod +++ b/backend/Dockerfile-prod @@ -24,5 +24,4 @@ RUN npm run build # Bind to all network interfaces so that it can be mapped to the host OS ENV HOST=0.0.0.0 -EXPOSE ${PORT} CMD [ "node", "." ] diff --git a/backend/docker.sh b/backend/docker.sh index b78e67d..cade815 100644 --- a/backend/docker.sh +++ b/backend/docker.sh @@ -19,4 +19,5 @@ echo "Installing packages" npm install echo "Running application" npm run rebuild +wait-for-it database:3306 -t 0 npm run start:watch diff --git a/backend/package.json b/backend/package.json index e8cee38..39802c8 100644 --- a/backend/package.json +++ b/backend/package.json @@ -33,7 +33,7 @@ "openapi-spec": "node ./dist/openapi-spec", "prestart": "npm run rebuild", "start": "node -r source-map-support/register .", - "start:watch": "tsc-watch --target es2017 --outDir ./dist --onSuccess \"node .\"", + "start:watch": "tsc-watch --target es2017 --outDir ./dist --onSuccess \"node .\" --noClear", "clean": "lb-clean dist *.tsbuildinfo .eslintcache", "rebuild": "npm run clean && npm run build" }, diff --git a/backend/src/datasources/database.datasource.ts b/backend/src/datasources/database.datasource.ts index c691450..9f5fdfa 100644 --- a/backend/src/datasources/database.datasource.ts +++ b/backend/src/datasources/database.datasource.ts @@ -4,7 +4,7 @@ import {juggler} from '@loopback/repository'; const config = { name: 'database', connector: 'mysql', - url: process.env.DATABASE_URL, + url: process.env.DATABASE_URL ?? process.env.CLEARDB_DATABASE_URL, host: '', port: 0, user: '', diff --git a/backend/src/index.ts b/backend/src/index.ts index 9dc5393..d84c55a 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -5,6 +5,7 @@ export * from './application'; export async function main(options: ApplicationConfig = {}) { const app = new SzakdolgozatBackendApplication(options); await app.boot(); + await app.migrateSchema({existingSchema: 'alter'}); await app.start(); const url = app.restServer.url; diff --git a/deploy-backend.sh b/deploy-backend.sh index 581c2cf..b66d90a 100755 --- a/deploy-backend.sh +++ b/deploy-backend.sh @@ -2,4 +2,4 @@ cd backend || exit docker-compose build backend -docker-compose run -e COMMAND=deploy -v /var/run/docker.sock:/var/run/docker.sock backend +USERNAME="0" docker-compose run -e COMMAND=deploy -v /var/run/docker.sock:/var/run/docker.sock backend diff --git a/docker-compose.yml b/docker-compose.yml index 487b768..3e44219 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,13 +15,17 @@ services: - ./backend:/home/node/app environment: - COMMAND=run - - DATABASE_URL=${CLEARDB_DATABASE_URL} + - DATABASE_URL=mysql://szakdolgozat:DevelopmentPassword@database/szakdolgozat + user: ${USERNAME:-node} database: image: mysql:5.6 ports: - "3306:3306" environment: - - MYSQL_ROOT_PASSWORD=DevelopmentPassword + - MYSQL_ROOT_PASSWORD=VerySecretPassword + - MYSQL_DATABASE=szakdolgozat + - MYSQL_USER=szakdolgozat + - MYSQL_PASSWORD=DevelopmentPassword networks: default: diff --git a/frontend/.dockerignore b/frontend/.dockerignore new file mode 100644 index 0000000..7aecc7e --- /dev/null +++ b/frontend/.dockerignore @@ -0,0 +1,5 @@ +node_modules +npm-debug.log +/dist +# Cache used by TypeScript's incremental build +*.tsbuildinfo