...

/

Containerize a Node.js Application

Containerize a Node.js Application

Learn how to package a Node.js (TypeScript) back-end application in a Docker container.

This tutorial assumes you have some experience building Node.js applications, so it will concentrate on how to Dockerize a TypeScript-based Node.js application.

We start by building a simple web application with the express framework, then we create a Docker image for it, and finally, we launch a container from that image.

There are three essential files for this tutorial:

  • main.ts
  • package.json
  • Dockerfile

The main.ts file contains the Node.js application’s code. It’s a simple application that, when accessed, prints hello-world to our browser:

Press + to interact
//file: src/main.ts
import * as express from 'express';
const app = express();
const PORT = process.env.PORT? process.env.PORT: 3000;
// @ts-ignore
app.get('/', (req: express.Request, res: express.Response) => res.send('Hello World'));
app.listen(PORT, () => {
console.log(`⚡️[server]: Server is running at https://localhost:${PORT}`);
});

We start a new application in the code above and create a route that returns Hello World as the response. ...