Follow the basic setup as in the Docker website. For me I had to download the Docker toolchain from here 🔗.
After that you should have docker installed and the Docker terminal ready to be opened from your desktop.
I followed the instructions from here https://nodejs.org/en/docs/guides/nodejs-docker-webapp/ to create my first Docker container with Node.js.
Here's the short and sweet if you want just to copy and paste to see if it works:
NOTE! On Windows or MacOS machines you cannot go straight to the localhost:49160 to see the app working. As explained here moby/moby#15740 they cannot run the Docker container natively so the port is actually the one on the VirtualBox so for me worked just write docker-machine ip default
and whatever the ip from that is your container's ip. (my app was on http://192.168.99.100:49160/)
// app.js
const express = require('express');
const app = express();
const port = 8080;
app.get('', (req, res) => {
res.send('Hello world! :D');
})
app.listen(port, err => {
if (err) console.log(err)
else console.log('App is listening on port ' + port);
})
// package.json
{
"name": "docker-test",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
// Dockerfile
FROM node:boron
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 8080
CMD [ "npm", "start" ]
Then after you have created those files to a folder inside there run these with your Docker terminal:
docker build -t YOUR_USER_NAME/node-web-app .
docker run -p 49160:8080 -d YOUR_USER_NAME/node-web-app
And that should be it! Look and behold your app should be running (probably on http://192.168.99.100:49160/)