Skip to content

Instantly share code, notes, and snippets.

@puppybits
Last active June 12, 2017 17:37
Show Gist options
  • Save puppybits/b6a55d1fcb4adb8903e83b37f7e2c06f to your computer and use it in GitHub Desktop.
Save puppybits/b6a55d1fcb4adb8903e83b37f7e2c06f to your computer and use it in GitHub Desktop.
<10s Docker/NPM build & develop from a docker container
#!/bin/bash
# This is the "build" command in the package.json.
# This build command will keep build time < 10s (except when a npm dependency changes).
# Rational: Docker's cache check the file hash for each line
# when a file has changed in any way it will bust the cache.
# This is problematic for package.json files that change
# frequently even with the dependencies are the same.
# In order to correct this we'll copy just the package.json
# dependencies to docker, run `npm install` then override it
# with the project files. That will build times to < 10s.
node -e \" \
require('fs').writeFileSync( \
'.deps.json', \
JSON.stringify({ \
dependencies:require('./package.json').dependencies \
}))\" && \ # create file with only npm dependencies
docker build -t my/project . && \ # build the docker container
rm .deps.json # remove the dependencies-only file
FROM node
RUN mkdir -p /app
WORKDIR /app
# .deps.json will only bust the cache when the package.json dependencies change
COPY .deps.json /app/package.json
RUN npm install
# cache will almost always bust here, but it's only copying files
COPY package.json /app/package.json
COPY src /app/src
CMD npm start
#!/bin/bash
# This is the "watch" command in the package.json.
# Use this to develop from inside a docker container but using your local file system.
# It work especially great for project using old version of node.
docker stop myProject 2>/dev/null # stop running container
docker rm myProject 2>/dev/null # remove container logs
docker run -d \ # start new daemon
-e "DEBUG=express:*,my_project:*" \ # turn on NPM debugging
--name myProject \ # name the container
-p 3000:3000 \ # map internal to external port
-v /Users/puppybits/myProject:/app \ # override container w/ local dev files
my/project \ # container name
nodemon -L server/index.js # start nodemon to watch for changes
docker logs -f myProject # watch container logs
# single line command
docker stop myProject 2>/dev/null && docker rm myProject 2>/dev/null && docker run -d -e "DEBUG=express:*,my_project:*" --name myProject -p 3000:3000 -v /Users/puppybits/myProject:/app my/project nodemon server/index.js && docker logs -f myProject
{
"name": "myProject",
"version": "1.0.0",
"description": "docker / npm sample",
"license": "UNLICENSED",
"main": "server/index.js",
"scripts": {
"start": "PORT=8080 node server/index.js",
"build": "node -e \"require('fs').writeFileSync('.deps.json',JSON.stringify({dependencies:require('./package.json').dependencies}))\" && docker build -t my/project . && rm .deps.json",
"watch": "docker stop myProject 2>/dev/null && docker rm myProject 2>/dev/null && docker run -d -e \"DEBUG=express:*,my_project:*\" --name myProject -p 3000:3000 -v /Users/puppybits/myProject:/app my/project nodemon -L server/index.js && docker logs -f myProject",
"ssh": "docker exec -ti myProject /bin/bash",
"deploy": "gcloud preview app deploy --stop-previous-version"
},
"author": "Puppybits <puppybits@gmail.com> (https://twitter.com/puppybits)",
"dependencies": {
"body-parser": "*",
"express": "*",
"nodemon": "*"
},
"repository": {
"type": "git",
"url": "https://github.com/puppybits"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment