Skip to content

Commit e3b0e54

Browse files
committed
setupping docker file assignment 1
1 parent b8bb7e4 commit e3b0e54

File tree

16 files changed

+361
-2
lines changed

16 files changed

+361
-2
lines changed

docker-2.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ docker image build -t customnginx .
145145
146146
docker image ls
147147
148-
vim Dockerfi le
148+
vim Dockerfile
149149
Or,
150150
nano Dockerfile
151151
@@ -156,27 +156,61 @@ docker image build -t customnginx .
156156
```
157157
cd dockerfile-sample-2
158158
159+
Run: ll (LL small letter)
160+
159161
vim Dockerfile
160162
161163
docker container run -p 80:80 --rm nginx
164+
Or,
165+
docker container run -p 8080:80 --rm nginx
162166
163167
docker image build -t nginx-with-html .
164168
165169
docker container run -p 80:80 --rm nginx-with-html
170+
Or,
171+
docker container run -p 8080:80 --rm nginx-with-html
172+
173+
If you run 8080:80 prt then go to http://localhost:8080/ because only localhost will not work and we will not see our inde html file updates.
166174
167175
docker image ls
168176
169177
docker image tag --help
170178
171-
docker image tag nginx-with-html:latest bretfisher/nginx-with-html:latest
179+
docker image tag nginx-with-html:latest codersh/nginx-with-html:latest
172180
173181
docker image ls
174182
175183
docker push
176184
```
185+
186+
187+
* Which of the following is an example of a container image that you can run?
188+
189+
Answer: nginx
190+
191+
* Which flag would you pass to specify a docker build referencing a file other than the default 'Dockerfile'?
192+
193+
ANSWER: -f
194+
195+
* Which Dockerfile stanza (Command) is best to use for changing the directory in a Dockerfile?
196+
197+
ANSWER: WORKDIR
198+
199+
177200
## Build Your Own Dockerfile and Run Containers :
178201

