How To Build a Node.js Application with Docker
How To Build a Node.js Application with Docker
Introduction
Docker is a platform for developing, shipping, and running applications using container technology. In this tutorial, we will learn how to build a Node.js application with Docker.
Prerequisites
- Basic knowledge of Node.js and Docker
- Node.js and Docker installed on your system
Steps
- Create a Node.js application
- Create a Dockerfile
- Build the Docker image
- Run the Docker container
Step 1: Create a Node.js Application
First, we need to create a Node.js application. Let's create a simple "Hello, World!" application. Create a new folder and navigate to it in your terminal. Then, run the following command:
mkdir node-docker-app
cd node-docker-app
npm init -y
touch index.js
Open the index.js file in your favorite text editor and add the following code:
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Save the file and exit your text editor.
Step 2: Create a Dockerfile
Next, we need to create a Dockerfile. This file will contain the instructions for building the Docker image. Create a new file called Dockerfile
Комментарии
Отправить комментарий