Skip to content

Instantly share code, notes, and snippets.

@hdhruna
Created August 22, 2019 08:57
Show Gist options
  • Save hdhruna/857fa1646438a05bd758823604360509 to your computer and use it in GitHub Desktop.
Save hdhruna/857fa1646438a05bd758823604360509 to your computer and use it in GitHub Desktop.
NodeJS Multistage Dockerfile
FROM node:alpine as development-base
WORKDIR /usr/src/app
COPY . .
ARG DOMAIN="dev.example.com"
ENV DOMAIN=${DOMAIN}
RUN npm install
FROM node:alpine as production-base
WORKDIR /usr/src/app
COPY . .
ARG DOMAIN="example.com"
ENV DOMAIN=${DOMAIN}
RUN npm install --production
FROM keymetrics/pm2:latest-alpine as development
WORKDIR /usr/src/app/
COPY --from=development-base /usr/src/app/ .
EXPOSE 5004/tcp
ENV NPM_CONFIG_LOGLEVEL=info NODE_ENV=development
CMD [ "pm2-runtime", "start", "/usr/src/app/index.js" ]
FROM keymetrics/pm2:latest-alpine as production
WORKDIR /usr/src/app/
EXPOSE 5004/tcp
COPY --from=production-base /usr/src/app/ .
ENV NPM_CONFIG_LOGLEVEL=warn NODE_ENV=production
CMD [ "pm2-runtime", "start", "/usr/src/app/index.js" ]
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// parse requests of content-type - application/json
app.use(bodyParser.json());
// define a simple route
app.get("/api", (req, res) => {
res.json(["Tony", "Lisa", "Michael", "Ginger", "Food"]);
});
app.all("/healthcheck", (req, res) => {
res.status(200).json({
responseCode: 200,
responseDesc: "Success",
environment: process.env.NODE_ENV
});
});
app.listen("5004", function() {
console.log(`listening on port 5004`);
});
{
"name": "samplenode",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
@hdhruna
Copy link
Author

hdhruna commented Aug 24, 2019

Ensure 18.09 or later version of Docker is installed

To build for development:
DOCKER_BUILDKIT=1 docker build -t app-dev:latest --target development .

To build for production:
DOCKER_BUILDKIT=1 docker build -t app:latest --target production .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment