diff --git a/Dockerfile b/Dockerfile index b27d4d3..7b14ec2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,36 +1,35 @@ -FROM node:8.9-alpine +FROM node:alpine LABEL Description="This image is used to start the hc-api-feathers" Vendor="Human-Connection gGmbH" Version="1.0" Maintainer="Human-Connection gGmbH (developer@human-connection.org)" -# update unix packages -RUN apk update && apk upgrade -RUN rm -rf /var/cache/apk/* -RUN yarn global add pm2 - # expose the app port EXPOSE 3030 -# set environment variables -# ENV NPM_CONFIG_PRODUCTION=false -# ENV HOST=0.0.0.0 +# override configuration by instance name in docker container +ENV NODE_APP_INSTANCE=docker ENV NODE_ENV=production -ENV API_PORT=3030 - -# start the application in a autohealing cluster -# NOTE: quick fix for server issues, restart api when reaching max of 300 MB Memory Usage (happens in conjunction with 100% CPU Usage) -# TODO: find better way of dealing with that issue -CMD NODE_ENV=production pm2 start server/index.js -n api --attach --max-memory-restart 1024M -# CMD NODE_ENV=production pm2 start server/index.js -n api -i 2 --attach -# as we have issues with pm2 currently in conjunction with nuxt, we use the standard approach here -# CMD NODE_ENV=production node server/index.js # create working directory -RUN mkdir -p /var/www/ -WORKDIR /var/www/ +RUN mkdir -p /API +WORKDIR /API + +# --no-cache: download package index on-the-fly, no need to cleanup afterwards +# --virtual: bundle packages, remove whole bundle at once, when done +RUN apk --no-cache --virtual build-dependencies add python make g++ + +RUN yarn global add pm2 # install app dependencies -COPY package.json /var/www/ -COPY yarn.lock /var/www/ -RUN yarn install --frozen-lockfile --non-interactive +COPY package.json /API +COPY yarn.lock /API +RUN yarn install --production=false --frozen-lockfile --non-interactive + +RUN apk del build-dependencies + # copy the code to the docker image -COPY . /var/www/ +COPY . /API + +# start the application in a autohealing cluster +# NOTE: quick fix for server issues, restart api when reaching max of 300 MB Memory Usage (happens in conjunction with 100% CPU Usage) +# TODO: find better way of dealing with that issue +CMD ["pm2", "start", "server/index.js", "-n", "api", "--attach", "--max-memory-restart", "1024M"] diff --git a/README.md b/README.md index deffa7a..12f238e 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,49 @@ The API for a better world. More information under [human-connection.org](https: > **Note:** This is only the API part of Human-Connection, you have to also checkout the [WebApp](https://github.com/Human-Connection/WebApp) which uses this API. +## Installation via docker + +Make sure you have a recent version of [docker](https://www.docker.com/) and [docker-compose](https://docs.docker.com/compose/). + +Run: +```bash +$ docker-compose up --build +``` +Now, your API should be running at [http://localhost:3030](http://localhost:3030) +and you can see some contributions at [http://localhost:3030/contributions](http://localhost:3030/contributions). + +For debugging you can run: +```bash +$ docker-compose run --rm --service-ports api yarn run dev:debug +``` +And debug your app with [Chrome Dev Tools](chrome://inspect). + +### Configuration in Docker + +Change configuration in `config/docker/local-development.json` or +`config/docker/local.json` and rerun `docker-compose up --build`. + +#### Local Staging Environment + +To get an environment which is close to production, run the following: +```sh +$ docker-compose -f docker-compose.yml -f docker-compose.staging.yml up --build +``` + +### Testing in Docker + +Run the entire test suite with: +```bash +$ docker-compose run --rm api yarn run test +``` + +If you want you can run specific tests: +```bash +$ docker-compose run --rm api yarn run mocha +$ docker-compose run --rm api yarn run cucumber +``` + + ## Local installation > we recommand to install the project locally for the best development ease and performance @@ -91,19 +134,8 @@ Getting up and running is as easy as 1, 2, 3, 4 ... 5. Now, your API should be running at [http://localhost:3030](http://localhost:3030). If you seeded your database, you will see some contributions at [http://localhost:3030/contributions](http://localhost:3030/contributions). -7. Setup and Start Thumbnail Service (optional) - - We are using [Thumbor](https://github.com/thumbor/thumbor) as a Thumbnail Microservice. - You can install it locally if you like but this is totally optional. - - **Install OR use docker** - - - At first you have to [install](http://thumbor.readthedocs.io/en/latest/installing.html) it locally and start it in the console with `thumbor` **OR** run it with docker `docker run -p 8000:8000 apsl/thumbor` - - Set the `thumbor.url` in `config/local.json` to `http://localhost:8888` (with docker `http://localhost:8000`) if not defined differently. The `thumbor.key` does not necessarily have to be defined, it just makes the URL more secure. - - > Do not forget to always start it if you choose that setup or otherwise you will not see any pictures at all. -### Local configuration +### Local Configuration You can override any default configuration in `config/local.json`. You can find a list of availabe defaults in `config/default.json`. @@ -121,7 +153,7 @@ replace `localhost` in your settings with your IP address in the local network: ``` -## Testing +### Local Testing Run the entire test suite with: ```bash diff --git a/config/default-docker.json b/config/default-docker.json new file mode 100644 index 0000000..3db818c --- /dev/null +++ b/config/default-docker.json @@ -0,0 +1,4 @@ +{ + "mongodb": "mongodb://mongo:27017/hc_api", + "host": "0.0.0.0" +} diff --git a/config/local-staging-docker.json b/config/local-staging-docker.json new file mode 100644 index 0000000..3c81e30 --- /dev/null +++ b/config/local-staging-docker.json @@ -0,0 +1,16 @@ +{ + "baseURL": "http://172.25.0.11:3030", + "seeder": { + "runOnInit": true, + "dropDatabase": true + }, + "smtpConfig": { + "host": "maildev", + "port": 25, + "ignoreTLS": true + }, + "thumbor": { + "url": "http://localhost:8000", + "key": "" + } +} diff --git a/config/test-docker.json b/config/test-docker.json new file mode 100644 index 0000000..c40c94e --- /dev/null +++ b/config/test-docker.json @@ -0,0 +1,3 @@ +{ + "mongodb": "mongodb://mongo:27017/hc_api_test" +} diff --git a/docker-compose.override.yml b/docker-compose.override.yml new file mode 100644 index 0000000..f86475e --- /dev/null +++ b/docker-compose.override.yml @@ -0,0 +1,13 @@ +version: '3' + +services: + api: + environment: + - NODE_ENV=development + ports: + - "3030:3030" + - "9229:9229" # node inspect + volumes: + - .:/API + - /API/node_modules + command: yarn run dev diff --git a/docker-compose.staging.yml b/docker-compose.staging.yml new file mode 100644 index 0000000..52222f2 --- /dev/null +++ b/docker-compose.staging.yml @@ -0,0 +1,43 @@ +version: '3' + +services: + api: + image: humanconnection/api-feathers + build: + context: . + environment: + - NODE_ENV=staging + depends_on: + - maildev + - thumbor + ports: + - "3030:3030" + networks: + hc-network: + ipv4_address: 172.25.0.11 + thumbor: + networks: + hc-network: + ipv4_address: 172.25.0.13 + maildev: + image: djfarrelly/maildev + networks: + - hc-network + ports: + - "1080:80" + - "1025:25" + thumbor: + image: apsl/thumbor + networks: + - hc-network + ports: + - "8000:8000" + +networks: + hc-network: + driver: bridge + ipam: + driver: default + config: + - + subnet: 172.25.0.0/16 diff --git a/docker-compose.yml b/docker-compose.yml index dafd68d..d928c5a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,16 +1,17 @@ -version: '2' +version: '3' services: - mongo-cluster: - image: mongo - ports: - - "27017:27017" - command: "--smallfiles --logpath=/dev/null" api: build: . - ports: - - "3030:3030" - links: - - mongo-cluster - environment: - PORT: 3030 \ No newline at end of file + depends_on: + - mongo + networks: + - hc-network + mongo: + image: mongo + networks: + - hc-network + command: "--smallfiles --logpath=/dev/null" + +networks: + hc-network: diff --git a/package.json b/package.json index d9094f5..d2d4b6a 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ }, "scripts": { "ci": "yarn run eslint && yarn run test:coverage", - "clear": "rm -Rf tmp", + "clean": "rm -Rf tmp", "mongo": "mongod --dbpath data --quiet", "mongo:win": "mongod --dbpath /data/db --quiet", "test": "yarn run mocha && yarn run cucumber", @@ -43,7 +43,7 @@ "start": "node server/", "dev": "cross-env DEBUG=feathers* nodemon server/", "dev:debug": "cross-env DEBUG=feathers* nodemon --inspect=0.0.0.0:9229 server/", - "mocha": "yarn run clear && cross-env NODE_ENV=test mocha --recursive --timeout 10000 --exit", + "mocha": "yarn run clean && cross-env NODE_ENV=test mocha --recursive --timeout 10000 --exit", "cucumber": "concurrently --kill-others --success first 'cross-env NODE_ENV=test node server/' 'wait-on tcp:3031 && cross-env NODE_ENV=test cucumber-js'" }, "dependencies": {