57.10 Project 10: Node + MongoDB + mongo-express, pushed to a private registry
Friends, today we run a Node profile app with MongoDB and mongo-express, then push the image to a private registry with a login. Teams at Nykaa and Flipkart keep private images this way so only their servers can pull them. Type every command yourself.
मित्रांनो, आज आपण MongoDB आणि mongo-express सह Node profile app चालवू, मग login असलेल्या private registry वर image push करू. Nykaa आणि Flipkart च्या teams खाजगी images अशाच ठेवतात म्हणजे फक्त त्यांचे servers pull करू शकतात. प्रत्येक command स्वतः टाइप करा.
दोस्तों, आज हम MongoDB और mongo-express के साथ Node profile app चलाएंगे, फिर login वाली private registry पर image push करेंगे. Nykaa और Flipkart की teams निजी images ऐसे ही रखती हैं ताकि सिर्फ उनके servers pull कर सकें. हर command खुद टाइप कीजिए.
What you will learn in this lesson
- Why a private registry matters for company images.
- How mongo-express gives a browser UI for MongoDB.
- How registry:2 plus htpasswd adds a login.
- How to tag, push, pull and run from that registry.
- How a prod Compose file uses only the image, not a local build.
1. Start from a real problem
Suppose we are at Nykaa and a team builds a user-profile app in Node.js with MongoDB.
Developers build on laptops and then need the same image on a test server. Copying tar files by hand breaks often.
A private registry on localhost:5000 (or later Harbor or ECR) stores the image behind a login. Only people with the password can pull it.
2. Node profile app
Step 1: Folders and package
Why: A small Express API plus HTML.
What: We keep server.js, package.json and public/index.html. The app stores name and city in MongoDB.
Example 1: Make folders
Type this:
mkdir -p profile-app/public && cd profile-app && pwd
You will see:
/home/you/profile-app
What it means: Project root ready.
Example 2: Write package.json
Type this:
printf '%s\n' '{' ' "name": "profile-app",' ' "version": "1.0.0",' ' "main": "server.js",' ' "dependencies": {' ' "express": "4.19.2",' ' "mongodb": "6.8.0"' ' }' '}' > package.json && wc -l package.json
You will see:
10 package.json
What it means: Express and the official mongodb driver.
Example 3: Write server.js
Type this:
printf '%s\n' 'const express = require("express");' 'const { MongoClient } = require("mongodb");' 'const app = express();' 'app.use(express.json());' 'app.use(express.static("public"));' 'const url = process.env.MONGO_URL || "mongodb://db:27017";' 'const ver = process.env.APP_VERSION || "dev";' 'let col;' 'MongoClient.connect(url).then(c => { col = c.db("profiles").collection("users"); });' 'app.get("/healthz", (_,res) => res.json({ ok: true, version: ver }));' 'app.get("/profiles", async (_,res) => res.json(await col.find({}).toArray()));' 'app.post("/profiles", async (req,res) => { await col.insertOne(req.body); res.json({ saved: true }); });' 'app.listen(3000, () => console.log("profile-app", ver, "on 3000"));' > server.js && wc -l server.js
You will see:
14 server.js
What it means: Health, list and create. Version from env.
Step 2: HTML page and Dockerfile
Why: UI plus image build.
What: A tiny page posts a profile. The Dockerfile uses node:20-alpine and a non-root user.
Example 4: Write public/index.html
Type this:
printf '%s\n' '<!doctype html><html><body>' '<h1>Profiles</h1>' '<form id="f"><input name="name" placeholder="name"/>' '<input name="city" placeholder="city"/>' '<button>Save</button></form>' '<pre id="out"></pre>' '<script>' 'f.onsubmit=async e=>{e.preventDefault();' ' await fetch("/profiles",{method:"POST",headers:{"Content-Type":"application/json"},' ' body:JSON.stringify(Object.fromEntries(new FormData(f)))});' ' out.textContent=JSON.stringify(await (await fetch("/profiles")).json(),null,2);};' '</script></body></html>' > public/index.html && wc -c public/index.html
You will see:
420 public/index.html
What it means: Browser form posts to /profiles.
Example 5: Write Dockerfile
Type this:
printf '%s\n' 'FROM node:20-alpine' 'WORKDIR /app' 'COPY package.json .' 'RUN npm install --omit=dev' 'COPY server.js .' 'COPY public ./public' 'USER node' 'ENV APP_VERSION=1.0' 'EXPOSE 3000' 'CMD ["node","server.js"]' > Dockerfile && wc -l Dockerfile
You will see:
10 Dockerfile
What it means: Alpine Node image as user node.
Example 6: Write .dockerignore
Type this:
printf '%s\n' 'node_modules' '.env' > .dockerignore && cat .dockerignore
You will see:
node_modules
.env
What it means: Local modules stay out of the build.
3. Compose with Mongo and mongo-express
Step 3: Local stack
Why: App + database + UI.
What: mongo-express is a browser UI for MongoDB. Bind it to 127.0.0.1 so only this machine can open it.
Example 7: Write compose.yaml
Type this:
printf '%s\n' 'name: profiles' 'services:' ' db:' ' image: mongo:7' ' volumes: [mongo-data:/data/db]' ' healthcheck:' ' test: ["CMD", "mongosh", "--quiet", "--eval", "db.adminCommand(\"ping\").ok"]' ' interval: 5s' ' retries: 20' '' ' api:' ' build: .' ' image: profile-app:1.0' ' ports: ["3000:3000"]' ' environment:' ' MONGO_URL: mongodb://db:27017' ' APP_VERSION: "1.0"' ' depends_on:' ' db: {condition: service_healthy}' '' ' ui:' ' image: mongo-express:1.0.2-20' ' ports: ["127.0.0.1:8081:8081"]' ' environment:' ' ME_CONFIG_MONGODB_URL: mongodb://db:27017/' ' ME_CONFIG_BASICAUTH: "false"' ' depends_on:' ' db: {condition: service_healthy}' '' 'volumes: {mongo-data:}' > compose.yaml && wc -l compose.yaml
You will see:
32 compose.yaml
What it means: Three services. UI only on localhost.
Example 8: Start and health
Type this:
docker compose up -d --build
sleep 8
curl -s http://localhost:3000/healthz
You will see:
{"ok":true,"version":"1.0"}
What it means: API healthy with version 1.0.
Example 9: Save a profile
Type this:
curl -s -X POST http://localhost:3000/profiles -H 'Content-Type: application/json' -d '{"name":"Asha","city":"Pune"}'
curl -s http://localhost:3000/profiles
You will see:
{"saved":true}
[{"name":"Asha","city":"Pune","_id":"..."}]
What it means: Round trip through Mongo works.
Step 4: Browse with mongo-express
Why: See the document in the UI.
What: Open http://127.0.0.1:8081, pick profiles / users, and you should see Asha.
Example 10: Show UI port binding
Type this:
docker compose port ui 8081
You will see:
127.0.0.1:8081
What it means: Only this machine can open mongo-express.
Example 11: Count documents via mongosh
Type this:
docker compose exec db mongosh --quiet profiles --eval 'db.users.countDocuments()'
You will see:
1
What it means: One profile document.
Example 12: Show app version in logs
Type this:
docker compose logs api 2>&1 | grep profile-app | tail -1
You will see:
profile-app 1.0 on 3000
What it means: APP_VERSION reached the process.
4. Private registry with login
Step 5: Start registry:2
Why: Login required.
What: We create an htpasswd file and mount it. Docker on this machine trusts localhost registries over HTTP.
Example 13: Create htpasswd and registry
Type this:
mkdir -p registry-auth && docker run --rm --entrypoint htpasswd httpd:2 -Bbn ci 'ci-pass' > registry-auth/htpasswd
printf '%s\n' 'name: reg' 'services:' ' registry:' ' image: registry:2' ' ports: ["5000:5000"]' ' environment:' ' REGISTRY_AUTH: htpasswd' ' REGISTRY_AUTH_HTPASSWD_REALM: Registry' ' REGISTRY_AUTH_HTPASSWD_PATH: /auth/htpasswd' ' volumes: [./registry-auth:/auth:ro, reg-data:/var/lib/registry]' '' 'volumes: {reg-data:}' > compose.registry.yaml
docker compose -f compose.registry.yaml up -d
You will see:
... Started
What it means: Private registry on port 5000.
Example 14: Login
Type this:
echo 'ci-pass' | docker login localhost:5000 -u ci --password-stdin
You will see:
Login Succeeded
What it means: Credentials stored for localhost:5000.
Example 15: Show htpasswd exists
Type this:
wc -l registry-auth/htpasswd
You will see:
1 registry-auth/htpasswd
What it means: One user: ci.
Step 6: Tag and push
Why: Put the image in the registry.
What: Retag profile-app:1.0 as localhost:5000/profile-app:1.0 and push.
Example 16: Tag
Type this:
docker tag profile-app:1.0 localhost:5000/profile-app:1.0 && docker images 'localhost:5000/profile-app' --format '{{.Repository}}:{{.Tag}}'
You will see:
localhost:5000/profile-app:1.0
What it means: Local tag points at the same image id.
Example 17: Push
Type this:
docker push localhost:5000/profile-app:1.0
You will see:
The push refers to repository [localhost:5000/profile-app]
1.0: digest: sha256:... size: ...
What it means: Image is in the private registry.
Example 18: List from registry API
Type this:
curl -s -u ci:ci-pass http://localhost:5000/v2/_catalog
You will see:
{"repositories":["profile-app"]}
What it means: Catalog shows profile-app.
5. Pull on another machine and clean up
Step 7: Prod Compose uses only the image
Why: No local build.
What: compose.prod.yaml pulls from the registry. That is what a second VM or a CI agent would do.
Example 19: Write compose.prod.yaml
Type this:
printf '%s\n' 'name: profiles-prod' 'services:' ' db:' ' image: mongo:7' ' volumes: [mongo-data:/data/db]' ' healthcheck:' ' test: ["CMD", "mongosh", "--quiet", "--eval", "db.adminCommand(\"ping\").ok"]' ' interval: 5s' ' retries: 20' '' ' api:' ' image: localhost:5000/profile-app:1.0' ' ports: ["3001:3000"]' ' environment:' ' MONGO_URL: mongodb://db:27017' ' APP_VERSION: "1.0"' ' depends_on:' ' db: {condition: service_healthy}' '' 'volumes: {mongo-data:}' > compose.prod.yaml && wc -l compose.prod.yaml
You will see:
22 compose.prod.yaml
What it means: No build: key. Only image:.
Example 20: Remove local app image and pull
Type this:
docker image rm profile-app:1.0 localhost:5000/profile-app:1.0
docker pull localhost:5000/profile-app:1.0
You will see:
1.0: Pulling from profile-app
Digest: sha256:...
Status: Downloaded newer image
What it means: Pull from the private registry worked.
Example 21: Run prod stack
Type this:
docker compose -f compose.prod.yaml up -d
sleep 8
curl -s http://localhost:3001/healthz
You will see:
{"ok":true,"version":"1.0"}
What it means: Second stack runs from the registry image.
Step 8: Common mistakes
Why: Insecure registries and open UIs.
What: A registry without HTTPS on a real hostname needs an insecure-registries entry. mongo-express on 0.0.0.0 is dangerous.
Example 22: Why localhost is special
Type this:
echo 'Docker allows HTTP to localhost registries by default'
You will see:
Docker allows HTTP to localhost registries by default
What it means: Remote hostnames need TLS or a daemon config.
Example 23: UI bound to loopback
Type this:
grep '127.0.0.1:8081' compose.yaml
You will see:
ports: ["127.0.0.1:8081:8081"]
What it means: mongo-express is not on the public network.
Example 24: Login before push
Type this:
docker logout localhost:5000 >/dev/null; docker push localhost:5000/profile-app:1.0 2>&1 | head -2
You will see:
unauthorized: authentication required
What it means: Push without login fails. Good.
Step 9: Clean up
Why: Stop stacks and registry.
What: down -v each Compose file. Remove local tags when finished.
Example 25: Stop app stacks
Type this:
docker compose down -v
docker compose -f compose.prod.yaml down -v
You will see:
... Removed
What it means: profiles and profiles-prod gone.
Example 26: Stop registry
Type this:
docker compose -f compose.registry.yaml down -v
You will see:
... Removed
What it means: Registry volume removed.
Example 27: Remove leftover images
Type this:
docker image rm localhost:5000/profile-app:1.0 2>/dev/null; echo done
You will see:
done
What it means: Disk space returned.
Step 10: Version in the image
Why: Know what is running.
What: Bake APP_VERSION at build time with ARG, or pass it at run time. Ops teams at Flipkart read the version from /healthz during rollouts.
Example 28: Show version endpoint
Type this:
curl -s http://localhost:3000/healthz 2>/dev/null || echo 'stack stopped — version was 1.0'
You will see:
stack stopped — version was 1.0
What it means: You saw version 1.0 while the stack was up.
Example 29: ARG idea for builds
Type this:
echo 'ARG APP_VERSION=1.0 then ENV APP_VERSION=$APP_VERSION in Dockerfile'
You will see:
ARG APP_VERSION=1.0 then ENV APP_VERSION=$APP_VERSION in Dockerfile
What it means: Build-time version becomes a runtime env.
Example 30: Tag equals version
Type this:
echo 'localhost:5000/profile-app:1.0 matches APP_VERSION 1.0'
You will see:
localhost:5000/profile-app:1.0 matches APP_VERSION 1.0
What it means: Keep tag and health version in sync.
Step 11: Registry catalog and tags
Why: Prove what was pushed.
What: The v2 API lists repositories and tags. Ops teams check this after CI pushes.
Example 31: List tags
Type this:
curl -s -u ci:ci-pass http://localhost:5000/v2/profile-app/tags/list
You will see:
{"name":"profile-app","tags":["1.0"]}
What it means: Tag 1.0 is present.
Example 32: Unauthorized catalog
Type this:
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:5000/v2/_catalog
You will see:
401
What it means: No credentials means denied.
Example 33: Digest stays stable
Type this:
docker image inspect localhost:5000/profile-app:1.0 --format '{{index .RepoDigests 0}}' | cut -d@ -f2 | cut -c1-20
You will see:
sha256:...
What it means: Digest identifies the exact bytes.
Step 12: Build-time version ARG
Why: Bake the tag into the image.
What: Pass --build-arg APP_VERSION=1.0 so /healthz matches the registry tag without a runtime override.
Example 34: Dockerfile ARG pattern
Type this:
printf '%s\n' 'FROM node:20-alpine' 'ARG APP_VERSION=dev' 'ENV APP_VERSION=$APP_VERSION' 'WORKDIR /app' 'COPY package.json .' 'RUN npm install --omit=dev' 'COPY server.js .' 'COPY public ./public' 'USER node' 'EXPOSE 3000' 'CMD ["node","server.js"]' > Dockerfile.versioned && grep APP_VERSION Dockerfile.versioned
You will see:
ARG APP_VERSION=dev
ENV APP_VERSION=$APP_VERSION
What it means: ARG becomes ENV for the running process.
Example 35: Build with arg
Type this:
docker build -f Dockerfile.versioned --build-arg APP_VERSION=1.0 -t profile-app:arg .
You will see:
... FINISHED
What it means: Image built with version 1.0 baked in.
Example 36: Read baked version
Type this:
docker run --rm --entrypoint printenv profile-app:arg APP_VERSION
You will see:
1.0
What it means: Health can report the same value CI tagged.
Ravindra Bagale's Tip
Push company images to a private registry with a login. Nykaa and Flipkart teams never leave production images on a public Hub repo without a reason.
Login असलेल्या private registry वर company images push करा. Nykaa आणि Flipkart च्या teams production images बिना कारण public Hub repo वर सोडत नाहीत.
Login वाली private registry पर company images push करें. Nykaa और Flipkart की teams बिना वजह production images को public Hub repo पर नहीं छोड़तीं.
Check yourself
Q1. Why use a private registry?
So only logged-in users and servers can pull your company images.
Q2. What does mongo-express give you?
A browser UI to see MongoDB databases, collections and documents.
Q3. How do you add a login to registry:2?
An htpasswd file and REGISTRY_AUTH=htpasswd.
Q4. Why bind mongo-express to 127.0.0.1?
So the UI is not open on the public network.
Q5. What is different in compose.prod.yaml?
It uses image: from the registry and has no build: key.
Q6. What happens if you push without docker login?
The registry returns unauthorized.
Friends, you ran a Node profile app with MongoDB and mongo-express, stood up a private registry with htpasswd, pushed, pulled and ran from that image. Did you understand? Let us try it once more. If not, look at Pictures 1 and 2 and repeat the login, tag and push steps. Next project: self-hosted Portainer, Gitea, Nextcloud and Uptime Kuma.
मित्रांनो, आज तुम्ही MongoDB व mongo-express सह Node profile app चालवला, htpasswd सह private registry उभी केली, push-pull करून image चालवली. नसेल तर Picture 1 आणि 2 बघा आणि login, tag, push पावले पुन्हा करा. पुढचा project: self-hosted Portainer, Gitea, Nextcloud आणि Uptime Kuma.
दोस्तों, आज आपने MongoDB व mongo-express के साथ Node profile app चलाया, htpasswd के साथ private registry खड़ी की, push-pull कर image चलाई. नहीं तो Picture 1 और 2 देखिए और login, tag, push कदम दोहराइए. अगला project: self-hosted Portainer, Gitea, Nextcloud और Uptime Kuma.
Lab: try at home
- Write the Node profile app and Dockerfile.
- Start Compose with Mongo and mongo-express.
- Create a profile and open the UI on 127.0.0.1:8081.
- Start registry:2 with htpasswd and docker login.
- Tag, push, remove local image, pull again.
- Run compose.prod.yaml from the registry image.
- Clean up all three Compose projects.
Recap
- Private registries protect company images.
- mongo-express is a MongoDB browser UI.
- htpasswd adds a login to registry:2.
- Prod Compose pulls images; it does not build.
- Bind admin UIs to 127.0.0.1.
- Keep image tags and APP_VERSION in sync.
Revision card
Interview card