Home Explore Blog CI



docker

1st chunk of `content/guides/nodejs/develop.md`
9299d4f18c34c7e1eeaf937a85395fda6cef7d9dce75ba720000000100000fc3
---
title: Use containers for Node.js development
linkTitle: Develop your app
weight: 20
keywords: node, node.js, development
description: Learn how to develop your Node.js application locally using containers.
aliases:
  - /get-started/nodejs/develop/
  - /language/nodejs/develop/
  - /guides/language/nodejs/develop/
---

## Prerequisites

Complete [Containerize a Node.js application](containerize.md).

## Overview

In this section, you'll learn how to set up a development environment for your containerized application. This includes:

- Adding a local database and persisting data
- Configuring your container to run a development environment
- Debugging your containerized application

## Add a local database and persist data

You can use containers to set up local services, like a database. In this section, you'll update the `compose.yaml` file to define a database service and a volume to persist data.

1. Open your `compose.yaml` file in an IDE or text editor.
2. Uncomment the database related instructions. The following is the updated
   `compose.yaml` file.

   > [!IMPORTANT]
   >
   > For this section, don't run `docker compose up` until you are instructed to. Running the command at intermediate points may incorrectly initialize your database.

   ```yaml {hl_lines="26-51",collapse=true,title=compose.yaml}
   # Comments are provided throughout this file to help you get started.
   # If you need more help, visit the Docker Compose reference guide at
   # https://docs.docker.com/go/compose-spec-reference/

   # Here the instructions define your application as a service called "server".
   # This service is built from the Dockerfile in the current directory.
   # You can add other services your application may depend on here, such as a
   # database or a cache. For examples, see the Awesome Compose repository:
   # https://github.com/docker/awesome-compose
   services:
     server:
       build:
         context: .
       environment:
         NODE_ENV: production
       ports:
         - 3000:3000

       # The commented out section below is an example of how to define a PostgreSQL
       # database that your application can use. `depends_on` tells Docker Compose to
       # start the database before your application. The `db-data` volume persists the
       # database data between container restarts. The `db-password` secret is used
       # to set the database password. You must create `db/password.txt` and add
       # a password of your choosing to it before running `docker compose up`.

       depends_on:
         db:
           condition: service_healthy
     db:
       image: postgres
       restart: always
       user: postgres
       secrets:
         - db-password
       volumes:
         - db-data:/var/lib/postgresql/data
       environment:
         - POSTGRES_DB=example
         - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
       expose:
         - 5432
       healthcheck:
         test: ["CMD", "pg_isready"]
         interval: 10s
         timeout: 5s
         retries: 5
   volumes:
     db-data:
   secrets:
     db-password:
       file: db/password.txt
   ```

   > [!NOTE]
   >
   > To learn more about the instructions in the Compose file, see [Compose file
   > reference](/reference/compose-file/).

3. Open `src/persistence/postgres.js` in an IDE or text editor. You'll notice
   that this application uses a Postgres database and requires some environment
   variables in order to connect to the database. The `compose.yaml` file doesn't
   have these variables defined yet.
4. Add the environment variables that specify the database configuration. The
   following is the updated `compose.yaml` file.

   ```yaml {hl_lines="16-19",collapse=true,title=compose.yaml}
   # Comments are provided throughout this file to help you get started.
   # If you need more help, visit the Docker Compose reference guide at
   # https://docs.docker.com/go/compose-spec-reference/

   # Here the instructions define your application as a service called "server".

Title: Adding a Local Database and Persisting Data
Summary
This section guides you through setting up a local PostgreSQL database using Docker containers and persisting the database data. It involves updating the `compose.yaml` file to define a database service, configure environment variables, and create a volume for persistent data storage. The instructions include uncommenting database-related configurations in the `compose.yaml` file and adding environment variables for database connection details.