Skip to content

Commit

Permalink
Add Dockerfile and docker-compose for development (#281)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kunalkapadia committed Apr 4, 2017
1 parent eebfabc commit 490b285
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 3 deletions.
37 changes: 37 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

1 change: 1 addition & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
save-prefix false
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# take default image of node boron i.e node 6.x
FROM node:6.10.1

MAINTAINER Kunal Kapadia <kunalkapadia12@gmail.com>

# 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" ]
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions bin/development.sh
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions bin/test.sh
Original file line number Diff line number Diff line change
@@ -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)
42 changes: 42 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -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"
38 changes: 38 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
});
}

Expand Down

0 comments on commit 490b285

Please sign in to comment.