Skip to content

Instantly share code, notes, and snippets.

@jmyrland
Created May 15, 2015 13:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmyrland/eaeef7b794c41ec1af9d to your computer and use it in GitHub Desktop.
Save jmyrland/eaeef7b794c41ec1af9d to your computer and use it in GitHub Desktop.
Docker mounted volume removes node_modules
# Set the base image to stable version of node
FROM node:0.12.2
# Install nodemon
RUN npm install -g nodemon
# Provides cached layer for node_modules
COPY package.json /tmp/package.json
RUN cd /tmp && npm install --production
RUN mkdir -p /app && cp -a /tmp/node_modules /app/
RUN mkdir /app/.tmp
# Define working directory
WORKDIR /app
COPY . /app
ENV DOCKER=true
# Expose port
EXPOSE 3002
# Run app using nodemon
CMD ["nodemon", "/app/index.js"]
/**
* This file is used for creating a symlink between the actual ./node_modules folder
* and the ./src/node_modules "folder".
*
* This is due to being able to both run the app smoothly in a local and containerized environment.
*/
var fs = require('fs');
// Paths
var NODE_MODULES = '../node_modules'
var SRC_NODE_MODULES = './src/node_modules';
// Check if the symlink is already existing..
fs.exists(SRC_NODE_MODULES, function(exists) {
// ?: Is it NOT existing?
if (!exists) {
// -> Nope, then create it and launch the server
createSymLink(launchServer);
return;
}
// E -> The symlink exists!
console.log('! SYMLINK exists, recreate it..')
// Remove the existing link (why? Because we do not know if it was created locally or in a container)
fs.unlink(SRC_NODE_MODULES, function(err) {
if (err) return console.log(err);
// Double check that the link was removed
fs.exists(SRC_NODE_MODULES, function(exists) {
console.log('! SYMLINK was ' + (exists ? 'NOT DELETED!' : 'deleted successfully..'));
// Was it removed?
if (exists) {
// -> NO! Then abort mission!
return console.log('! Aborting server launch..')
}
// E -> It was removed successfully. Re-create it and launch the server
createSymLink(launchServer);
});
});
});
var createSymLink = function(callback) {
fs.symlink(NODE_MODULES, SRC_NODE_MODULES, 'dir', function(err) {
if (err) return console.log(err);
console.log('! SYMLINK created!')
callback();
});
}
var launchServer = function() {
console.log('! Launching server..')
require('./src/app.js');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment