From 0d0ca4deda9fb510e0c878c7ec5dd8f0fb412739 Mon Sep 17 00:00:00 2001
From: Pagan Gazzard <pjgazzard@googlemail.com>
Date: Tue, 27 Jan 2015 13:04:34 +0000
Subject: [PATCH] Switch to using resin/rpi-node and node images, making use of
 package.json caching.

---
 Dockerfile.i386   | 42 +++++++++++++++++++++++++++++++++---------
 Dockerfile.rpi    | 40 ++++++++++++++++++++++++++++++++++++----
 Dockerfile.x86_64 | 37 +++++++++++++++++++++++++++----------
 Makefile          |  2 +-
 deps.sh           | 10 ----------
 package.json      |  1 -
 postinstall.sh    | 21 +--------------------
 7 files changed, 98 insertions(+), 55 deletions(-)
 delete mode 100644 deps.sh

diff --git a/Dockerfile.i386 b/Dockerfile.i386
index cedcdc60..f91cc52f 100644
--- a/Dockerfile.i386
+++ b/Dockerfile.i386
@@ -1,20 +1,44 @@
 FROM resin/i386-debian:jessie
 
-# Install.
-RUN apt-get update && apt-get install -y curl git unzip wget npm nodejs inetutils-ping libsqlite3-dev socat supervisor && apt-get clean && rm -rf /var/lib/apt/lists/
-RUN ln -sf /usr/bin/nodejs  /usr/bin/node
+# Nodejs
+RUN apt-get update \
+	&& apt-get install -y curl git unzip wget npm nodejs inetutils-ping \
+	&& apt-get clean \
+	&& rm -rf /var/lib/apt/lists/
+RUN ln -sf /usr/bin/nodejs /usr/bin/node
 
-ADD . /app
+# Supervisor apt dependencies
+RUN apt-get -q update \
+	&& apt-get install -qqy socat supervisor \
+	&& apt-get clean \
+	&& rm -rf /var/lib/apt/lists/
 
