Permalink
Browse files

Merge branch 'makefile'

  • Loading branch information...
Pete Saia
Pete Saia committed Apr 2, 2015
2 parents 5810b04 + 52fb661 commit 7da978bb8fc8d5fa4b69d1a5fdef879aba4981db
View
@@ -1 +1,2 @@
node_modules
bower_components
View
@@ -1,3 +1,5 @@
node_modules
bower_components
.DS_Store
backups/
env_make
View
@@ -1,5 +1,4 @@
# https://registry.hub.docker.com/_/nginx/
FROM nginx
FROM ubuntu:14.04
# whoami
MAINTAINER Pete Saia, pete@lev-interactive.com
@@ -11,7 +10,7 @@ RUN apt-get update && \
apt-get install -y nodejs
# Install globals we need for npm to build and run the project.
RUN npm install -g forever bower grunt-cli node-gyp
RUN npm config set python python2.7 && npm install -g forever bower grunt-cli node-gyp
# Var for express/node. You'd want to overwrite this when running
# in staging or production. Overwrite on the run command.
@@ -20,34 +19,29 @@ ENV NODE_ENV development
# Port to run the app on.
ENV APP_PORT 8080
# Port to run the app on.
ENV LOGS_ROOT /src/logs
# Where the app libs on the host (container).
ENV APP_ROOT /src/app
# Where the logs live on the host (container).
ENV LOGS_ROOT /src/logs
# Copy the local app to the host.
COPY src/ $APP_ROOT
# Go to the app root.
WORKDIR $APP_ROOT
# Prep the app. This would be a good place to build assets and whatnot.
RUN mkdir $LOGS_ROOT && npm cache clean && npm install
# Setup nginx config files.
COPY nginx/sites-enabled /etc/nginx/sites-enabled
COPY nginx/nginx.conf /etc/nginx/nginx.conf
RUN npm install && mkdir -p $LOGS_ROOT /var/backups
# Expose the port that the app runs on so it can be bound.
EXPOSE $APP_PORT
# Start the app. Notice forever didn't start it with `start`
# so it wouldn't go into the background. Important that it stays
# in the foreground.
CMD forever start -a -w -v \
CMD forever -w --watchDirectory . --watchIgnore *.log \
-l ${LOGS_ROOT}/app.forever.log \
-o ${LOGS_ROOT}/app.stdout.log \
-e ${LOGS_ROOT}/app.stderr.log \
index.js && \
nginx -g "daemon off;"
index.js
View
156 Makefile
@@ -0,0 +1,156 @@
#
# Environment specific vars. Defaults set below.
#
include env_make
#
# Phony targets.
#
.PHONY: help build run-db run-dev dump-db import-db run-release destroy
#
# Iterated after major releases.
#
VERSION ?= 1.0.0
#
# App image name: <user>/<appname>
#
APP_IMAGE_NAME ?= lev-interactive/myapp
#
# Set NODE_ENV for node/express.
#
NODE_ENV ?= development
#
# Main app container name.
#
APP_CONTAINER_NAME ?= myapp_container
#
# Container for database.
#
MONGO_CONTAINER_NAME ?= myapp_mongo_container
#
# Container for redis.
#
REDIS_CONTAINER_NAME ?= myapp_redis_container
#
# Container for nginx.
#
NGINX_CONTAINER_NAME ?= myapp_nginx_container
#
# Backup directory for db exports.
#
BACKUPS ?= backups
#
# Current directory.
#
CURDIR = $(shell pwd)
#
# Last backed up mongodb dump.
#
LAST_DUMP := $(shell ls -t $(CURDIR)/$(BACKUPS)/mongo | head -1)
help:
@echo "\nApplication management. Please make sure you have the env_make file setup.\n"
@echo "Usage: \n\
make build This builds the $(APP_IMAGE_NAME) image. \n\
make run-dev This will start the application mapping port 80 to 3030. All src \n\
files will be volumed as well for automatic restarts.\n\
be working in for instant changes. Runs on port 3000. \n\
make run-release This will run a container without the volumes on port 80. Good \n\
for production. @TODO \n\
make save This will save the database in the backups directory. \n\
make restore This will restore from the last time you saved. \n\
make destroy Stops and removes all running containers. \
"
build:
@docker build -t $(APP_IMAGE_NAME):$(VERSION) --rm .
save:
@if ! docker ps -a | grep -qF $(MONGO_CONTAINER_NAME); then \
echo "No mongo container is running."; exit 1; \
fi
@mkdir -p $(BACKUPS) $(BACKUPS)/mongo
@docker exec $(MONGO_CONTAINER_NAME) mongodump \
--out /var/backups/mongo/dump-`date +%Y-%m-%d:%H:%M:%S`
restore:
docker exec $(MONGO_CONTAINER_NAME) mongorestore --drop \
/var/backups/mongo/$(LAST_DUMP)
run-dev:
@echo "\n\033[0;33m=> Starting Redis --------------------------------------\033[0m"
@docker run -d \
--restart always \
--name $(REDIS_CONTAINER_NAME) \
redis:latest
@echo "\n\033[0;33m=> Starting Mongo --------------------------------------\033[0m"
@docker run -d \
--restart always \
--name $(MONGO_CONTAINER_NAME) \
-v $(CURDIR)/server-config/mongo/mongo.conf:/etc/mongod.conf \
-v $(CURDIR)/$(BACKUPS):/var/backups \
mongo:3.0
@echo "\n\033[0;33m=> Starting app instances (clustered) --------------------------------------\033[0m"
@docker run -d \
--name $(APP_CONTAINER_NAME)_1 \
--link $(MONGO_CONTAINER_NAME):mongo \
--link $(REDIS_CONTAINER_NAME):redis \
--restart always \
-e NODE_ENV=$(NODE_ENV) \
-v $(CURDIR)/src:/app/src \
$(APP_IMAGE_NAME):$(VERSION)
@docker run -d \
--name $(APP_CONTAINER_NAME)_2 \
--link $(MONGO_CONTAINER_NAME):mongo \
--link $(REDIS_CONTAINER_NAME):redis \
--restart always \
-e NODE_ENV=$(NODE_ENV) \
-v $(CURDIR)/src:/app/src \
$(APP_IMAGE_NAME):$(VERSION)
@docker run -d \
--name $(APP_CONTAINER_NAME)_3 \
--link $(MONGO_CONTAINER_NAME):mongo \
--link $(REDIS_CONTAINER_NAME):redis \
--restart always \
-e NODE_ENV=$(NODE_ENV) \
-v $(CURDIR)/src:/app/src \
$(APP_IMAGE_NAME):$(VERSION)
@echo "\n\033[0;33m=> Starting NGINX --------------------------------------\033[0m"
@docker run -d \
--name $(NGINX_CONTAINER_NAME) \
--link $(APP_CONTAINER_NAME)_1:nodeapp_1 \
--link $(APP_CONTAINER_NAME)_2:nodeapp_2 \
--link $(APP_CONTAINER_NAME)_3:nodeapp_3 \
--restart always \
-p "3030:80" \
-v $(CURDIR)/server-config/nginx/sites-enabled:/etc/nginx/sites-enabled \
-v $(CURDIR)/server-config/nginx/nginx.conf:/etc/nginx/nginx.conf \
nginx:1.7.9
@echo "\n\033[0;33m=> Tailing the nginx logs. --------------------------------------\033[0m"
docker logs -f $(NGINX_CONTAINER_NAME)
destroy:
@echo "\033[0mTearing down environment. \033[0m"
@-docker rmi $(docker images -f "dangling=true" -q)
@-docker stop $(APP_CONTAINER_NAME)_1
@-docker rm -v $(APP_CONTAINER_NAME)_1
@-docker stop $(APP_CONTAINER_NAME)_2
@-docker rm -v $(APP_CONTAINER_NAME)_2
@-docker stop $(APP_CONTAINER_NAME)_3
@-docker rm -v $(APP_CONTAINER_NAME)_3
@-docker stop $(MONGO_CONTAINER_NAME)
@-docker rm -v $(MONGO_CONTAINER_NAME)
@-docker stop $(REDIS_CONTAINER_NAME)
@-docker rm -v $(REDIS_CONTAINER_NAME)
@-docker stop $(NGINX_CONTAINER_NAME)
@-docker rm -v $(NGINX_CONTAINER_NAME)
View

This file was deleted.

Oops, something went wrong.
View
@@ -0,0 +1,2 @@
# Copy this file to env_make.
NODE_ENV=development

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1 @@
@@ -1,15 +1,13 @@
user nginx;
worker_processes 1;
# daemon off; # Don't need to add because it gets appended by the docker image.
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
@@ -25,8 +23,7 @@ http {
keepalive_timeout 65;
#gzip on;
# gzip on;
# include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
@@ -0,0 +1,21 @@
upstream main-application {
least_conn;
server nodeapp_1:8080 weight=10 max_fails=3 fail_timeout=30s;
server nodeapp_2:8080 weight=10 max_fails=3 fail_timeout=30s;
server nodeapp_3:8080 weight=10 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
access_log /var/log/nginx/nodeapp.access.log;
error_log /var/log/nginx/nodeapp.error.log;
location / {
proxy_pass http://main-application;
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;
}
}
Oops, something went wrong.

0 comments on commit 7da978b

Please sign in to comment.