Skip to content

Commit

Permalink
adding docker and compose requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
BretFisher committed Nov 16, 2017
1 parent 81688de commit 7377f1a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# add git-ignore syntax here of things you don't want copied into docker image

.git
*Dockerfile*
*docker-compose*
node_modules
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# if you're doing anything beyond your local machine, please pin this to a specific version at https://hub.docker.com/_/node/
FROM node:8

RUN mkdir -p /opt/app
# set our node environment, either development or production
# defaults to production, compose overrides this to development on build and run
ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV

# default to port 80 for node, and 5858 or 9229 for debug
ARG PORT=80
ENV PORT $PORT
EXPOSE $PORT 5858 9229

# check every 30s to ensure this service returns HTTP 200
HEALTHCHECK CMD curl -fs http://localhost:$PORT/ || exit 1

# install dependencies first, in a different location for easier app bind mounting for local development
WORKDIR /opt
COPY package.json package-lock.json* ./
RUN npm install && npm cache clean --force
ENV PATH /opt/node_modules/.bin:$PATH

# run bower
ENV BOWER_PATH /opt
COPY ./bower.json .
RUN bower install --allow-root

# copy in our source code last, as it changes the most
WORKDIR /opt/app
COPY . /opt/app

# if you want to use npm start instead, then use `docker run --init in production`
# so that signals are passed properly. Note the code in index.js is needed to catch Docker signals
# using node here is still more graceful stopping then npm with --init afaik
# I still can't come up with a good production way to run with npm and graceful shutdown
CMD [ "node", "app.js" ]
10 changes: 8 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ var path = require('path'),

var app = express();

// allow custom bower install location with default
var bowerComponents = path.join(__dirname + '/bower_components');
if (process.env.BOWER_PATH) {
bowerComponents = path.join(process.env.BOWER_PATH + '/bower_components');
};

app.use(express.static('public'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.use('/bower_components', express.static(bowerComponents));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
Expand Down Expand Up @@ -43,5 +49,5 @@ app.get('/search/:loc', function(req, res) {
});

app.listen(process.env.PORT || 3000, function() {
console.log('Example app listening on port 3000!');
console.log('App listening on port ' + process.env.PORT);
});
30 changes: 30 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '3.1'

services:
web:
build:
context: .
args:
- NODE_ENV=development
# you can use legacy debug config or new inspect
# NOTE: if nodemon isn't restarting on changes, you might be on Windows
# which has trouble seeing file changes, so add -L to use legacy polling
# https://github.com/remy/nodemon#application-isnt-restarting
#command: ../node_modules/.bin/nodemon --debug=0.0.0.0:5858
command: ../node_modules/.bin/nodemon --inspect=0.0.0.0:9229
ports:
- "80:80"
- "5858:5858"
- "9229:9229"
volumes:
- .:/opt/app
# this is a workaround to prevent host node_modules from accidently getting mounted in container
# in case you want to use node/npm both outside container for test/lint etc. and also inside container
# this will overwrite the default node_modules dir in container so it won't conflict with our
# /opt/node_modules location. Thanks to PR from @brnluiz
- notused:/opt/app/node_modules
environment:
- NODE_ENV=development

volumes:
notused:
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"chai": "^4.1.1",
"express": "^4.15.3",
"lodash": "^4.17.4",
"nodemon": "^1.12.1",
"pug": "^2.0.0-rc.2"
},
"scripts": {
Expand Down

0 comments on commit 7377f1a

Please sign in to comment.