From 490b2850f36589ee31c3504c0c1cfabbec664e4f Mon Sep 17 00:00:00 2001 From: Kunal Kapadia Date: Wed, 5 Apr 2017 00:09:13 +0530 Subject: [PATCH] Add Dockerfile and docker-compose for development (#281) * Add Dockerfile and docker-compose for development * Update Docker-Compose files as per .env changes * Replace npm with yarn in Dockerfile * Replace npmrc with yarnrc * Remove yarn installation steps from Dockerfile * Add docker-compose test --- .dockerignore | 37 ++++++++++++++++++++++++++++++++++++ .npmrc | 1 - .yarnrc | 1 + Dockerfile | 26 +++++++++++++++++++++++++ README.md | 8 ++++++++ bin/development.sh | 5 +++++ bin/test.sh | 6 ++++++ docker-compose.test.yml | 42 +++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 38 +++++++++++++++++++++++++++++++++++++ index.js | 4 ++-- 10 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 .dockerignore delete mode 100644 .npmrc create mode 100644 .yarnrc create mode 100644 Dockerfile create mode 100644 bin/development.sh create mode 100644 bin/test.sh create mode 100644 docker-compose.test.yml create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..5148e527 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,37 @@ +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules +jspm_packages + +# Optional npm cache directory +.npm + +# Optional REPL history +.node_repl_history diff --git a/.npmrc b/.npmrc deleted file mode 100644 index 449691b7..00000000 --- a/.npmrc +++ /dev/null @@ -1 +0,0 @@ -save-exact=true \ No newline at end of file diff --git a/.yarnrc b/.yarnrc new file mode 100644 index 00000000..95b8581e --- /dev/null +++ b/.yarnrc @@ -0,0 +1 @@ +save-prefix false diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..e4b77756 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +# take default image of node boron i.e node 6.x +FROM node:6.10.1 + +MAINTAINER Kunal Kapadia + +# create app directory in container +RUN mkdir -p /app + +# set /app directory as default working directory +WORKDIR /app + +# only copy package.json initially so that `RUN yarn` layer is recreated only +# if there are changes in package.json +ADD package.json yarn.lock /app/ + +# --pure-lockfile: Don’t generate a yarn.lock lockfile +RUN yarn --pure-lockfile + +# copy all file from current dir to /app in container +COPY . /app/ + +# expose port 4040 +EXPOSE 4040 + +# cmd to start service +CMD [ "yarn", "start" ] diff --git a/README.md b/README.md index ca0aa38e..7f83e8c5 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,14 @@ Get code coverage summary on executing `yarn test` `yarn test` also generates HTML code coverage report in `coverage/` directory. Open `lcov-report/index.html` to view it. ![Code coverage HTML report](https://cloud.githubusercontent.com/assets/4172932/12625331/571a48fe-c559-11e5-8aa0-f9aacfb8c1cb.jpg) +## Docker + +```sh +# For Development +# service restarts on file change +1. bash bin/development.sh +``` + ## A Boilerplate-only Option If you would prefer not to use any of our tooling, delete the following files from the project: `package.json`, `gulpfile.babel.js`, `.eslintrc` and `.travis.yml`. You can now safely use the boilerplate with an alternative build-system or no build-system at all if you choose. diff --git a/bin/development.sh b/bin/development.sh new file mode 100644 index 00000000..05cae5a8 --- /dev/null +++ b/bin/development.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +# --build: Build images before starting containers. +# --abort-on-container-exit: Stops all containers if any container is stopped +docker-compose up --build --abort-on-container-exit diff --git a/bin/test.sh b/bin/test.sh new file mode 100644 index 00000000..241084ac --- /dev/null +++ b/bin/test.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +# --build: Build images before starting containers. +# --abort-on-container-exit: Stops all containers if any container is stopped +docker-compose -f 'docker-compose.test.yml' -p ci up --build --abort-on-container-exit +exit $(docker wait ci_express-mongoose-es6-rest-api_1) diff --git a/docker-compose.test.yml b/docker-compose.test.yml new file mode 100644 index 00000000..cff969cf --- /dev/null +++ b/docker-compose.test.yml @@ -0,0 +1,42 @@ +version: '2' + +services: + express-mongoose-es6-rest-api: + build: + context: . + + image: express-mongoose-es6-rest-api:latest + + volumes: + # Mounts the project directory on the host to /app inside the container, + # allowing you to modify the code without having to rebuild the image. + - .:/app + # Just specify a path and let the Engine create a volume. + # Data present in the base image at the specified mount point will be copied + # over to the new volume upon volume initialization. + # node_modules from this new volume will be used and not from your local dev env. + - /app/node_modules/ + + # Set environment variables from this file + env_file: + - .env + + # Overwrite any env var defined in .env file (if required) + environment: + - MONGO_HOST=mongodb://mongo/express-mongoose-es6-rest-api-test + - DEBUG=express-mongoose-es6-rest-api:* + + # Link to containers in another service. + # Links also express dependency between services in the same way as depends_on, + # so they determine the order of service startup. + links: + - mongo + + command: + - /bin/bash + - -c + - yarn --pure-lockfile && yarn test + mongo: + image: "mongo:3.4.2" + ports: + - "27017:27017" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..083b52e5 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,38 @@ +version: '2' + +services: + express-mongoose-es6-rest-api: + build: + context: . + volumes: + # Mounts the project directory on the host to /app inside the container, + # allowing you to modify the code without having to rebuild the image. + - .:/app + # Just specify a path and let the Engine create a volume. + # Data present in the base image at the specified mount point will be copied + # over to the new volume upon volume initialization. + # node_modules from this new volume will be used and not from your local dev env. + - /app/node_modules/ + + # Expose ports [HOST:CONTAINER} + ports: + - "4040:4040" + + # Set environment variables from this file + env_file: + - .env + + # Overwrite any env var defined in .env file (if required) + environment: + - MONGO_HOST=mongodb://mongo/express-mongoose-es6-rest-api-development + - DEBUG=express-mongoose-es6-rest-api:* + + # Link to containers in another service. + # Links also express dependency between services in the same way as depends_on, + # so they determine the order of service startup. + links: + - mongo + mongo: + image: "mongo:3.4.2" + ports: + - "27017:27017" diff --git a/index.js b/index.js index c995d24a..01e808c1 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ mongoose.Promise = Promise; const mongoUri = config.mongo.host; mongoose.connect(mongoUri, { server: { socketOptions: { keepAlive: 1 } } }); mongoose.connection.on('error', () => { - throw new Error(`unable to connect to database: ${config.db}`); + throw new Error(`unable to connect to database: ${mongoUri}`); }); // print mongoose logs in dev env @@ -32,7 +32,7 @@ if (config.MONGOOSE_DEBUG) { if (!module.parent) { // listen on port config.port app.listen(config.port, () => { - debug(`server started on port ${config.port} (${config.env})`); + console.info(`server started on port ${config.port} (${config.env})`); // eslint-disable-line no-console }); }