liveBook · Manning

Database

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

A database is a system that stores and manages persistent structured data for retrieval and modification.

A database stores dynamic data that an application updates. In a video-streaming service, it can hold each video’s path instead of leaving that path hardcoded in the service. The video file itself belongs in file storage; the database holds its metadata. section 4.0 section 4.5

To add MongoDB to a containerized development application:

  • Add a db service to Docker Compose using the public mongo image. Other containers reach MongoDB on port 27017; a host port mapping allows access from the host. section 4.5.2
  • Set DBHOST and DBNAME for the video-streaming service. In Node.js, connect with the mongodb driver, select the database, and use its videos collection. section 4.5.2
  • Load a video metadata record, then run the application with docker compose up --build. section 4.5.2

A video metadata record

The sample record has an _id and a videoPath of SampleVideo_1280x720_1mb.mp4. Storing the path in MongoDB removes the need to hardcode it in the video-streaming service. section 4.5.2 section 4.5

Listing 4.5 Adding a MongoDB database
version: '3'
services:
 
  db:                                      #1
    image: mongo:7.0.0                     #2
    container_name: db                     #3
 
    ports:
     - "4000:27017"                        #4
    restart: always                        #5
 
  azure-storage:
    image: azure-storage
    build: 
      context: ./azure-storage
      dockerfile: Dockerfile
    container_name: video-storage
    ports:
     - "4001:80"
    environment:
      - PORT=80
      - STORAGE_ACCOUNT_NAME=${STORAGE_ACCOUNT_NAME}
      - STORAGE_ACCESS_KEY=${STORAGE_ACCESS_KEY}
    restart: "no"
 
  video-streaming:
    image: video-streaming
    build: 
      context: ./video-streaming
      dockerfile: Dockerfile
    container_name: video-streaming
    ports:
     - "4002:3000"
    environment:
      - PORT=80
      - DBHOST=mongodb://db:27017          #6
      - DBNAME=video-streaming             #7
      - VIDEO_STORAGE_HOST=video-storage
      - VIDEO_STORAGE_PORT=80
    restart: "no"

The Compose listing shows the database container beside the application services, including its image, port mapping, and connection setting. section 4.5.2

A single database server can host multiple databases. Giving each microservice its own database preserves that separation as the application grows; a service that does not need a database need not connect to one. Docker Compose manages these containers for development, not production. This setup demonstrates integration rather than database design or data engineering. section 4.5.2 section 4.5.5 section 4.1 section 4.5

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Works through MongoDB integration in a containerized application but defers database design and data engineering.
Examples
Uses a MongoDB container, a Node.js connection, and a JSON record containing a video path.
What it assumes
Builds on an existing containerized microservice and Docker Compose setup without requiring database design instruction.
What you can do after
Apply it via Docker Compose and the mongodb driver to store video metadata for a Node.js microservice.
How it connects
Connects databases to microservices, Docker Compose, file storage, and video metadata.

Also explained in

Compare how 24 books explain database →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.