Categories
Nodejs

How to Dockerize a Node Web App

Spread the love

Running our Node web app in Docker saves us lots of headaches.

Every time a Docker image is deployed, it’s guaranteed to deploy a fresh container.

This way, we won’t have to worry about messing up our code in the container.

Also, the Docker image is built with an image, so we can build it repeatedly without doing anything manually.

In this article, we’ll look at how to Dockerize a simple Node web app.

Create the Node.js App

We start by creating our Node app.

To start, we create a project folder and run npm init --yes to create package.json .

Then we can replace everything in their with:

{
  "name": "my-app",
  "version": "1.0.0",
  "description": "a simple app",
  "author": "",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}

Then we create a server.js file in the same folder and add:

'use strict';

const express = require('express');

const PORT = 8080;
const HOST = '0.0.0.0';

const app = express();
app.get('/', (req, res) => {
  res.send('Hello World');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

It just has one route that responds with ‘hello world’.

Creating a Dockerfile

Next, we create a Dockerfile in the project folder.

Then we add:

FROM node:12
WORKDIR /usr/src/app
COPY package*.json ./

RUN npm install
COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]

We get the Node 12 image, then create a working directory to build the image.

Then we copy package.json and package-lock.json to the root directory with COPY package*.json ./

Next, we run npm install to install the packages.

And then we bundle the app’s source code with COPY . . .

Then we use EXPOSE 8080 to open the 8080 port for the Docker image.

Finally, we run node server.js to start the app with CMD [ “node”, “server.js” ] .

Next, we create .dockerignore file to stop Docker from copying the local modules to the Docker container.

We do the same with the NPM logs.

So we have the following in .dockerignore :

node_modules
npm-debug.log

Building the Image

We can then build the image with:

docker build -t <your username>/my-app .

Where <your username> is the username of your account.

Then we should see our image when we run docker images .

Run the Image

Once it’s built, we can run our image with:

docker run -p 8888:8080 -d <your username>/my-app

-d runs the container in detached mode, which leaves it running in the background.

-p redirects a public port to a private port in the container.

We can then run docker ps to get the container ID.

The app’s output can be obtained with:

docker logs <container id>

And we can go into the container with:

docker exec -it <container id> /bin/bash

Then to test our app, we can run:

curl -i localhost:8888

Then we should get the ‘hello world’ response from the app.

Conclusion

We can create a Docker image for a Node web app with a simple Dockerfile.

Then we don’t have to do much to get it running with Docker.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *