From d9288f25bc003dcda460e72f85f4877122f530f3 Mon Sep 17 00:00:00 2001 From: Antoine AMARA Date: Tue, 9 Apr 2019 08:18:18 +0200 Subject: [PATCH 1/2] config: add docker-compose file to be able to quickly deploy the application create a docker-compose configration to deploy the nodejs app and a postgresql service. add configs for networks and open the port for the nodejs app (5000 for app, 9042 if you want to enable the debugger) and the database (for debugging, if it is needed). add a docker volume to persist the database. --- docker-compose.yml | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..160a0ab --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,37 @@ +version: "3" + +services: + nodejs: + image: node:10.15-alpine + volumes: + - ./:/app/ + working_dir: /app/ + networks: + - public + ports: + - "5000:5000" + - "9042:9042" + environment: + - PORT= + - NODE_ENV=development + - SECRET_KEY= + - DATABASE_URL=${DATABASE_URL} + - GITHUB_CLIENT_ID=${GITHUB_CLIENT_ID} + - GITHUB_CLIENT_SECRET=${GITHUB_CLIENT_SECRET} + command: npm run start + + postgres: + image: postgres:11.2-alpine + volumes: + - data_db:/var/lib/postgresql/data + networks: + - public + ports: + - "5432:5432" + + +networks: + public: + +volumes: + data_db: From a24a9fd1796fdbbba2122642972d41325b9d9307 Mon Sep 17 00:00:00 2001 From: Antoine AMARA Date: Fri, 12 Apr 2019 17:17:34 +0200 Subject: [PATCH 2/2] config: add a Makefile to run and manage docker environment create a Makefile inspired by gazr normalization to be able to easily deploy and manage the docker environment. this config let you launch basic docker-compose command to run and manage the services inside containers. add a section to the USER_GUIDE to explain how to use the Makefile commands to run your environment. Fixes: https://github.com/MLH/mlh-hackathon-nodejs-starter/issues/3 Refs: https://gazr.io/ --- Makefile | 59 ++++++++++++++++++++++++++++++++++++++++++++++ docs/USER_GUIDE.md | 57 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d47e15d --- /dev/null +++ b/Makefile @@ -0,0 +1,59 @@ +## +# MLH Hackathon Nodejs Starter +# commands to run and manipulate docker environment +## +DKR=docker-compose +NODE=nodejs + +default: help; + +init: ## initialize your environment from the begining, build docker stuff, install dependencies and run the watch mode. + ${DKR} build + make dependencies + make watch + +watch: ## run the application in development mode with a live reload, display the status of the containers and display the logs. + ${DKR} up -d + make status + echo "" + make logs + +run: ## run the production mode of the app, no livereload here, just launch nodejs and display the status of components. + ${DKR} run --rm ${NODE} npm run deploy + make status + +stop: ## stop the entire application, the database data is preserved. + ${DKR} down + +restart: ## restart all components (nodejs and database). + make stop + make watch + +dependencies: ## install (or reinstall) npm dependencies. + ${DKR} run --rm ${NODE} npm install + +status: ## display status of each component, Up -> UNIX process is running. Exit -> UNIX process was killed or crash. + ${DKR} ps + +logs: ## display nodejs server logs. + ${DKR} logs -f ${NODE} + +command: ## launch a bash command inside your environment. example: make command cmd="npm install --save express" + ${DKR} run --rm ${NODE} ${cmd} + +destroy: ## stop, clean and destroy everything. delete node_modules, destroy process and delete the entire database. + read -p "This command will stop your env, clean the repo and destroy the database. Did you want to continue? (y/n)" resp ; \ + echo "" ; \ + if [ "$$resp" = "y" ]; then \ + ${DKR} run --rm ${NODE} rm -rf ./node_modules ; ${DKR} down -v --remove-orphans ; \ + fi + +help: ## display this help. + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' ${MAKEFILE_LIST} | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + +.PHONY: + init watch run stop restart dependencies status logs destroy +.SILENT: + init watch run stop restart dependencies status logs destroy + +# end diff --git a/docs/USER_GUIDE.md b/docs/USER_GUIDE.md index e9139ac..cf2f0d4 100644 --- a/docs/USER_GUIDE.md +++ b/docs/USER_GUIDE.md @@ -217,6 +217,63 @@ The `DATABASE_URL` variable is the path to your database system. This is where y The `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` variables are the app credentials from your [GitHub OAuth app](https://github.com/settings/developers). +## Deploy development environment with docker + +You can easily and quickly deploy an environment with Docker containers. A +Makefile is available with some commands to quick and easy launch. + +### What you need ? + +* a docker daemon running on your machine. +* docker-compose tools to be able to launch the environment. + +### How to use. + +The first time you want to launch the environment, you have to initialize it +(build stuff for docker and install the dependencies), for that just launch: + +```sh +make init +``` + +This command will initialize and launch the entire process in watch mode. +(Nodejs process with livereload). + +If your environment was already been initialized, you can just use a command to +run the application: + +```sh +make watch # run the nodejs app in development mode with a livereload. +# OR +make run # run the nodejs app in production mode. +``` + +If you want to visualize the logs of the Nodejs app: + +```sh +make logs +``` + +If you want to switch off your environment: + +```sh +make stop +``` + +Finally, when your work is finished and you don't need the environment at all, +you can stop and destroy all stuff and data (node_modules and database data): + +```sh +make destroy +``` + +**Note**: if you want advanced commands for this docker environment, consult the +help: + +```sh +make help +``` + ## Deployment ### Deploy to Heroku