There seemed to be a lot of example Docker containers for NodeJS/Express apps, but most of these seemed to be setup to run on macOS. I ran into a few issues when trying to get it all setup using a Windows host so thought I’d make some notes on it for future reference.
First off create an app.js file
'use strict';
const express = require('express');
const PORT = 3000;
const app = express();
app.get('/', function (req, res) {
res.send('Edit me!\n');
});
app.listen(PORT);
Then a package.json
{
"name": "docker-nodejs",
"version": "1.0.0",
"description": "A minimal NodeJS project config for Docker that restarts automatically on code changes",
"author": "Author",
"main": "app.js",
"scripts": {
"start": "nodemon -L app.js"
},
"dependencies": {
"express": "^4.13.3"
}
}
Add a docker-compose.yml that sets up the port forwarding and volume mount points
version: "2"
services:
test:
build: .
command: npm start
ports:
- "3000:3000"
volumes:
- .:/opt/app
and a Dockerfile which pulls in the node image, and install nodemon globally. Note that the npm install required –no-bin-links on Windows, otherwise the npm install will fail
FROM node:boron
RUN mkdir -p /opt/app
WORKDIR /opt/app
COPY . /opt/app
RUN npm install -g nodemon
RUN npm install --no-bin-links
EXPOSE 3000
CMD [ "npm", "start" ]
Finally add a .dockerignore which contains:
node_modules
npm-debug.log
Then to kick it all off:
docker-compose up --build
The complete project can be cloned from my Github:
Recent Comments