🍃
Database & Messaging

MongoDB

Document database for flexible JSON-like data

🐧 Linux🍎 Mac🪟 Windows📋 Quick reference →
Reviewed: Tested on: Kubernetes 1.29 · Terraform 1.8 · Ubuntu 22.04

What is this?

MongoDB stores flexible JSON-like documents — great for apps with changing data shapes.

📁

Config files for MongoDB

Where to create or edit the main configuration — paths below match the setup steps.

  • /etc/mongod.conf

    Location: MongoDB server

    Storage, replication, security

📥

Step 01

Install MongoDB

(01)Install MongoDB

Linux
1curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
2echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
3sudo apt-get update && sudo apt-get install -y mongodb-org
⚙️

Step 02

Configure MongoDB

(01)Configure MongoDB

Linux
1mongosh
2use mydb
3db.users.insertOne({name: 'test'})

Step 03

Verify

(01)Verify installation

Linux
1mongosh --eval 'db.runCommand({ ping: 1 })'

📋Config templates

4 YAML templates for MongoDB. Copy and deploy after setup.

4 ready-to-copy templates. Expand one, copy the YAML, then run the deploy commands.

docker-compose.yml

yaml
1version: "3"
2
3services:
4 app:
5 build: .
6 ports:
7 - "8080:8080"
8 depends_on:
9 - db
10 environment:
11 SERVER_PORT: 8080
12 MONGO_URI: mongodb://db:27017
13 security_opt:
14 - no-new-privileges:true
15
16 db:
17 image: mongo:latest
18 restart: always
19 ports:
20 - "27017:27017"
21 security_opt:
22 - no-new-privileges:true

Dockerfile

dockerfile
1FROM golang:1.22-alpine AS builder
2WORKDIR /app
3COPY go.mod go.sum ./
4RUN go mod download
5COPY . .
6RUN CGO_ENABLED=0 go build -o server .
7
8FROM alpine:3.19
9WORKDIR /app
10COPY --from=builder /app/server .
11EXPOSE 8080
12CMD ["./server"]
📄

Step 01

Run the Stack

(01)Build and start

Linux
1docker compose up -d --build
2docker compose ps
3curl http://localhost:8080/health