Node.js is a JavaScript runtime built on Chrome's V8 engine, designed to execute JavaScript code on the server side. It is well-suited for building scalable and high-performance web applications due to its event-driven, non-blocking I/O model, making it lightweight and efficient for data-intensive real-time applications on distributed devices. Traditionally, web developers used JavaScript for the frontend and languages like Java, .NET, or PHP for the back end; Node.js allows frontend developers to use JavaScript across the entire stack.
Official Image
The official Node.js image is available at: https://hub.docker.com/_/node
Building a Simple Web Server
1. Coding
Creating a basic HTTP server in Node.js requires the built-in http module. Here is an example implementation:
const http = require('http');
const hostname = '0.0.0.0';
const port = 8080;
const requestHandler = (request, response) => {
response.statusCode = 200;
response.setHeader('Content-Type', 'text/plain');
response.end('Greetings from Node!');
};
const webServer = http.createServer(requestHandler);
webServer.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
2. Writing the Dockerfile
The following Dockerfile defines the environment for the application:
# Use a specific lightweight Node.js version
FROM node:10-alpine
# Define the working directory inside the container
WORKDIR /usr/src/app
# Copy package files if applicable, then app code
COPY package*.json ./
COPY server.js ./
# Install dependencies (if using package.json)
RUN npm install
# Expose the port the app runs on
EXPOSE 8080
# Start the application
CMD [ "node", "server.js" ]
3. Build and Run
Build the image using the following command:
docker build -t node-web-app .
Run the container:
docker run -p 3000:8080 node-web-app
For quick script execution without rebuilding images, you can mount a local directory and run the script directly using the Node image:
docker run -it --rm \
-v /local/path/to/script:/app \
-w /app \
node:10-alpine \
node script.js
Setting Up a Team Documentation Site with Hexo
Hexo is a fast, static site generator that parses Markdown files and renders them into static web pages using various themes. It is ideal for blogs, documentation sites, and official websites. Key advantages include:
- Rich Theme Ecosystem: Numerous open-source themes are available.
- Extensive Plugins: Supports search, word count, compression, and more.
- Flexible & Extensible: Easy to modify themes and plugins with basic JavaScript and HTML knowledge.
- Markdown Support: Team members can write content in Markdown, which is automatically converted to a styled site.
1. Installation
Ensure the folllowing prerequisites are installed:
- Node.js (version 6.9 or higher)
- Git
Install the Hexo command-line interface global:
npm install -g hexo-cli
2. Initialization
Create a new Hexo project:
hexo init my-documentation-site
cd my-documentation-site
npm install
3. Configuring Site Information
Edit the _config.yml file in the root directory to set site title, author, language, and other parameters. Refer to the official configuration guide.
4. Generating Static Files
Hexo initializes with a default theme and a sample post. Generate the static site:
hexo generate
The output will be located in the public folder. You can also deploy the site to platforms like GitHub Pages using hexo deploy.
5. Local Hosting with hexo-server
Install the local server component:
npm install hexo-server --save
Start the server on a specific port:
hexo server -p 5000
Access the site at http://localhost:5000.
6. Containerized Build and Hosting
To containerize the Hexo site, use the following Dockerfile:
FROM node:10.15.3-alpine
# Set up environment
EXPOSE 8000
# Set working directory
WORKDIR /doc-site
# Copy project files
COPY . .
# Install Hexo CLI and project dependencies
RUN npm config set unsafe-perm true && \
npm config set registry https://registry.npm.taobao.org && \
npm install -g hexo-cli && \
cd src && \
npm install && \
npm install hexo-neat --save && \
npm install hexo-wordcount --save && \
npm install hexo-prism-plugin --save && \
npm install hexo-generator-search --save && \
npm install hexo-permalink-pinyin --save && \
hexo generate && \
npm install hexo-server --save
# Switch to source directory
WORKDIR /doc-site/src
# Run the server
ENTRYPOINT ["hexo", "server", "-p", "8000"]
This Dockerfile installs necessary tools, generates the static site, and serves it using the Hexo internal server within the container.