Node. js web applications are encapsulated in Docker containers

  • 2021-08-09 07:00:31
  • OfStack

Little began to learn new content again. This time, we learned to encapsulate an Node. js application into an Docker container. The premise of completing this tutorial is to have an Docker that can be installed and works normally. As well as a general understanding of how Node. js applications work.

In Part 1 of this tutorial, you need to create an Web application, then build an Docker image for the application, and finally run the image as a container.

Docker allows applications to package dependencies to complete a standardized unit, which is a container. For applications, Docker is called a standard Linux operating system, and a mirror is the software loaded into the container.

Creating an Node. js application

First, you need to create an package. json file and the dependencies it contains.


{
 "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"
 }
}

Then enter npm install Dependencies related to installation.

Then create an server. js file and an web application.


'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}`);

This completes a standard and simplest web application.

Next, you will need to create 1 image to encapsulate the image.

Create 1 Dockerfile file


touch Dockerfile

Open the file and enter the related basic image


FROM node:12

Create related working directories


# Create app directory
WORKDIR /usr/src/app

Copy related package management files and install related dependencies


# 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

Copy the relevant program code


# Bundle app source
COPY . .

Bind the associated port number


EXPOSE 8080

Create a persistent command to let the system run in the foreground.


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

Finally, Dockerfile is constructed as follows


FROM node:12

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

About the dockerignore file

This file is to prevent copying to related files, for example, node_modules does not need to be copied inside the docker image


'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}`);
0

Build an docker image


'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}`);
1

Enter the command above to build the docker image.

The final docker image is as follows


'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}`);
2

Run related images

At this point, the image has been built, and it needs to be run here.


docker run -p 49160:8080 -d <your username>/node-web-app

To enter the container, enter the following command


'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}`);
4

Test

Enter ps to see the details of the image of docker


'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}`);
5

Use curl to access Web sites


'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}`);
6

These are the details of how Node. js web applications are encapsulated in Docker containers. For more information about Node. js applications encapsulated in Docker containers, please pay attention to other related articles on this site!


Related articles: