Using Docker for a Node.js API Project

Date: 2023-05-28
Author: Justin


Introduction

Containerization has become a cornerstone of efficient and scalable software development. Docker, a popular containerization platform, helps developers to create, deploy, and run applications with ease and consistency. This post will guide you through the process of using Docker in a Node.js API project, highlighting the core concepts and steps involved in the process.

Understanding Docker

Docker is a platform that uses OS-level virtualization to package software into containers. These containers are standalone packages that include everything needed to run an application, including the code, runtime, libraries, and system tools. This ensures that the application runs the same way regardless of the environment.

Setting Up a Node.js API Project

Before diving into Docker, let's set up a simple Node.js API. We'll use Express.js, a popular Node.js web application framework.

Create a new directory for your project and initialize a new Node.js project:

mkdir node-docker-api
cd node-docker-api
npm init -y
npm install express

Install Express.js:


Create an app.js file and add the following code:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello Docker!');
});

app.listen(port, () => {
  console.log(`App running on http://localhost:${port}`);
});

This will create a simple API that returns "Hello Docker!" when the root (/) URL is accessed.

You can start the server by running node app.js.

Dockerizing the Node.js API

Now that we have our Node.js API set up, let's dockerize it.

  1. Install Docker: Ensure Docker is installed on your machine. You can download Docker from the official Docker website.

  2. Create a Dockerfile: In the root of your project, create a new file called Dockerfile. This file defines the environment in which your application will run. Add the following content:

# Use the official Node.js runtime as a parent image
FROM node:14

# Set the working directory in the container to /app
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install the application dependencies inside the container
RUN npm install

# Copy the rest of the application to the working directory
COPY . .

# Make the container listen on port 3000
EXPOSE 3000

# Define the command to run the application
CMD [ "node", "app.js" ]

Build the Docker Image: Now we can build our Docker image which includes our app and everything it needs to run:

docker build -t node-docker-api .

The -t option lets you tag the image with a friendly name. The . specifies that the Dockerfile is in the current directory.

Run the Docker Container: Finally, you can run the application inside a Docker container using the following command:

```bash
docker run -p 3000:3000 node-docker-api
```

The -p option maps the port 3000 on the host machine to the port 3000 inside the container. This allows you to access the application from your browser at http://localhost:3000.

Conclusion

And there you have it – a Node.js API running in a Docker container! By using Docker, you can ensure that your application runs consistently across any platform. While this example is straightforward, Docker's potential extends far beyond this, making it a powerful tool in modern software development.

© 2024 Justin Riggio. All rights reserved. DigitalOcean Referral Badge