Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ Dockerfile
.git
README.md
.gitignore
.env
db-init.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would check db-init.js into version control (since it is code that gets executed), and modify the script so it pulls config/sensitive info from the already configured .env file. All config should be centralized and removed from code (and all code that is run should be checked in to VC).

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ typings/
.fusebox/

#DynamoDB Local files
.dynamodb/
.dynamodb/
db-init.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above comment.

4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app

WORKDIR /home/node/app

COPY package*.json ./
COPY app/package*.json ./

USER node

RUN npm install

COPY --chown=node:node . .
COPY --chown=node:node ./app .

EXPOSE 8080

Expand Down
714 changes: 713 additions & 1 deletion README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app.js → app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const db = require('./db');
const sharks = require('./routes/sharks');

const path = __dirname + '/views/';
const port = 8080;
const port = process.env.PORT || 8080;

app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
Expand All @@ -14,5 +14,5 @@ app.use(express.static(path));
app.use('/sharks', sharks);

app.listen(port, function () {
console.log('Example app listening on port 8080!')
console.log(`Example app listening on ${port}!`)
})
File renamed without changes.
25 changes: 25 additions & 0 deletions app/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const mongoose = require('mongoose');

const {
MONGO_USERNAME,
MONGO_PASSWORD,
MONGO_HOSTNAME,
MONGO_PORT,
MONGO_DB
} = process.env;

const options = {
useNewUrlParser: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 500,
connectTimeoutMS: 10000,
};

const url = `mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}?authSource=admin`;

mongoose.connect(url, options).then( function() {
console.log("MongoDB is connected");
})
.catch( function(err) {
console.log(err);
});
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions package.json → app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"ejs": "^2.6.1",
"express": "^4.16.4",
"mongoose": "^5.4.10"
},
"devDependencies": {
"nodemon": "^1.18.10"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
79 changes: 79 additions & 0 deletions app/wait-for.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/sh

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would maybe add a comment in the script linking out to the original on GH.

TIMEOUT=15
QUIET=0

echoerr() {
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
}

usage() {
exitcode="$1"
cat << USAGE >&2
Usage:
$cmdname host:port [-t timeout] [-- command args]
-q | --quiet Do not output any status messages
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
exit "$exitcode"
}

wait_for() {
for i in `seq $TIMEOUT` ; do
nc -z "$HOST" "$PORT" > /dev/null 2>&1

result=$?
if [ $result -eq 0 ] ; then
if [ $# -gt 0 ] ; then
exec "$@"
fi
exit 0
fi
sleep 1
done
echo "Operation timed out" >&2
exit 1
}

while [ $# -gt 0 ]
do
case "$1" in
*:* )
HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
shift 1
;;
-q | --quiet)
QUIET=1
shift 1
;;
-t)
TIMEOUT="$2"
if [ "$TIMEOUT" = "" ]; then break; fi
shift 2
;;
--timeout=*)
TIMEOUT="${1#*=}"
shift 1
;;
--)
shift
break
;;
--help)
usage 0
;;
*)
echoerr "Unknown argument: $1"
usage 1
;;
esac
done

if [ "$HOST" = "" -o "$PORT" = "" ]; then
echoerr "Error: you need to provide a host and port to test."
usage 2
fi

wait_for "$@"
11 changes: 0 additions & 11 deletions db.js

This file was deleted.

49 changes: 49 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
version: '3'

services:
nodejs:
build:
context: .
dockerfile: Dockerfile
image: nodejs
container_name: nodejs
restart: unless-stopped
env_file: .env
environment:
- MONGO_USERNAME=$MONGO_USERNAME
- MONGO_PASSWORD=$MONGO_PASSWORD
- MONGO_HOSTNAME=$MONGO_HOSTNAME
- MONGO_PORT=$MONGO_PORT
- MONGO_DB=$MONGO_DB
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would set the MONGO_HOSTNAME variable to db explicitly here, instead of setting it to db in the config parameter file. Would either leave it blank/unset or omit it from that file entirely. db is a "Compose-level" variable, so referring to it at the Compose level makes more sense and is cleaner, IMO. See comment on Tutorial markdown.

ports:
- "80:8080"
volumes:
- ./app:/home/node/app
- node_modules:/home/node/app/node_modules
depends_on:
- db
networks:
- app-network
command: ./wait-for.sh db:27017 -- /home/node/app/node_modules/.bin/nodemon app.js

db:
image: mongo
container_name: db
restart: unless-stopped
environment:
- MONGO_INITDB_ROOT_USERNAME=user
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would avoid checking these (even more) sensitive parameters into version control. Would create a separate .env file (or use the same file) and externalize these from the Dockerfile.

- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=admin
volumes:
- ./db-init.js:/docker-entrypoint-initdb.d/db-init.js
- dbdata:/data/db
networks:
- app-network

networks:
app-network:
driver: bridge

volumes:
dbdata:
node_modules: