Skip to content

Commit

Permalink
more lecture apps
Browse files Browse the repository at this point in the history
  • Loading branch information
BretFisher committed Feb 27, 2017
1 parent 1296cba commit 1abd322
Show file tree
Hide file tree
Showing 19 changed files with 420 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# showing a few improper ways to do things

# order of things: package managers before adding code. high changes at bottom
# too many run commands
# not cleaning up after pkg installs and tmp files
# installing multiple services per dockerfile

# example of bad: http://pastebin.com/f6YhKmQ2
# http://stackoverflow.com/questions/42079122/how-to-install-nvm-in-a-dockerfile#comment71332110_42079122
13 changes: 13 additions & 0 deletions compose-file-sample/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3.1'

services:
proxy:
image: nginx:1.11 # this will use the latest version of 1.11.x
ports:
- '80:80' # expose 80 on host and sent to 80 in container
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
web:
image: httpd # this will use httpd:latest
expose:
- '80' # on expose port 80 to other containers on it's bridge/overlay network
15 changes: 15 additions & 0 deletions compose-file-sample/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
server {

listen 80;

location / {

proxy_pass http://web;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;

}
}
30 changes: 30 additions & 0 deletions dockerfile-assignment-1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# 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 directory
# https://docs.npmjs.com/cli/shrinkwrap#caveats
node_modules

# Debug log from npm
npm-debug.log
32 changes: 32 additions & 0 deletions dockerfile-assignment-1/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch on Host",
"program": "${workspaceRoot}/bin/www",
"cwd": "${workspaceRoot}"
},
{
"type": "node2",
"request": "attach",
"name": "Docker 9229",
"port": 9229
},
{
"name": "Docker 5858",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"localRoot": "${workspaceRoot}",
"remoteRoot": "/usr/src/app"
}
]
}
27 changes: 27 additions & 0 deletions dockerfile-assignment-1/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# use this empty Dockerfile to build your assignment

# This dir contains a Node.js app, you need to get it running in a container
# No modifications to the app should be necessary, only edit this Dockerfile

# Goals of this assignment
# - you should use the latest 6.x version of the official 'node' image on Docker Hub
# - this app listens on port 3000, but the container should launch on port 80
# so it will respond to http://localhost:80 on your computer
# - create nnew directory /usr/src/app for app source files with 'RUN mkdir -p /usr/src/app'
# - then it needs to first copy in package.json file
# - then it needs to run 'npm install' to install dependencies from that file
# - to keep it clean and small, run 'npm cache clean' after above
# - then it needs to copy in the rest of the files
# - then it needs to start container with command 'node ./bin/www'
# - in the end you should be using FROM, RUN, WORKDIR, COPY, EXPOSE, and CMD commands

FROM node:6-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json package.json
RUN npm install && npm cache clean
COPY . .
EXPOSE 3000
CMD [ "node", "./bin/www" ]

# docker build -t bretfisher/dockerfile-assignment-1 . && docker container run --rm -p 80:3000 -p 5858:5858 -p 9229:9229 bretfisher/dockerfile-assignment-1
46 changes: 46 additions & 0 deletions dockerfile-assignment-1/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
126 changes: 126 additions & 0 deletions dockerfile-assignment-1/bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('dockerfile-assignment-1:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}

//
// need this in docker container to properly exit since node doesn't handle SIGINT/SIGTERM
// this also won't work on using npm start since:
// https://github.com/npm/npm/issues/4603
// https://github.com/npm/npm/pull/10868
// https://github.com/RisingStack/kubernetes-graceful-shutdown-example/blob/master/src/index.js
// if you want to use npm then start with `docker run --init` to help, but I still don't think it's
// a graceful shutdown of node process
//

// quit on ctrl-c when running docker in terminal
process.on('SIGINT', function onSigint () {
console.info('Got SIGINT (aka ctrl-c in docker). Graceful shutdown ', new Date().toISOString());
shutdown();
});

// quit properly on docker stop
process.on('SIGTERM', function onSigterm () {
console.info('Got SIGTERM (docker container stop). Graceful shutdown ', new Date().toISOString());
shutdown();
})

// shut down server
function shutdown() {
server.close(function onServerClosed (err) {
if (err) {
console.error(err);
process.exitCode = 1;
}
process.exit();
})
}
//
// need above in docker container to properly exit
//
17 changes: 17 additions & 0 deletions dockerfile-assignment-1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "dockerfile-assignment-1",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.16.0",
"cookie-parser": "~1.4.3",
"debug": "~2.6.0",
"express": "~4.14.1",
"hbs": "~4.0.1",
"morgan": "~1.7.0",
"serve-favicon": "~2.3.2"
}
}
Binary file added dockerfile-assignment-1/public/images/picard.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions dockerfile-assignment-1/public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
9 changes: 9 additions & 0 deletions dockerfile-assignment-1/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Node.js Express App' });
});

module.exports = router;
9 changes: 9 additions & 0 deletions dockerfile-assignment-1/routes/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});

module.exports = router;
3 changes: 3 additions & 0 deletions dockerfile-assignment-1/views/error.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>{{message}}</h1>
<h2>{{error.status}}</h2>
<pre>{{error.stack}}</pre>
4 changes: 4 additions & 0 deletions dockerfile-assignment-1/views/index.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>{{title}}</h1>
<p>It Worked! You Deserve The Captain's Applause</p>

<img src="/images/picard.gif" />
10 changes: 10 additions & 0 deletions dockerfile-assignment-1/views/layout.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
{{{body}}}
</body>
</html>
Loading

0 comments on commit 1abd322

Please sign in to comment.