Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DevOps Lifecycle
  • DevOps Roadmap
  • Docker Tutorial
  • Kubernetes Tutorials
  • Amazon Web Services [AWS] Tutorial
  • AZURE Tutorials
  • GCP Tutorials
  • Docker Cheat sheet
  • Kubernetes cheat sheet
  • AWS interview questions
  • Docker Interview Questions
  • Ansible Interview Questions
  • Jenkins Interview Questions
Open In App
Next Article:
Docker - Docker Container for Node.js
Next article icon

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.

Table of Content

  • Primary Terminologies
  • Step-by-Step Process for Docker Compose with Node.js Applications
    • Build the Docker Image
    • View docker logs
  • Conclusion
  • Docker Compose for Node.js Applications: Best Practices - FAQs

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
Docker - Docker Container for Node.js
author
sadasivareddy
Improve
Article Tags :
  • Docker
  • DevOps

Similar Reads

  • Docker Compose for PHP Applications: Best Practices
    In the domain of web development, PHP provides the service as a widely utilized scripting language in being part of websites web applications, and APIs. It is embedded in HTML and is executed on the server side. It produces the dynamic web page content helping manage the dependencies, configurations
    8 min read
  • Docker Compose For Java Applications: Simplifying Deployment
    Docker is an open-source platform for developers and sysadmins to build, ship, and run distributed applications. If we want to define and run more than one container then we should go for docker compose. We have to configure multiple container environments in a single file so that it simplifies the
    8 min read
  • Docker - Docker Container for Node.js
    Node.js is an open-source, asynchronous event-driven JavaScript runtime that is used to run JavaScript applications. It is widely used for traditional websites and as API servers. At the same time, a Docker container is an isolated, deployable unit that packages an application along with its depende
    12 min read
  • How To Use Docker For IoT Applications?
    Docker is a super tool that makes our lives much less complicated by providing us with standardization, productivity, performance, maintainability, and compatibility of our code. It lets us continuously and hastily install and test our code, and it is platform-impartial. Docker provides the ability
    8 min read
  • Docker Compose for Python Applications: A Comprehensive Guide
    In the domain of present day software development, Python has arisen as a powerhouse language, leaned toward for its simplicity, readability, and flexibility. With Python, developers can make a wide cluster of uses, from web services and APIs to data handling pipelines and AI models. Notwithstanding
    6 min read
  • Docker Compose Tool To Run aMulti Container Applications
    The article talks about how to run multi-container applications using a single command. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can configure a file (YAML file) to configure your docker containers. Then Once you configured the Yaml fil
    8 min read
  • How to Run GUI Based Applications inside Docker?
    A Docker Container is an isolated application platform that contains everything needed to run an application built from one or more images. Docker is an Open Source project that provides an open platform to run any number of applications inside a container according to your requirements and you can
    7 min read
  • Node First Application
    NodeJS is widely used for building scalable and high-performance applications, particularly for server-side development. It is commonly employed for web servers, APIs, real-time applications, and microservices. Perfect for handling concurrent requests due to its non-blocking I/O model.Used in buildi
    4 min read
  • Building and Managing .NET Applications with Docker Compose
    Modern software development involves putting together applications from a variety of services that have to collaborate. For .NET developers, this can be really complex if everything is done in the development environment. Docker Compose provides an easy way for developers to define and manage multi-
    5 min read
  • Docker Compose For Windows: Tips And Tricks
    Docker is a powerful Container platform that allows you to create containers where you can run, deploy, and develop applications without worrying about different dependencies, services, and limitations of the operating system. The best part is the isolation of the container so you can use this conta
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences