Skip to content

Instantly share code, notes, and snippets.

@kmassada
Last active January 9, 2018 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmassada/8a40cfbb8dc0f99000cd787e73b50c05 to your computer and use it in GitHub Desktop.
Save kmassada/8a40cfbb8dc0f99000cd787e73b50c05 to your computer and use it in GitHub Desktop.
Node.js container on gce with pid and exit on ctrl-c

~/lab2 $ ls

Dockerfile  index.js  package.json

~/lab2 $ uname -a

Linux instance-gke 4.4.70+ #1 SMP Fri Oct 6 05:36:11 PDT 2017 x86_64 Intel(R) Xeon(R) CPU @ 2.20GHz GenuineIntel GNU/Linux

~/lab2 $ cat /etc/release

CHROMEOS_AUSERVER=https://tools.google.com/service/update2
...
CHROMEOS_RELEASE_BRANCH_NUMBER=79
CHROMEOS_RELEASE_BUILDER_PATH=lakitu-release/R61-9765.79.0
CHROMEOS_RELEASE_BUILD_NUMBER=9765
CHROMEOS_RELEASE_BUILD_TYPE=Official Build
CHROMEOS_RELEASE_CHROME_MILESTONE=61
CHROMEOS_RELEASE_DESCRIPTION=9765.79.0 (Official Build) stable-channel lakitu 
CHROMEOS_RELEASE_NAME=Chrome OS
CHROMEOS_RELEASE_PATCH_NUMBER=0
CHROMEOS_RELEASE_TRACK=stable-channel
CHROMEOS_RELEASE_VERSION=9765.79.0
DEVICETYPE=OTHER
GOOGLE_RELEASE=9765.79.0
HWID_OVERRIDE=LAKITU DEFAULT
BUILD_ID=9765.79.0
NAME="Container-Optimized OS"
GOOGLE_CRASH_ID=Lakitu
VERSION_ID=61
BUG_REPORT_URL=https://crbug.com/new
PRETTY_NAME="Container-Optimized OS from Google"
VERSION=61
GOOGLE_METRICS_PRODUCT_ID=26
HOME_URL="https://cloud.google.com/compute/docs/containers/vm-image/"
ID=cos

~/lab2 $ docker build . -t makz-labs/14601556

Sending build context to Docker daemon 4.096 kB
Step 1/10 : FROM node:8
 ---> b87c2ad8344d
Step 2/10 : USER node
 ---> Using cache
 ---> 1f37b5f2835f
Step 3/10 : ENV HOME /home/node
 ---> Using cache
 ---> f23db773276a
Step 4/10 : RUN mkdir $HOME/app
 ---> Using cache
 ---> 0aaa97a8b2b3
Step 5/10 : WORKDIR $HOME/app
 ---> Using cache
 ---> c9273837c0bf
Step 6/10 : COPY package.json $HOME/app
 ---> Using cache
 ---> 916973d294c5
Step 7/10 : RUN npm set progress=false &&     npm config set depth 0 &&     npm install --only=production &&     np
m cache clean --force
 ---> Using cache
 ---> 0bf5558e6f1c
Step 8/10 : COPY . $HOME/app
 ---> Using cache
 ---> cbe1454f4924
Step 9/10 : ENV NODE_APP_INSTANCE osBTCM2
 ---> Using cache
 ---> ede6174cd075
Step 10/10 : ENTRYPOINT node index.js
 ---> Using cache
 ---> d40f47895b88
Successfully built d40f47895b88

~/lab2 $ docker run -it --rm makz-labs/14601556

This process is your pid 1
Running on http://0.0.0.0:8080
^CCaught interrupt signal
FROM node:8
USER node
ENV HOME=/home/node
RUN mkdir $HOME/app
WORKDIR $HOME/app
COPY package.json $HOME/app
RUN npm set progress=false && \
npm config set depth 0 && \
npm install --only=production && \
npm cache clean --force
COPY . $HOME/app
ENV NODE_APP_INSTANCE TEST_ENV
ENTRYPOINT ["node", "index.js"]
'use strict';
const express = require('express');
const process = require('process');
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello world\n');
});
// Exit on Ctrl-c
process.on('SIGINT', function() {
console.log("Caught interrupt signal");
if (true)
process.exit();
});
// Run app
app.listen(PORT, HOST);
// Catch Process
if (process.pid) {
console.log('This process is your pid ' + process.pid);
}
// Log it
console.log(`Running on http://${HOST}:${PORT}`);
{
"name": "web_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "GKE User",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.13.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment