The goal of this blog is to show you how to get a Node.js application into a Docker container. This blog is intended for development, and not for a production deployment. This blog also assumes you have a working Docker Installation and a basic understanding of how a Node.js application is structured. If you want to learn in more detail about it, you can refer here.
So let’s start to learn how we can dockerizing a Node.js web app.
In the first part of this blog we will create a simple web application in Node.js, then we will build a Docker image for that application, and lastly we will instantiate a container from that image.
Introduction:
Docker allows you to package an application with its environment and all of its dependencies into a “box”, called a container. Usually, a container consists of an application running in a stripped-to-basics version of a Linux operating system. An image is the blueprint for a container, a container is a running instance of an image.
Let’s start to create a Node.js App:
First, create a new directory where all the files would live. In this directory create a package.json
file that describes your app and its dependencies:
{
"name": "docker_web_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "First Last <first.last@example.com>",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1"
}
}
With your new package.json
file, run npm install
. If you are using npm
version 5 or later, this will generate a package-lock.json
file which will be copied to your Docker image.
Then, create a server.js
file that defines a web app using the Express.js framework:
'use strict';
const express = require('express');
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
In the next steps, we’ll look at how you can run this app inside a container using the official Docker image. First, you’ll need to build a Docker image of your app.
Creating a Dockerfile
Create an empty file called Dockerfile
:
touch Dockerfile
Your Dockerfile should now look like this:
FROM node:14
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
Building your image
Go to the directory that has your Dockerfile
and run the following command to build the Docker image. The -t
flag lets you tag your image so it’s easier to find later using the image command:
docker build . -t <your username>/node-web-app
Your image will now be listed by Docker:
$ docker images
# Example
REPOSITORY TAG ID CREATED
node 14 1934b0b038d1 5 days ago
<your username>/node-web-app latest d64d3505b0d2 1 minute ago
Run the image:
Running your image with -d
runs the container in detached mode, leaving the container running in the background. The -p
flag redirects a public port to a private port inside the container. Run the image you previously built:
docker run -p 49160:8080 -d <your username>/node-web-app
Print the output of your app:
# Get container ID
$ docker ps
# Print app output
$ docker logs <container id>
# Example
Running on http://localhost:8080
Test
To test your app, get the port of your app that Docker mapped:
$ docker ps
# Example
ID IMAGE COMMAND ... PORTS
ecce33b30ebf <your username>/node-web-app:latest npm start ... 49160->8080
In the example above, Docker mapped the 8080
port inside of the container to the port 49160
on your machine.
Now you can call your app using curl
(install if needed via: sudo apt-get install curl
):
$ curl -i localhost:49160
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 12
ETag: W/"c-M6tWOb/Y57lesdjQuHeB1P/qTV0"
Date: Mon, 13 Nov 2017 20:53:59 GMT
Connection: keep-alive
Hello world
Conclusion:
In this blog, we’ve learnt how to get a Node.js application into a Docker container. We’ve seen that how we can create the docker image. It allows us to package an application with its environment and all of its dependencies into a container.
Hey there, I am glad you have reached the end of this post. If you liked this post or have some questions or want to discuss something let me know in the comment section.
For more info you can check:
https://nodejs.org/en/docs/guides/
