Skip to content

Commit

Permalink
Updated to handle production enviroments
Browse files Browse the repository at this point in the history
  • Loading branch information
taesup committed Mar 8, 2019
1 parent f3519ed commit 2160061
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 89 deletions.
17 changes: 11 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
DOCKERHUB_NAME=your_docker_username
IMAGE_NAME=image_name
IMAGE_VERSION=0.0.0
# Container version
DOCKERHUB_NAME=name
IMAGE_NAME=docker-deploy-demo
IMAGE_VERSION=0.0.1

EXPRESS_CONTAINER_PORT=8080
# Container runtime
PORT=8080
EXPRESS_HOST_PORT=8080
EXPRESS_CONTAINER_PORT=8080

POSTGRES_HOST_PORT=5432
POSTGRES_CONTAINER_PORT=5432
POSTGRES_HOSTNAME=postgres-primary-db
POSTGRES_USER=postgres_user
POSTGRES_USER=username
POSTGRES_PASSWORD=password
POSTGRES_DB=project_db
POSTGRES_DB=database_name

REDIS_HOSTNAME=redis://redis-server:6379
REDIS_HOST_PORT=6379
REDIS_CONTAINER_PORT=6379
SESSION_SECRET=secret
37 changes: 26 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
# Built from Node latest Alpine
FROM node:10.0
FROM node:10.15.1-alpine

# Specify an optional argument with a default value
ARG app_directory=/src/app
ARG NODE_ENV=development
# Update latest security patches
RUN apk update && apk upgrade

# Set the app directory as the context for all commands and entry to the container
WORKDIR ${app_directory}
# RUN adduser webuser -
RUN adduser -D -g '' webuser

# ONLY copy over the package.json to install NPM packages
# SET default APP_DIR path
ARG APP_DIR=/src/app

# Create APP_DIR path and set permissions

RUN mkdir -p $APP_DIR
RUN chown -R webuser:webuser $APP_DIR

# Switch user to non-privileged user
USER webuser

# Change working directory to application directory
WORKDIR $APP_DIR

# Copy package.json to /app directory
COPY package.json .

# Install node module dependencies
# Install node modules/dependencies
RUN npm install

# Add the rest of the project files(most builds will start from here based on cache)
# Copy application code
COPY . .

# Start the node application as you normally would
# Expose this port on DOCKER NETWORK (NOT HOST MAPPING)
EXPOSE 8080

# Start the Express server
CMD ["node", "./server/server.js"]
5 changes: 1 addition & 4 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ services:
command: ["npm", "run", "dev"]
container_name: ${IMAGE_NAME}
volumes:
- ".:/src/app/:rw"
- ".:/src/app"
env_file: .env
environment:
ENVIRONMENT: production
NODE_ENV: production
ports:
- "${EXPRESS_HOST_PORT}:${EXPRESS_CONTAINER_PORT}"
networks:
Expand Down
38 changes: 38 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: "3"
services:
express-server:
image: ${DOCKERHUB_NAME}/${IMAGE_NAME}:${IMAGE_VERSION}
depends_on:
- postgres-primary-db
container_name: ${IMAGE_NAME}
env_file: .env
environment:
ENVIRONMENT: production
NODE_ENV: production
ports:
- "${EXPRESS_HOST_PORT}:${EXPRESS_CONTAINER_PORT}"
networks:
- my-app-network
postgres-primary-db:
image: postgres:10.0-alpine
env_file: .env
volumes:
- pg-data-volume:/var/lib/postgresql/data
ports:
- '${POSTGRES_HOST_PORT}:${POSTGRES_CONTAINER_PORT}'
networks:
- my-app-network
redis-server:
image: redis:4.0-alpine
env_file: .env
volumes:
- redis-data-volume:/data
ports:
- '${REDIS_HOST_PORT}:${REDIS_CONTAINER_PORT}'
networks:
- my-app-network
volumes:
pg-data-volume:
redis-data-volume:
networks:
my-app-network:
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ version: "3"
services:
express-server:
build: .
image: ${DOCKERHUB_NAME}/${IMAGE_NAME}/${IMAGE_VERSION}
env_file: .env
image: ${DOCKERHUB_NAME}/${IMAGE_NAME}:${IMAGE_VERSION}
1 change: 0 additions & 1 deletion server/db/bookshelf.js → server/database/bookshelf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const knex = require('./knex');
const bookshelf = require('bookshelf')(knex);
bookshelf.plugin('registry');

module.exports = bookshelf;
2 changes: 2 additions & 0 deletions server/database/knex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const config = require('../knexfile.js');
module.exports = require('knex')(config);
File renamed without changes.
13 changes: 13 additions & 0 deletions server/database/seeds/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.seed = function(knex, Promise) {
// Deletes ALL existing entries
return knex('users').del()
.then(function () {
// Inserts seed entries
return knex('users').insert([
{ username: 'ed' },
{ username: 'raymond' },
{ username: 'jason' }
]);
});
};

3 changes: 0 additions & 3 deletions server/db/knex.js

This file was deleted.

74 changes: 16 additions & 58 deletions server/knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,23 @@
require('dotenv').config({path: '../.env'})

module.exports = {

development: {
client: 'pg',
connection: {
host: 'localhost',
database: process.env.POSTGRES_DB,
port: process.env.POSTGRES_CONTAINER_PORT,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations',
directory: __dirname + '/db/migrations'
},
seeds: {
directory: __dirname + '/db/seeds'
}
client: 'pg',
connection: {
host: process.env.POSTGRES_HOSTNAME,
database: process.env.POSTGRES_DB,
port: process.env.POSTGRES_CONTAINER_PORT,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD
},

staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
pool: {
min: 2,
max: 10
},

production: {
client: 'pg',
connection: {
host: process.env.POSTGRES_HOSTNAME,
database: process.env.POSTGRES_DB,
port: process.env.POSTGRES_CONTAINER_PORT,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations',
directory: __dirname + '/db/migrations'
},
seeds: {
directory: __dirname + '/db/seeds'
}
migrations: {
tableName: 'knex_migrations',
directory: './database/migrations'
},
seeds: {
directory: './database/seeds'
}

};
14 changes: 10 additions & 4 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const User = require('./database/models/User');

// data vars
const PORT = process.env.EXPRESS_CONTAINER_PORT;
const User = require('./db/models/user');
const PORT = process.env.PORT;
const SESSION_SECRET = process.env.SESSION_SECRET;
const REDIS_HOSTNAME = process.env.REDIS_HOSTNAME;

if (!PORT) { console.log('No Port Found'); }
if (!SESSION_SECRET) { console.log('No Session Secret Found'); }
if (!REDIS_HOSTNAME) { console.log('No Redis Hostname Found'); }
if (!PORT || !SESSION_SECRET || !REDIS_HOSTNAME) { return process.exit(1); }

// setup server middleware
const app = express();
app.use(bodyParser.json({ extended: true }));
app.use(express.static('server/public'));

// routes
app.get('/api/smoke', (req, res) => {
Expand Down

0 comments on commit 2160061

Please sign in to comment.