avatarBhargav Bachina

Summary

This article provides instructions on how to containerize a Node.js REST API using Docker, with a focus on a TypeScript version.

Abstract

The article begins by referencing a previous article on creating a production-ready Node.js REST API with TypeScript. It then proceeds to explain how to containerize this API using Docker, providing two methods: one without using webpack and one with webpack. The method without webpack involves creating a Dockerfile with the base image node:8.11-slim, copying the package.json file and installing dependencies, and then copying the remaining files. The method with webpack involves copying the bundle file into the Docker filesystem and running it with Node, eliminating the need to copy node_modules and run npm install. The article also provides instructions on how to build and run the Docker image, as well as how to interact with the container.

Bullet points

  • The article is a continuation of a previous one on creating a production-ready Node.js REST API with TypeScript.
  • There are two methods to containerize the API using Docker: one without using webpack and one with webpack.
  • The method without webpack involves creating a Dockerfile with the base image node:8.11-slim, copying the package.json file and installing dependencies, and then copying the remaining files.
  • The method with webpack involves copying the bundle file into the Docker filesystem and running it with Node, eliminating the need to copy node_modules and run npm install.
  • The article provides instructions on how to build and run the Docker image, as well as how to interact with the container.
  • The article includes images and code snippets to illustrate the process.

Containerize your Node.js Rest API and run it on Docker — Typescript version

Photo by Daniel von Appen on Unsplash

I recently published an article regarding production ready nodejs rest API with typescript and this article is the extension for that. Below is the link if you want to have a look.

clone the below repo for this article.

git clone https://github.com/bbachi/prod-ready-node-rest-api-typescript.git

There are two ways to run this API on docker i.e.without webpack and with webpack. when we use webpack you don’t have to install node_modules since it’s part of the bundle.

Without webpack

The following is the dockerfile which runs with the base image node:8.11-slim. we are copying package.json first and install all the dependencies before we copy other files. This is because we don’t want to install node_modules every time we change anything and build the image. Docker uses cache to take the layers as long as you don’t change commands.

Build the docker image from Dockerfile with this command and tag it with the name node-rest-API. you can list the images with command docker images.

docker build -t node-api-typescript .
docker run -it -p 3080:3070 --name=nodeapitypesctipt node-api-typescript

Interact with container

if you want to interact with your container, run the exec command where you can explore the container file system. you can edit files and execute npm commands as well.

docker exec -it nodeapitypesctipt bash

With webpack

as I said earlier, we don’t have to copy node_modules and run npm install to install all the dependencies. all you need to copy the bundle file into the docker filesystem and run with node.

Run the following commands if you want to build the image and run the container.

docker build -f Dockerfile.webpack -t node-rest-api-webpack .
docker run -it -p 3090:3070 --name=nodeapiwithwebpack node-rest-api-webpack

That’s it. Thank you for reading!!

Docker
Nodejs
Dockerfiles
Web Development
Containers
Recommended from ReadMedium