Last active
October 25, 2015 18:43
-
-
Save zackferrofields/b48ff98eb26895b6030c to your computer and use it in GitHub Desktop.
Docker compose nginx, node & redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./app/Dockerfile | |
FROM node:4.1.0 | |
WORKDIR /src/app | |
ADD package.json package.json | |
RUN npm install |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./app/server.js | |
let http = require('http'); | |
let redis = require('redis'); | |
const PORT = process.env.PORT || 3000; | |
const DB_HOST = process.env.REDIS_PORT_6379_TCP_ADDR || '127.0.01'; | |
const DB_PORT = process.env.REDIS_PORT_6379_TCP_PORT || 6379; | |
const KEY_COUNTER = 'counter'; | |
let client = redis.createClient(DB_PORT, DB_HOST); | |
/** | |
* @summary Increments and return a counter | |
* @public | |
* @function increment | |
* | |
* @returns {Promise} | |
* | |
* @example | |
* increment() | |
* .then(count => console.log(count)); | |
* > 1 | |
*/ | |
function increment() { | |
return new Promise((resolve, reject) => client.incr(KEY_COUNTER, (err, reply) => { | |
if (err) return reject(err); | |
return resolve(reply); | |
})); | |
} | |
let server = http.createServer((request, response) => { | |
response.writeHead(200, {'Content-Type': 'text/html'}); | |
response.write('<!DOCTYPE html>'); | |
increment() | |
.then(count => { | |
response.write(`This page has been viewed ${count} times!`); | |
response.end(); | |
}) | |
.catch( err => { | |
response.write('Hello World!'); | |
response.end(); | |
}) | |
}); | |
server.listen(PORT, () => { | |
process.stdout.write(`Listening on port: ${PORT} \n`); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./docker-compose.yml | |
nginx: | |
build: ./nginx | |
links: | |
- node1:node1 | |
- node2:node2 | |
ports: | |
- "80:80" | |
node1: | |
build: ./app | |
volumes: | |
- "./app:/src/app" | |
ports: | |
- "3000" | |
links: | |
- redis | |
command: node server | |
node2: | |
build: ./app | |
volumes: | |
- "./app:/src/app" | |
ports: | |
- "3000" | |
links: | |
- redis | |
command: node server | |
redis: | |
image: redis | |
ports: | |
- "6379" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./nginx/Dockerfile | |
FROM nginx:latest | |
COPY nginx.conf /etc/nginx/nginx.conf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./nginx/nginx.conf | |
worker_processes 3; | |
events { worker_connections 1024; } | |
http { | |
upstream node-app { | |
least_conn; | |
server node1:3000 weight=10 max_fails=3 fail_timeout=30s; | |
server node2:3000 weight=10 max_fails=3 fail_timeout=30s; | |
} | |
server { | |
listen 80; | |
location / { | |
proxy_pass http://node-app; | |
proxy_http_version 1.1; | |
proxy_set_header Upgrade $http_upgrade; | |
proxy_set_header Connection 'upgrade'; | |
proxy_set_header Host $host; | |
proxy_cache_bypass $http_upgrade; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment