Skip to content

Commit dec232c

Browse files
committed
Updated to handle production enviroments
1 parent c0d5f43 commit dec232c

13 files changed

+118
-90
lines changed

.env.example

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
DOCKERHUB_NAME=your_docker_username
2-
IMAGE_NAME=image_name
3-
IMAGE_VERSION=0.0.0
1+
# Container version
2+
DOCKERHUB_NAME=name
3+
IMAGE_NAME=docker-deploy-demo
4+
IMAGE_VERSION=0.0.1
45

5-
EXPRESS_CONTAINER_PORT=8080
6+
# Container runtime
7+
PORT=8080
68
EXPRESS_HOST_PORT=8080
9+
EXPRESS_CONTAINER_PORT=8080
710

811
POSTGRES_HOST_PORT=5432
912
POSTGRES_CONTAINER_PORT=5432
1013
POSTGRES_HOSTNAME=postgres-primary-db
11-
POSTGRES_USER=postgres_user
14+
POSTGRES_USER=username
1215
POSTGRES_PASSWORD=password
13-
POSTGRES_DB=project_db
16+
POSTGRES_DB=database_name
1417

18+
REDIS_HOSTNAME=redis://redis-server:6379
1519
REDIS_HOST_PORT=6379
1620
REDIS_CONTAINER_PORT=6379
21+
SESSION_SECRET=secret

Dockerfile

+26-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
# Built from Node latest Alpine
2-
FROM node:10.0
1+
FROM node:10.15.1-alpine
32

4-
# Specify an optional argument with a default value
5-
ARG app_directory=/src/app
6-
ARG NODE_ENV=development
3+
# Update latest security patches
4+
RUN apk update && apk upgrade
75

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

11-
# ONLY copy over the package.json to install NPM packages
9+
# SET default APP_DIR path
10+
ARG APP_DIR=/src/app
11+
12+
# Create APP_DIR path and set permissions
13+
14+
RUN mkdir -p $APP_DIR
15+
RUN chown -R webuser:webuser $APP_DIR
16+
17+
# Switch user to non-privileged user
18+
USER webuser
19+
20+
# Change working directory to application directory
21+
WORKDIR $APP_DIR
22+
23+
# Copy package.json to /app directory
1224
COPY package.json .
1325

14-
# Install node module dependencies
26+
# Install node modules/dependencies
1527
RUN npm install
1628

17-
# Add the rest of the project files(most builds will start from here based on cache)
29+
# Copy application code
1830
COPY . .
1931

20-
# Start the node application as you normally would
32+
# Expose this port on DOCKER NETWORK (NOT HOST MAPPING)
33+
EXPOSE 8080
34+
35+
# Start the Express server
2136
CMD ["node", "./server/server.js"]

docker-compose.override.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ services:
77
command: ["npm", "run", "dev"]
88
container_name: ${IMAGE_NAME}
99
volumes:
10-
- ".:/src/app/:rw"
10+
- ".:/src/app"
1111
env_file: .env
12-
environment:
13-
ENVIRONMENT: production
14-
NODE_ENV: production
1512
ports:
1613
- "${EXPRESS_HOST_PORT}:${EXPRESS_CONTAINER_PORT}"
1714
networks:

docker-compose.prod.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
version: "3"
2+
services:
3+
express-server:
4+
image: ${DOCKERHUB_NAME}/${IMAGE_NAME}:${IMAGE_VERSION}
5+
depends_on:
6+
- postgres-primary-db
7+
container_name: ${IMAGE_NAME}
8+
env_file: .env
9+
environment:
10+
ENVIRONMENT: production
11+
NODE_ENV: production
12+
ports:
13+
- "${EXPRESS_HOST_PORT}:${EXPRESS_CONTAINER_PORT}"
14+
networks:
15+
- my-app-network
16+
postgres-primary-db:
17+
image: postgres:10.0-alpine
18+
env_file: .env
19+
volumes:
20+
- pg-data-volume:/var/lib/postgresql/data
21+
ports:
22+
- '${POSTGRES_HOST_PORT}:${POSTGRES_CONTAINER_PORT}'
23+
networks:
24+
- my-app-network
25+
redis-server:
26+
image: redis:4.0-alpine
27+
env_file: .env
28+
volumes:
29+
- redis-data-volume:/data
30+
ports:
31+
- '${REDIS_HOST_PORT}:${REDIS_CONTAINER_PORT}'
32+
networks:
33+
- my-app-network
34+
volumes:
35+
pg-data-volume:
36+
redis-data-volume:
37+
networks:
38+
my-app-network:

docker-compose.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ version: "3"
22
services:
33
express-server:
44
build: .
5-
image: ${DOCKERHUB_NAME}/${IMAGE_NAME}/${IMAGE_VERSION}
6-
env_file: .env
5+
image: ${DOCKERHUB_NAME}/${IMAGE_NAME}:${IMAGE_VERSION}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const knex = require('./knex');
22
const bookshelf = require('bookshelf')(knex);
33
bookshelf.plugin('registry');
4-
54
module.exports = bookshelf;

server/database/knex.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const config = require('../knexfile.js');
2+
module.exports = require('knex')(config);
File renamed without changes.

server/database/seeds/users.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
exports.seed = function(knex, Promise) {
2+
// Deletes ALL existing entries
3+
return knex('users').del()
4+
.then(function () {
5+
// Inserts seed entries
6+
return knex('users').insert([
7+
{ username: 'ed' },
8+
{ username: 'raymond' },
9+
{ username: 'jason' }
10+
]);
11+
});
12+
};
13+

server/db/knex.js

-3
This file was deleted.

server/knexfile.js

+16-59
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,23 @@
11
// Update with your config settings.
22
require('dotenv').config({path: '../.env'})
3-
43
module.exports = {
5-
6-
development: {
7-
client: 'pg',
8-
connection: {
9-
host: 'localhost',
10-
database: process.env.POSTGRES_DB,
11-
port: process.env.POSTGRES_CONTAINER_PORT,
12-
user: process.env.POSTGRES_USER,
13-
password: process.env.POSTGRES_PASSWORD
14-
},
15-
pool: {
16-
min: 2,
17-
max: 10
18-
},
19-
migrations: {
20-
tableName: 'knex_migrations',
21-
directory: __dirname + '/db/migrations'
22-
},
23-
seeds: {
24-
directory: __dirname + '/db/seeds'
25-
}
4+
client: 'pg',
5+
connection: {
6+
host: process.env.POSTGRES_HOSTNAME,
7+
database: process.env.POSTGRES_DB,
8+
port: process.env.POSTGRES_CONTAINER_PORT,
9+
user: process.env.POSTGRES_USER,
10+
password: process.env.POSTGRES_PASSWORD
2611
},
27-
28-
staging: {
29-
client: 'postgresql',
30-
connection: {
31-
database: 'my_db',
32-
user: 'username',
33-
password: 'password'
34-
},
35-
pool: {
36-
min: 2,
37-
max: 10
38-
},
39-
migrations: {
40-
tableName: 'knex_migrations'
41-
}
12+
pool: {
13+
min: 2,
14+
max: 10
4215
},
43-
44-
production: {
45-
client: 'pg',
46-
connection: {
47-
host: process.env.POSTGRES_HOSTNAME,
48-
database: process.env.POSTGRES_DB,
49-
port: process.env.POSTGRES_CONTAINER_PORT,
50-
user: process.env.POSTGRES_USER,
51-
password: process.env.POSTGRES_PASSWORD
52-
},
53-
pool: {
54-
min: 2,
55-
max: 10
56-
},
57-
migrations: {
58-
tableName: 'knex_migrations',
59-
directory: __dirname + '/db/migrations'
60-
},
61-
seeds: {
62-
directory: __dirname + '/db/seeds'
63-
}
16+
migrations: {
17+
tableName: 'knex_migrations',
18+
directory: './database/migrations'
19+
},
20+
seeds: {
21+
directory: './database/seeds'
6422
}
65-
6623
};

server/server.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
const express = require('express');
22
const bodyParser = require('body-parser');
3-
const app = express();
3+
const User = require('./database/models/User');
44

55
// data vars
6-
const PORT = process.env.EXPRESS_CONTAINER_PORT;
7-
const User = require('./db/models/user');
6+
const PORT = process.env.PORT;
7+
const SESSION_SECRET = process.env.SESSION_SECRET;
8+
const REDIS_HOSTNAME = process.env.REDIS_HOSTNAME;
9+
10+
if (!PORT) { console.log('No Port Found'); }
11+
if (!SESSION_SECRET) { console.log('No Session Secret Found'); }
12+
if (!REDIS_HOSTNAME) { console.log('No Redis Hostname Found'); }
13+
if (!PORT || !SESSION_SECRET || !REDIS_HOSTNAME) { return process.exit(1); }
814

915
// setup server middleware
16+
const app = express();
1017
app.use(bodyParser.json({ extended: true }));
11-
app.use(express.static('server/public'));
1218

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

0 commit comments

Comments
 (0)