179202
* Building our own image is quite fun part of the container images.
203+
* Docker file are part process workflow and part art.
204+
* Take exisiting Node.js app and dockerize it.
205+
* Make Dockerfile, build it, test it, push it, (rm it), Run it.
206+
* Expect this to be iterative, rarely do I get it right the first time.
207+
* Details in dockerfile-assignment-1.Dockerfile.
208+
* Use the Alphine version of the official 'node' 6.x image.
209+
* Expected result is web site at http://localhost or, http://localhost:8080
210+
* Tag and Push to your docker hub account.
211+
* Remove the image from local cache, run again from hub.
212+
213+
180214
```
181215
cd dockerfile-assignment-1
182216

dockerfile-assignment-1/.dockerignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# add git-ignore syntax here of things you don't want copied into docker image
2+
3+
# for all code you usually don't want .git history in image, just the current commit you have checked out
4+
.git
5+
6+
# you usually don't want dockerfile and compose files in the image either
7+
*Dockerfile*
8+
*docker-compose*
9+
10+
# for Node.js apps, you want to build the node_modules in the image, and not copy from host
11+
node_modules

dockerfile-assignment-1/.gitignore

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# node-waf configuration
20+
.lock-wscript
21+
22+
# Compiled binary addons (http://nodejs.org/api/addons.html)
23+
build/Release
24+
25+
# Dependency directory
26+
# https://docs.npmjs.com/cli/shrinkwrap#caveats
27+
node_modules
28+
29+
# Debug log from npm
30+
npm-debug.log
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
// Use IntelliSense to learn about possible Node.js debug attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch on Host",
11+
"program": "${workspaceRoot}/bin/www",
12+
"cwd": "${workspaceRoot}"
13+
},
14+
{
15+
"type": "node2",
16+
"request": "attach",
17+
"name": "Docker 9229",
18+
"port": 9229
19+
},
20+
{
21+
"name": "Docker 5858",
22+
"type": "node",
23+
"request": "attach",
24+
"port": 5858,
25+
"address": "localhost",
26+
"restart": false,
27+
"sourceMaps": false,
28+
"localRoot": "${workspaceRoot}",
29+
"remoteRoot": "/usr/src/app"
30+
}
31+
]
32+
}

dockerfile-assignment-1/Dockerfile

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# use this empty Dockerfile to build your assignment
2+
3+
# This dir contains a Node.js app, you need to get it running in a container
4+
# No modifications to the app should be necessary, only edit this Dockerfile
5+
6+
# Overview of this assignment
7+
# use the instructions from developer below to create a working Dockerfile
8+
# feel free to add command inline below or use a new file, up to you (but must be named Dockerfile)
9+
# once Dockerfile builds correctly, start container locally to make sure it works on http://localhost
10+
# then ensure image is named properly for your Docker Hub account with a new repo name
11+
# push to Docker Hub, then go to https://hub.docker.com and verify
12+
# then remove local image from cache
13+
# then start a new container from your Hub image, and watch how it auto downloads and runs
14+
# test again that it works at http://localhost
15+
16+
17+
# Instructions from the app developer
18+
# - you should use the 'node' official image, with the alpine 6.x branch (node:6-alpine)
19+
# yes this is a 2-year old image of node, but all official images are always
20+
# available on Docker Hub forever, to ensure even old apps still work.
21+
# It is common to still need to deploy old app versions, even years later.
22+
# - this app listens on port 3000, but the container should launch on port 80
23+
# so it will respond to http://localhost:80 on your computer
24+
# - then it should use alpine package manager to install tini: 'apk add --update tini'
25+
# - then it should create directory /usr/src/app for app files with 'mkdir -p /usr/src/app'
26+
# - Node uses a "package manager", so it needs to copy in package.json file
27+
# - then it needs to run 'npm install' to install dependencies from that file
28+
# - to keep it clean and small, run 'npm cache clean --force' after above
29+
# - then it needs to copy in all files from current directory
30+
# - then it needs to start container with command '/sbin/tini -- node ./bin/www'
31+
# - in the end you should be using FROM, RUN, WORKDIR, COPY, EXPOSE, and CMD commands
32+
33+
# Bonus Extra Credit
34+
# this will not have you setting up a complete image useful for local development, test, and prod
35+
# it's just meant to get you started with basic Dockerfile concepts and not focus too much on
36+
# proper Node.js use in a container. **If you happen to be a Node.js Developer**, then
37+
# after you get through more of this course, you should come back and use my
38+
# Node Docker Good Defaults sample project on GitHub to change this Dockerfile for
39+
# better local development with more advanced topics
40+
# https://github.com/BretFisher/node-docker-good-defaults
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM node:6-alpine
2+
3+
EXPOSE 3000
4+
5+
RUN apk add --update tini
6+
7+
WORKDIR /usr/src/app
8+
9+
COPY package.json package.json
10+
11+
RUN npm install && npm cache clean --force
12+
13+
COPY . .
14+
15+
CMD [ "/sbin/tini", "--", "node", "./bin/www" ]

dockerfile-assignment-1/app.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var express = require('express');
2+
var path = require('path');
3+
var favicon = require('serve-favicon');
4+
var logger = require('morgan');
5+
var cookieParser = require('cookie-parser');
6+
var bodyParser = require('body-parser');
7+
8+
var index = require('./routes/index');
9+
var users = require('./routes/users');
10+
11+
var app = express();
12+
13+
// view engine setup
14+
app.set('views', path.join(__dirname, 'views'));
15+
app.set('view engine', 'hbs');
16+
17+
// uncomment after placing your favicon in /public
18+
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
19+
app.use(logger('dev'));
20+
app.use(bodyParser.json());
21+
app.use(bodyParser.urlencoded({ extended: false }));
22+
app.use(cookieParser());
23+
app.use(express.static(path.join(__dirname, 'public')));
24+
25+
app.use('/', index);
26+
app.use('/users', users);
27+
28+
// catch 404 and forward to error handler
29+
app.use(function(req, res, next) {
30+
var err = new Error('Not Found');
31+
err.status = 404;
32+
next(err);
33+
});
34+
35+
// error handler
36+
app.use(function(err, req, res, next) {
37+
// set locals, only providing error in development
38+
res.locals.message = err.message;
39+
res.locals.error = req.app.get('env') === 'development' ? err : {};
40+
41+
// render the error page
42+
res.status(err.status || 500);
43+
res.render('error');
44+
});
45+
46+
module.exports = app;

dockerfile-assignment-1/bin/www

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var debug = require('debug')('dockerfile-assignment-1:server');
9+
var http = require('http');
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
var port = normalizePort(process.env.PORT || '3000');
16+
app.set('port', port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
var server = http.createServer(app);
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on('error', onError);
30+
server.on('listening', onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
var port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== 'listen') {
58+
throw error;
59+
}
60+
61+
var bind = typeof port === 'string'
62+
? 'Pipe ' + port
63+
: 'Port ' + port;
64+
65+
// handle specific listen errors with friendly messages
66+
switch (error.code) {
67+
case 'EACCES':
68+
console.error(bind + ' requires elevated privileges');
69+
process.exit(1);
70+
break;
71+
case 'EADDRINUSE':
72+
console.error(bind + ' is already in use');
73+
process.exit(1);
74+
break;
75+
default:
76+
throw error;
77+
}
78+
}
79+
80+
/**
81+
* Event listener for HTTP server "listening" event.
82+
*/
83+
84+
function onListening() {
85+
var addr = server.address();
86+
var bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}
91+

dockerfile-assignment-1/package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "dockerfile-assignment-1",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"start": "node ./bin/www"
7+
},
8+
"dependencies": {
9+
"body-parser": "^1.18.3",
10+
"cookie-parser": "~1.4.3",
11+
"debug": "~2.6.0",
12+
"express": "^4.16.4",
13+
"hbs": "~4.0.1",
14+
"morgan": "^1.9.1",
15+
"serve-favicon": "^2.5.0"
16+
}
17+
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
body {
2+
padding: 50px;
3+
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4+
}
5+
6+
a {
7+
color: #00B7FF;
8+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var express = require('express');
2+
var router = express.Router();
3+
4+
/* GET home page. */
5+
router.get('/', function(req, res, next) {
6+
res.render('index', { title: 'Node.js Express App' });
7+
});
8+
9+
module.exports = router;
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var express = require('express');
2+
var router = express.Router();
3+
4+
/* GET users listing. */
5+
router.get('/', function(req, res, next) {
6+
res.send('respond with a resource');
7+
});
8+
9+
module.exports = router;
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>{{message}}</h1>
2+
<h2>{{error.status}}</h2>
3+
<pre>{{error.stack}}</pre>

0 commit comments

Comments
 (0)