Let's get started. Install Docker. I use Docker Toolbox 1.12 on Windows 10, so commands may need to be adjusted a bit based on your circumstance. When I run docker --version I get this
Docker version 17.10.0-ce, build f4ffd25
Let's create our directory for a project C:\Users\evgeniy.sharapov\Repos\7in14\node. Below we are working in command line, CMD is ok, but ConEmu is even better.
Since we are using Toolbox we would have to essentially convert our Windows path to something that is located on Boot2Docker, in this case it's going to be /c/Users/evgeniy.sharapov/Repos/7in14/nodejs. To save some command line space you could create an environment variable.
set PROJ=/c/Users/evgeniy.sharapov/Repos/7in14/nodejs
For NodeJS development - it's a good idea to choose LTS Node Alpine based image. For instance, node:8-alpine. Let's get start by running npm init:
docker run -it -v %PROJ%:/app -w /app --rm node:8-alpine npm init
Answer the questons and have a look at the package.json file. Note that we have --rm among other options. This will remove container after it ran our command, in this case npm init.
{
"name": "7in14node",
"version": "1.0.0",
"description": "7 in 14 Challenge Node JS",
"main": "index.js",
"directories": {
"test": "test"
},
"dependencies": {
"babel-cli": "^6.26.0"
},
"devDependencies": {},
"scripts": {
"test": "npm test"
},
"keywords": [
"node"
],
"author": "Evgeniy Sharapov",
"license": "ISC"
}
Note, that you can easily create a batch file n.bat with following content.
docker run -it -v %PROJ%:/app -w /app --rm node:8-alpine %*
After that you can run all the command using n.bat.
Let's add some libraries to write our application using ECMAScript2017:
n npm install --no-bin-links --save-dev babel-cli babel-core babel-loader babel-preset-es2015 babel-preset-stage-2
You have to use --no-bin-link to avoid issues of filesystem bridge between Boot2Docker VM and Windows Host file system.
docker run -it -v %PROJ%:/app -w /app --rm node:8-alpine npm install --save-dev
Let's create index.js file
console.log("Hello, world!");
Now you can run it as
n node index.js
Note that we have not installed anything and hence we avoid any version conflicts, later we can easily switch to a new version of Node without any install/uninstall shenanigans.