Home Explore Blog CI



docker

2nd chunk of `content/guides/nodejs/develop.md`
8a15e0efea666fee9a20f35afd254cf98353a2278a5e7c300000000100000fb9
         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".
   # 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
         POSTGRES_HOST: db
         POSTGRES_USER: postgres
         POSTGRES_PASSWORD_FILE: /run/secrets/db-password
         POSTGRES_DB: example
       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
   ```

5. Add the `secrets` section under the `server` service so that your application securely handles the database password. The following is the updated `compose.yaml` file.

   ```yaml {hl_lines="33-34",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
         POSTGRES_HOST: db
         POSTGRES_USER: postgres
         POSTGRES_PASSWORD_FILE: /run/secrets/db-password
         POSTGRES_DB: example
       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

Title: Configuring Database Environment Variables and Secrets
Summary
This section describes how to configure environment variables and secrets for the Node.js application to connect to the PostgreSQL database defined in the `compose.yaml` file. It involves adding environment variables such as `POSTGRES_HOST`, `POSTGRES_USER`, `POSTGRES_PASSWORD_FILE`, and `POSTGRES_DB` to the `server` service definition in `compose.yaml`. Additionally, it guides you to add a `secrets` section under the `server` service to securely handle the database password.