Hello World with Express and Docker

Inspired by the good introduction how to Dockerizing a Node.js web app I gave it a try myself. The ingredients are:

Node.js®

a JavaScript runtime built on Chrome's V8 JavaScript engine.

Express

Fast, unopinionated, minimalist web framework for Node.js

and Docker

BUILD, SHIP, RUN

I started with the cute Hello world example available from the Express homepage. Let's create the file app.js:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

We want to do it the Docker way so let's create a Dockerfile to test our app locally:

# latest LTS (long term support) version from DockerHub
FROM node:argon

# 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" ]

A minimalistic package.json completes the example.

{
  "name": "hello-world",
  "version": "1.0.0",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "4.15.3"
  }
}

Now we are ready to build and run run the example locally with Docker:

$ docker build . -t examples/nodejs-docker-webapp
$ docker run -it --rm -p 8080:8080 examples/nodejs-docker-webapp
npm info it worked if it ends with ok
....
npm info start docker_web_app@1.0.0

> docker_web_app@1.0.0 start /usr/src/app
> node server.js

Running on http://localhost:8080

:tada: The app is up and running at http://localhost:8080 without the need to install node on your local machine.

Go ahead: Express yourself! :wink:

PS: The example code is available on GitHub nodejs-docker-webapp, too.