Subscribe
How to Dockerize a Node.js Application with Docker Compose
4 mins read

By: vishwesh

How to Dockerize a Node.js Application with Docker Compose

Docker is a platform that allows you to run applications in containers. Containers are lightweight, portable, and self-contained, which makes them a great choice for deploying and running applications. Docker Compose is a tool that allows you to define and run multi-container Docker applications. In this article, we will explore how to use Docker Compose to Dockerize a Node.js application.

Prerequisites

Before we start, make sure that you have the following installed on your system:

  • Docker
  • Docker Compose
  • Node.js

If you don't have Docker and Docker Compose installed, you can download them from the Docker website. Node.js can be downloaded from the official Node.js website.

Step 1: Create a Node.js application

First, let's create a simple Node.js application. Open your terminal and run the following commands:

mkdir myapp
cd myapp
npm init

This will create a new directory called myapp, navigate you into the directory, and initialize a new Node.js project.

Next, let's create an index.js file 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}/`);
});

This creates a simple HTTP server that listens on port 3000 and responds with Hello World when you visit http://localhost:3000 in your web browser.

Step 2: Create a Dockerfile

Now that we have a Node.js application, we need to create a Dockerfile to define how the application should be built and run in a container. Create a new file called Dockerfile in the root of your project directory and add the following code:

FROM node:14-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

Let's go through each line of the Dockerfile:

  • FROM node:14-alpine - This specifies the base image for our Docker image. We are using the official Node.js image with Alpine Linux as the base operating system.
  • WORKDIR /app - This sets the working directory inside the container to /app.
  • COPY package*.json ./ - This copies the package.json and package-lock.json files from our local directory to the /app directory inside the container.
  • RUN npm install - This runs the npm install command to install the dependencies defined in the package.json file.
  • COPY . . - This copies all the files from our local directory to the /app directory inside the container.
  • EXPOSE 3000 - This exposes port 3000 inside the container.
  • CMD ["npm", "start"] - This runs the npm start command to start our Node.js application.

Step 3: Define a Docker Compose file

Now that we have a Dockerfile, we can use Docker Compose to define how our application should be run in multiple containers.

Create a new file called docker-compose.yml in the root of your project directory and add the following code:

version: '3'

services:
  app:
    build: .
    ports:
      - '3000:3000'

Let's go through each line of the Docker Compose file:

  • version: '3' - This specifies the version of Docker Compose that we are using.
  • services: - This defines the services that we want to run.
  • app: - This is the name of the service.
  • build: . - This tells Docker Compose to build the Docker image for our Node.js application using the Dockerfile in the current directory (.).
  • ports: - This specifies the ports that we want to expose.
  • - '3000:3000' - This maps port 3000 on the host to port 3000 inside the container.

Step 4: Build and run the application with Docker Compose

Now that we have defined our Docker Compose file, we can use it to build and run our Node.js application.

Open your terminal and navigate to the root of your project directory. Run the following command to build the Docker image and start the containers:

docker-compose up --build

This will build the Docker image for your Node.js application and start the containers. Your Node.js application should now be running. Visit http://localhost:3000 in your web browser to see the Hello World message.

Stopping the application

To stop the application, press Ctrl + C in your terminal. This will stop the containers.

Alternatively, you can run the following command to stop the containers:

docker-compose down

Conclusion

In this article, we have explored how to use Docker Compose to Dockerize a Node.js application. We started by creating a simple Node.js application, then created a Dockerfile to define how the application should be built and run in a container. We then defined a Docker Compose file to define how our application should be run in multiple containers. Finally, we built and ran the application with Docker Compose.

Docker and Docker Compose are powerful tools that can make it much easier to deploy and manage applications. By Dockerizing your applications, you can ensure that they run consistently across different environments and are easily deployable to production.

Recent posts

Don't miss the latest trends

    Popular Posts

    Popular Categories