Skip to content

Commit 490b285

Browse files
authored
Add Dockerfile and docker-compose for development (kunalkapadia#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
1 parent eebfabc commit 490b285

10 files changed

+165
-3
lines changed

.dockerignore

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# nyc test coverage
18+
.nyc_output
19+
20+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21+
.grunt
22+
23+
# node-waf configuration
24+
.lock-wscript
25+
26+
# Compiled binary addons (http://nodejs.org/api/addons.html)
27+
build/Release
28+
29+
# Dependency directories
30+
node_modules
31+
jspm_packages
32+
33+
# Optional npm cache directory
34+
.npm
35+
36+
# Optional REPL history
37+
.node_repl_history

.npmrc

-1
This file was deleted.

.yarnrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
save-prefix false

Dockerfile

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# take default image of node boron i.e node 6.x
2+
FROM node:6.10.1
3+
4+
MAINTAINER Kunal Kapadia <[email protected]>
5+
6+
# create app directory in container
7+
RUN mkdir -p /app
8+
9+
# set /app directory as default working directory
10+
WORKDIR /app
11+
12+
# only copy package.json initially so that `RUN yarn` layer is recreated only
13+
# if there are changes in package.json
14+
ADD package.json yarn.lock /app/
15+
16+
# --pure-lockfile: Don’t generate a yarn.lock lockfile
17+
RUN yarn --pure-lockfile
18+
19+
# copy all file from current dir to /app in container
20+
COPY . /app/
21+
22+
# expose port 4040
23+
EXPOSE 4040
24+
25+
# cmd to start service
26+
CMD [ "yarn", "start" ]

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ Get code coverage summary on executing `yarn test`
143143
`yarn test` also generates HTML code coverage report in `coverage/` directory. Open `lcov-report/index.html` to view it.
144144
![Code coverage HTML report](https://cloud.githubusercontent.com/assets/4172932/12625331/571a48fe-c559-11e5-8aa0-f9aacfb8c1cb.jpg)
145145

146+
## Docker
147+
148+
```sh
149+
# For Development
150+
# service restarts on file change
151+
1. bash bin/development.sh
152+
```
153+
146154
## A Boilerplate-only Option
147155

148156
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.

bin/development.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
# --build: Build images before starting containers.
4+
# --abort-on-container-exit: Stops all containers if any container is stopped
5+
docker-compose up --build --abort-on-container-exit

bin/test.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
# --build: Build images before starting containers.
4+
# --abort-on-container-exit: Stops all containers if any container is stopped
5+
docker-compose -f 'docker-compose.test.yml' -p ci up --build --abort-on-container-exit
6+
exit $(docker wait ci_express-mongoose-es6-rest-api_1)

docker-compose.test.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
version: '2'
2+
3+
services:
4+
express-mongoose-es6-rest-api:
5+
build:
6+
context: .
7+
8+
image: express-mongoose-es6-rest-api:latest
9+
10+
volumes:
11+
# Mounts the project directory on the host to /app inside the container,
12+
# allowing you to modify the code without having to rebuild the image.
13+
- .:/app
14+
# Just specify a path and let the Engine create a volume.
15+
# Data present in the base image at the specified mount point will be copied
16+
# over to the new volume upon volume initialization.
17+
# node_modules from this new volume will be used and not from your local dev env.
18+
- /app/node_modules/
19+
20+
# Set environment variables from this file
21+
env_file:
22+
- .env
23+
24+
# Overwrite any env var defined in .env file (if required)
25+
environment:
26+
- MONGO_HOST=mongodb://mongo/express-mongoose-es6-rest-api-test
27+
- DEBUG=express-mongoose-es6-rest-api:*
28+
29+
# Link to containers in another service.
30+
# Links also express dependency between services in the same way as depends_on,
31+
# so they determine the order of service startup.
32+
links:
33+
- mongo
34+
35+
command:
36+
- /bin/bash
37+
- -c
38+
- yarn --pure-lockfile && yarn test
39+
mongo:
40+
image: "mongo:3.4.2"
41+
ports:
42+
- "27017:27017"

docker-compose.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
version: '2'
2+
3+
services:
4+
express-mongoose-es6-rest-api:
5+
build:
6+
context: .
7+
volumes:
8+
# Mounts the project directory on the host to /app inside the container,
9+
# allowing you to modify the code without having to rebuild the image.
10+
- .:/app
11+
# Just specify a path and let the Engine create a volume.
12+
# Data present in the base image at the specified mount point will be copied
13+
# over to the new volume upon volume initialization.
14+
# node_modules from this new volume will be used and not from your local dev env.
15+
- /app/node_modules/
16+
17+
# Expose ports [HOST:CONTAINER}
18+
ports:
19+
- "4040:4040"
20+
21+
# Set environment variables from this file
22+
env_file:
23+
- .env
24+
25+
# Overwrite any env var defined in .env file (if required)
26+
environment:
27+
- MONGO_HOST=mongodb://mongo/express-mongoose-es6-rest-api-development
28+
- DEBUG=express-mongoose-es6-rest-api:*
29+
30+
# Link to containers in another service.
31+
# Links also express dependency between services in the same way as depends_on,
32+
# so they determine the order of service startup.
33+
links:
34+
- mongo
35+
mongo:
36+
image: "mongo:3.4.2"
37+
ports:
38+
- "27017:27017"

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mongoose.Promise = Promise;
1717
const mongoUri = config.mongo.host;
1818
mongoose.connect(mongoUri, { server: { socketOptions: { keepAlive: 1 } } });
1919
mongoose.connection.on('error', () => {
20-
throw new Error(`unable to connect to database: ${config.db}`);
20+
throw new Error(`unable to connect to database: ${mongoUri}`);
2121
});
2222

2323
// print mongoose logs in dev env
@@ -32,7 +32,7 @@ if (config.MONGOOSE_DEBUG) {
3232
if (!module.parent) {
3333
// listen on port config.port
3434
app.listen(config.port, () => {
35-
debug(`server started on port ${config.port} (${config.env})`);
35+
console.info(`server started on port ${config.port} (${config.env})`); // eslint-disable-line no-console
3636
});
3737
}
3838

0 commit comments

Comments
 (0)