+# Compile time dependencies
+RUN apt-get -q update \
+	&& apt-get install -qqy g++ git libsqlite3-dev make ssh-client \
+	&& apt-get clean \
+	&& rm -rf /var/lib/apt/lists/
+
+# Deploy key for private npm modules
+RUN mkdir -p /root/.ssh
+COPY deploy_key /root/.ssh/id_rsa
+COPY ssh_config /root/.ssh/config
+RUN chmod 400 /root/.ssh/*
+
+# Copy supervisord launch file for resin-supervisor
+COPY resin-supervisor.conf /etc/supervisor/conf.d/resin-supervisor.conf
+
+# Install dependencies
 WORKDIR /app
+COPY package.json postinstall.sh /app/
+RUN npm install --unsafe-perm --production \
+	&& npm cache clean
+
+# Copy source
+COPY . /app/
 
 ENV SUPERVISOR_IMAGE resin/i386-supervisor
 ENV CONFIG_MOUNT_POINT /boot/config.json
 ENV LED_FILE /dev/null
 
-RUN rm -rf node_modules
-RUN npm install --unsafe-perm --production
-
-RUN ln -sf /app/entry.sh /start
-
 CMD ["/app/entry.sh"]
diff --git a/Dockerfile.rpi b/Dockerfile.rpi
index fba3fae9..ad92a8f3 100644
--- a/Dockerfile.rpi
+++ b/Dockerfile.rpi
@@ -1,4 +1,36 @@
-FROM resin/supervisor-base:latest
-ADD . /app
-RUN /build/builder
-RUN apt-get clean
+FROM resin/rpi-node:0.10.36-slim
+
+# Supervisor apt dependencies
+RUN apt-get -q update \
+	&& apt-get install -qqy socat supervisor \
+	&& apt-get clean \
+	&& rm -rf /var/lib/apt/lists/
+
+# Compile time dependencies
+RUN apt-get -q update \
+	&& apt-get install -qqy g++ git libsqlite3-dev make ssh-client \
+	&& apt-get clean \
+	&& rm -rf /var/lib/apt/lists/
+
+# Deploy key for private npm modules
+RUN mkdir -p /root/.ssh
+COPY deploy_key /root/.ssh/id_rsa
+COPY ssh_config /root/.ssh/config
+RUN chmod 400 /root/.ssh/*
+
+# Copy supervisord launch file for resin-supervisor
+COPY resin-supervisor.conf /etc/supervisor/conf.d/resin-supervisor.conf
+
+# Install dependencies
+WORKDIR /app
+COPY package.json postinstall.sh /app/
+RUN npm install --unsafe-perm --production \
+	&& npm cache clean
+
+# Copy source
+COPY . /app/
+
+RUN chmod +x /app/src/enterContainer.sh
+RUN /app/node_modules/.bin/coffee -c /app/src
+
+CMD ["/app/entry.sh"]
diff --git a/Dockerfile.x86_64 b/Dockerfile.x86_64
index 186b5eb2..d3770977 100644
--- a/Dockerfile.x86_64
+++ b/Dockerfile.x86_64
@@ -1,20 +1,37 @@
-FROM dockerfile/nodejs
+FROM node:0.10.36-slim
 
-RUN apt-get -q update && apt-get install -qqy libsqlite3-dev socat supervisor
+# Supervisor apt dependencies
+RUN apt-get -q update \
+	&& apt-get install -qqy socat supervisor \
+	&& apt-get clean \
+	&& rm -rf /var/lib/apt/lists/
 
-ADD . /app
+# Compile time dependencies
+RUN apt-get -q update \
+	&& apt-get install -qqy g++ git libsqlite3-dev make ssh-client \
+	&& apt-get clean \
+	&& rm -rf /var/lib/apt/lists/
 
+# Deploy key for private npm modules
+RUN mkdir -p /root/.ssh
+COPY deploy_key /root/.ssh/id_rsa
+COPY ssh_config /root/.ssh/config
+RUN chmod 400 /root/.ssh/*
+
+# Copy supervisord launch file for resin-supervisor
+COPY resin-supervisor.conf /etc/supervisor/conf.d/resin-supervisor.conf
+
+# Install dependencies
 WORKDIR /app
+COPY package.json postinstall.sh /app/
+RUN npm install --unsafe-perm --production \
+	&& npm cache clean
+
+# Copy source
+COPY . /app/
 
 ENV SUPERVISOR_IMAGE resin/x86_64-supervisor
 ENV CONFIG_MOUNT_POINT /boot/config.json
 ENV LED_FILE /dev/null
 
-RUN rm -rf node_modules
-RUN npm install --unsafe-perm --production
-
-RUN ln -sf /app/entry.sh /start
-
-RUN apt-get clean
-
 CMD ["/app/entry.sh"]
diff --git a/Makefile b/Makefile
index a2eb8e14..04ea76d3 100644
--- a/Makefile
+++ b/Makefile
@@ -45,7 +45,7 @@ endif
 	docker tag -f resin/supervisor-base:$(BUILDSTEP_VERSION) resin/supervisor-base:latest
 endif
 
-supervisor: supervisor-base
+supervisor:
 	cp Dockerfile.$(ARCH) Dockerfile
 	echo "ENV VERSION "`jq -r .version package.json` >> Dockerfile
 	docker build --no-cache=$(DISABLE_CACHE) -t resin/$(ARCH)-supervisor:$(SUPERVISOR_VERSION) .
diff --git a/deps.sh b/deps.sh
deleted file mode 100644
index 64e36f55..00000000
--- a/deps.sh
+++ /dev/null
@@ -1,10 +0,0 @@
-set -o errexit
-set -o pipefail
-
-if [ $NODE_ENV == 'production' ]; then
-	# Deploy key for private npm modules
-	mkdir -p /root/.ssh
-	cp deploy_key /root/.ssh/id_rsa
-	chmod 400 /root/.ssh/id_rsa
-	cp ssh_config /root/.ssh/config
-fi
diff --git a/package.json b/package.json
index 51650fb1..c68eca49 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,6 @@
   "name": "resin-supervisor",
   "version": "0.0.5",
   "scripts": {
-    "preinstall": "bash deps.sh",
     "postinstall": "bash postinstall.sh",
     "start": "./entry.sh"
   },
diff --git a/postinstall.sh b/postinstall.sh
index 9574b64b..77f3dabe 100755
--- a/postinstall.sh
+++ b/postinstall.sh
@@ -1,24 +1,7 @@
 set -o errexit
 set -o pipefail
 
-# Copy supervisord launch file for resin-supervisor
-cp /app/resin-supervisor.conf /etc/supervisor/conf.d/resin-supervisor.conf
-
-if [ $NODE_ENV == 'production' ]; then
-	chmod +x src/enterContainer.sh
-
-	node ./node_modules/coffee-script/bin/coffee -c ./src
-	# We don't need coffee-script at runtime
-	npm uninstall coffee-script
-	# Empty the apt and npm caches of the packages we installed
-	npm cache clean
-	apt-get clean
-	# Remove deploy key
-	rm -rf /root/.ssh/* deploy_key
-	# Remove unnecessary source files
-	rm -rf Makefile src/*.coffee
-	# Remove the git repo info
-	rm -rf .git
+if [[ $NODE_ENV == 'production' ]]; then
 	# Remove the c files we no longer need (the sqlite3 node module has a massive ~5M c file)
 	find . -name '*.c' -delete
 	# And the tar files (sqlite3 module again)
@@ -30,6 +13,4 @@ if [ $NODE_ENV == 'production' ]; then
 	# And any benchmark results (ttyjs->socket.io->socket.io-client->active-x-obfuscator->zeparser has an 8MB benchmark.html)
 	find . -type d -name 'benchmark*' -exec rm -rf '{}' \; 2> /dev/null || true
 	find . -name 'benchmark*' -delete
-	# And npm - we've finished with it
-	find . -type d -name 'npm' -exec rm -rf '{}' \; 2> /dev/null || true
 fi