Open In App

Docker Compose for Node.js Applications

Last Updated : 16 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Docker Compose is a powerful tool that facilitates managing multi-container Docker applications. While developing Node.js applications, it usually gets to a point where you need to interact with services such as a database, message broker, or cache system. Manually handling these services can be quite cumbersome especially when you need to work in more than one environment—say development and testing. Docker Compose tackles this issue by providing a way to define, configure, and run multi-container Docker applications with a single command, this post shares best practices for using Docker Compose with Node.js to seamlessly develop and deploy.

Primary Terminologies

  • Docker: An open-source project that automates the deployment of applications inside software containers, which can be installed on any system.
  • Container: A lightweight, stand-alone, executable software package that contains everything required to run an application—code, runtime, system tools, libraries, and settings.
  • Docker Compose: A tool for defining and running multi-container Docker applications. This can be easily done by the use of a YAML file for an application's services configuration while giving the ability to start all the services through one command.
  • Dockerfile: A script that contains instructions to build a Docker image, specifying the base image, dependencies, and commands to run the application.
  • Service: A container inside a Docker Compose configuration, each service represents one or multiple containers running a particular component of the application
  • Volume: A data persistence mechanism of Docker containers that is generated and used. Volume will keep data for the container beyond the life cycle of its file system, meaning the data is still maintained even when the container is not running.
  • Network: Docker enables you to create isolated networks for better control of the interaction between containers, between containers, and the host system.

Step-by-Step Process for Docker Compose with Node.js Applications

Step 1: Install Docker

  • Install docker by using following command
sudo yum -y install docker 
To Install Docker
  • Enable docker by using following command
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
To Start,Enable and Check Status of Docker

Step 2: Install Docker Compose

  • Install docker compose using following command
sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose 
sudo chmod +x /usr/local/bin/docker-compose
To Install Docker Compose

Step 3: Create a Node.js Application

Set Up the Project Directory

  • Create a new directory for your project and navigate into it:
mkdir my-node-app
cd my-node-app
Create a New Directory

Step 4: Install Node.js and npm

  • Install prerequisites
sudo yum install -y gcc-c++ make
To Install Prerequisites
  • Add NodeSource repository
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
To Add NodeSource Repository
  • Install Node.js and npm
sudo yum install -y nodejs
To Install Node.js and Npm
  • Check versions by using following command
node --version
npm --version
To Check Versions

Step 5: Initialize Node.js Project

  • Initialize a new Node.js project and install Express:
npm init -y
To Initialize Node.js Project
npm install express
To Install Npm

Step 6: Create Application Code

  • Create an index.js file with a simple Express server:

// index.js

const express = require('express');

const app = express();

const port = 3000;

app.get('/', (req, res) => {

res.send('Hello, GeeksforGeeks..!');

});

app.listen(port, () => {

console.log(`Server running at http://localhost:${port}/`);

});

index.js file

Step 7: Create a Dockerfile

  • Create a Dockerfile in the root of your project directory:

# Use the official Node.js image.

FROM node:18

# Set the working directory in the container.

WORKDIR /usr/src/app

# Copy package.json and package-lock.json

COPY package*.json ./

# Install dependencies

RUN npm install

# Copy the rest of the application code

COPY . .

# Expose the port the app runs on

EXPOSE 3000

# Define the command to run the application

CMD [ "node", "index.js" ]

Dockerfile

Build the Docker Image

  • Build the Docker image using the Dockerfile:
docker build -t my-node-app .
To Build the Docker Image

Step 8: Create a Docker Compose File

  • Create a docker-compose.yml file in the root of your project directory:

version: '3'

services:

app:

build: .

ports:

- "3000:3000"

volumes:

- .:/usr/src/app

environment:

NODE_ENV: development

Docker-Compose.yml

Step 9: Start the Application

  • Use Docker Compose to build and start your application:
docker-compose up -d
To Start the Application

View docker logs

docker-compose logs
To View Docker Logs

Step 10: Verify the Application

  • Access your application by navigating to http://<IP address >:3000 in your web browser. You should see the message "Hello, GeeksforGeeks..!".
To Verify the Application

Conclusion

Docker Compose is a powerful utility that makes the management of multi-container Node.js applications much simpler. With the best practices that follow, you will be able to compose effective and scalable development environments, covering from development to testing and finally production. Whether your case is development, testing, or production, Docker Compose greatly simplifies the deployment process, is fast in its operations, and guarantees the same version deployed across all environments, this will enable you to master these practices and optimize your workflow while reducing potential problems and keeping a robust, reliable infrastructure for your application.


Next Article
Article Tags :

Similar Reads