Skip to content

Instantly share code, notes, and snippets.

@rafaismyname
Created November 12, 2018 15:19
Show Gist options
  • Save rafaismyname/be460b3f65085a162638b04cf3a2609f to your computer and use it in GitHub Desktop.
Save rafaismyname/be460b3f65085a162638b04cf3a2609f to your computer and use it in GitHub Desktop.
Basic node dev env with docker-compose
NODE_ENV=development
HOST=0.0.0.0
PORT=1880
#LOCATED AT: ./api/Dockerfile
version: '3.1'
services:
db:
image: mongo:3.6
ports:
- 27017:27017
redis:
image: redis:4.0-alpine
ports:
- 6379:6379
rabbitmq:
image: rabbitmq:3.6-management
environment:
- RABBITMQ_DEFAULT_USER=user
- RABBITMQ_DEFAULT_PASS=password
ports:
- 5672:5672
- 15672:15672
api:
build:
context: .
args:
- NODE_ENV=development
- PORT=1880
command: ../node_modules/.bin/nodemon --inspect=0.0.0.0:9229
env_file:
- ./.env
ports:
- 1880:1880
- 9229:9229
volumes:
- .:/opt/app
links:
- db
- redis
- rabbitmq
FROM node:8
RUN mkdir -p /opt/app
# set our node environment, either development or production
# defaults to production, compose overrides this to development on build and run
ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV
# default to port 80 for node
ARG PORT=80
ENV PORT $PORT
EXPOSE $PORT
# install dependencies first, in a different location for easier app bind mounting for local development
WORKDIR /opt
COPY package.json yarn.lock* ./
RUN yarn install && \
yarn global add pm2 && \
yarn cache clean --force
ENV PATH /opt/node_modules/.bin:$PATH
# copy in our source code last, as it changes the most
WORKDIR /opt/app
COPY . /opt/app
CMD ["pm2-docker", "start", "--json", "pm2.json"]
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const bluebird = require('bluebird');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(bodyParser.json({ limit: '1mb' }));
app.use(bodyParser.urlencoded({ limit: '1mb', extended: true }));
const mongoParams = { useMongoClient: true, promiseLibrary: bluebird };
mongoose.Promise = bluebird;
mongoose.connect(process.env.MONGO_URL, mongoParams);
const server = http.createServer(app);
app.get('/', function (request, reply){
return reply.json({ success: true });
});
server.listen(process.env.PORT, process.env.HOST, () => {
console.log(`API running at: ${process.env.HOST}:${process.env.PORT}`);
});
{
"name": "api",
"version": "1.5.5",
"main": "index.js",
"license": "UNLICENSED",
"private": true,
"engines": {
"node": "8.11"
},
"dependencies": {
"axios": "^0.18.0",
"bcrypt": "^2.0.1",
"bluebird": "^3.5.1",
"body-parser": "^1.17.2",
"bull-arena": "^2.4.3",
"bunnymq": "^2.3.2",
"cors": "^2.7.1",
"express": "^4.15.4",
"jsonwebtoken": "^8.1.0",
"lodash": "^4.11.1",
"moment": "^2.15.1",
"moment-timezone": "^0.5.14",
"mongoose": "^4.4.12",
"redis": "^2.8.0",
"shortid": "^2.2.8",
},
"devDependencies": {
"nodemon": "^1.17.4"
}
}
{
"apps": [{
"name": "api",
"script": "./index.js",
"node_args": "--max-old-space-size=512",
"env": {
"production": true
}
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment