Skip to content

Instantly share code, notes, and snippets.

@jubianchi
Last active December 4, 2015 11:19
Show Gist options
  • Save jubianchi/d94bf59cc720e126ced8 to your computer and use it in GitHub Desktop.
Save jubianchi/d94bf59cc720e126ced8 to your computer and use it in GitHub Desktop.
console.log("Hello World!");
"use strict";
let http = require('http'),
server = http.createServer((req, res) => {
console.log(req.method + ' ' + req.url);
if (req.url === '/kill') {
process.exit(0);
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(process.env['GREET'] ? process.env['GREET'] : "Hello World!");
});
server.listen(8080);
"use strict";
let fs = require("fs"),
http = require("http");
if (fs.existsSync("/logs") === false) {
console.log("Creating log directory");
fs.mkdirSync("/logs");
}
let log = fs.openSync("/logs/did.log", "a"),
server = http.createServer((req, res) => {
fs.writeSync(log, req.method + " " + req.url + "\n");
if (req.url === "/kill") {
process.exit(0);
}
res.writeHead(200, {"Content-Type": "text/plain"});
res.end(process.env["GREET"] ? process.env["GREET"] : "Hello World!");
});
server.listen(8080);
"use strict";
let fs = require("fs"),
http = require("http");
if (fs.existsSync("/logs") === false) {
console.log("Creating log directory");
fs.mkdirSync("/logs");
}
let log = fs.openSync("/logs/did.log", "a"),
server = http.createServer((req, res) => {
fs.writeSync(log, (process.env["DID"] ? process.env["DID"] : 1) + " " + req.method + " " + req.url + "\n");
if (req.url === "/kill") {
process.exit(0);
}
res.writeHead(200, {"Content-Type": "text/plain"});
res.end(process.env["GREET"] ? process.env["GREET"] : "Hello World!");
});
server.listen(8080);
if [ "$1" = "stop" ]
then
docker rm -f dive-in-docker-haproxy
docker rm -f dive-in-docker-1
docker rm -f dive-in-docker-2
exit 0
fi
docker run -d \
--name dive-in-docker-1 \
--volume $(pwd):/app:ro \
--volume $(pwd):/logs \
--env DID=1 \
dive-in-docker \
/app/03-dive-in-docker.js
docker run -d \
--name dive-in-docker-2 \
--volume $(pwd):/app:ro \
--volume $(pwd):/logs \
--env DID=2 \
--env GREET \
dive-in-docker \
/app/03-dive-in-docker.js
docker run -d \
--name dive-in-docker-haproxy \
--link dive-in-docker-1:did1 \
--link dive-in-docker-2:did2 \
--publish 8888:80 \
--volume $(pwd)/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro \
haproxy
dive-in-docker-1:
image: dive-in-docker
command: /app/03-dive-in-docker.js
environment:
- DID=1
volumes:
- .:/app:ro
- .:/logs
dive-in-docker-2:
image: dive-in-docker
command: /app/03-dive-in-docker.js
environment:
- DID=2
- GREET
volumes:
- .:/app:ro
- .:/logs
dive-in-docker-haproxy:
image: haproxy
links:
- dive-in-docker-1:did1
- dive-in-docker-2:did2
ports:
- 8888:80
volumes:
- ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
FROM jubianchi/docker-node-4
RUN opkg-cl update && \
opkg-cl upgrade
ADD ./ /app
EXPOSE 8080
CMD ["/app/00-dive-in-docker.js"]
FROM jubianchi/docker-node-4
RUN opkg-cl update && \
opkg-cl upgrade
RUN curl -s \
http://nodejs.org/dist/v5.1.0/node-v5.1.0-linux-x64.tar.gz \
| gunzip | tar -xf - -C /
ADD ./ /app
EXPOSE 8080
ENTRYPOINT ["/node-v5.1.0-linux-x64/bin/node"]
CMD ["/app/01-dive-in-docker.js"]
defaults
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
option httplog
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout http-request 15s
timeout check 10s
maxconn 3000
listen stats
bind *:8080
mode http
stats enable
stats uri /
frontend all
bind *:80
mode http
acl acl_stats path_beg /stats
default_backend dive_in_docker
use_backend haproxy_stats if acl_stats
backend dive_in_docker
mode http
option forwardfor
balance roundrobin
option httpchk HEAD /
server srv_did_1 did1:8080 check inter 10s
server srv_did_2 did2:8080 check inter 10s
backend haproxy_stats
mode http
option forwardfor
server ha_1 127.0.0.1:8080 check inter 1s
@jubianchi
Copy link
Author

docker run --rm \
            --volume $(pwd):/app \
            --publish 8888:8080/tcp \
            --name dive-in-docker \
            jubianchi/docker-node-4:latest \
            /app/01-dive-in-docker.js

@jubianchi
Copy link
Author

export GREET='Hello Docker!'
docker run --rm \
            --volume $(pwd):/app \
            --env GREET \
            --publish 8888:8080/tcp \
            --name dive-in-docker \
            jubianchi/docker-node-4:latest \
            /app/01-dive-in-docker.js

@jubianchi
Copy link
Author

GREET='Hello Docker!' ./03-dive-in-docker.sh
./03-dive-in-docker.sh stop
GREET='Hello Docker!' docker-compose up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment