Ravindra BagaleCourses & study guides मराठी

Chapter 57: Docker Projects Part 1: Websites, Databases, LAMP, LEMP, WordPress, 3-Tier and Every Language with MySQL and MongoDB

DevOps

57.11 Project 11: Self-hosted team tools: Portainer, Gitea, Nextcloud and Uptime Kuma

Friends, today we run Portainer, Gitea, Nextcloud and Uptime Kuma on one Docker host, then put them behind one nginx with host names. A 40-person Pune startup that cannot send code to outside services uses this pattern. Type every command yourself.

What you will learn in this lesson

  1. Why a small company self-hosts Git, files and monitoring.
  2. How Portainer talks to docker.sock safely on localhost.
  3. How Gitea and Nextcloud store data in named volumes.
  4. How nginx routes by Host header on port 80.
  5. How to back up every volume with a throwaway container.

1. Start from a real problem

Suppose we run IT for a 40-person startup in Pune that cannot send its code and files to outside services because a bank client forbids it.

The team still needs Git, a file drive, a Docker UI and an uptime checker.

With Docker Compose we run Portainer, Gitea, Nextcloud and Uptime Kuma on one VM and put nginx in front with names like git.lab.local.

Browsergit.lab.local / drive.lab.localnginx :80picks backend by HostPortainer Gitea Nextcloud Kumaeach on its own network aliasNamed volumesdata survives restarts
Picture 1: four tools behind nginx

2. Portainer alone

Step 1: Folders and Portainer

Why: Manage Docker from the browser.

What: Portainer CE mounts docker.sock. Bind the UI to 127.0.0.1 and treat it like root access.

Example 1: Make folders

Type this:

mkdir -p selfhost && cd selfhost && pwd

You will see:

/home/you/selfhost

What it means: Project root ready.

Example 2: Write compose.portainer.yaml

Type this:

printf '%s\n' 'name: portainer' 'services:' '  portainer:' '    image: portainer/portainer-ce:2.21.4' '    ports: ["127.0.0.1:9000:9000"]' '    volumes:' '      - /var/run/docker.sock:/var/run/docker.sock' '      - portainer-data:/data' '    restart: unless-stopped' '' 'volumes: {portainer-data:}' > compose.portainer.yaml && wc -l compose.portainer.yaml

You will see:

12 compose.portainer.yaml

What it means: CE image, sock mount, localhost UI.

Example 3: Start and check

Type this:

docker compose -f compose.portainer.yaml up -d
docker compose -f compose.portainer.yaml ps

You will see:

NAME                STATUS
portainer-portainer-1   Up

What it means: Portainer is up on 127.0.0.1:9000.

Step 2: Why sock access is powerful

Why: Portainer equals root on Docker.

What: Anyone who can open Portainer can start privileged containers. Keep it on loopback or behind SSO.

Example 4: Show sock mount

Type this:

grep docker.sock compose.portainer.yaml

You will see:

- /var/run/docker.sock:/var/run/docker.sock

What it means: Sock gives full Docker control.

Example 5: Show loopback bind

Type this:

grep '127.0.0.1:9000' compose.portainer.yaml

You will see:

ports: ["127.0.0.1:9000:9000"]

What it means: Only this machine reaches the UI.

Example 6: List from Portainer host view idea

Type this:

docker ps --format '{{.Names}}' | head -5

You will see:

portainer-portainer-1

What it means: Same list Portainer shows in Containers.

3. Gitea and Nextcloud

Step 3: Gitea with Postgres

Why: Push a repo over HTTP.

What: Gitea is a light self-hosted Git service. Postgres holds metadata; a volume holds repos.

Example 7: Write compose.gitea.yaml

Type this:

printf '%s\n' 'name: gitea' 'services:' '  db:' '    image: postgres:16-alpine' '    environment:' '      POSTGRES_USER: gitea' '      POSTGRES_PASSWORD: ${GITEA_DB_PASSWORD}' '      POSTGRES_DB: gitea' '    volumes: [gitea-db:/var/lib/postgresql/data]' '    healthcheck:' '      test: ["CMD-SHELL", "pg_isready -U gitea -d gitea"]' '      interval: 5s' '      retries: 20' '' '  gitea:' '    image: gitea/gitea:1.22.3' '    ports: ["127.0.0.1:3000:3000"]' '    environment:' '      GITEA__database__DB_TYPE: postgres' '      GITEA__database__HOST: db:5432' '      GITEA__database__NAME: gitea' '      GITEA__database__USER: gitea' '      GITEA__database__PASSWD: ${GITEA_DB_PASSWORD}' '    volumes: [gitea-data:/data]' '    depends_on:' '      db: {condition: service_healthy}' '' 'volumes: {gitea-db:, gitea-data:}' > compose.gitea.yaml
printf '%s\n' 'GITEA_DB_PASSWORD=gitea-pass' >> .env && wc -l compose.gitea.yaml

You will see:

28 compose.gitea.yaml

What it means: Gitea + Postgres on localhost:3000.

Example 8: Start Gitea

Type this:

docker compose -f compose.gitea.yaml --env-file .env up -d
sleep 10
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:3000

You will see:

200

What it means: Install wizard is reachable.

Example 9: Show volumes

Type this:

docker volume ls | grep gitea

You will see:

gitea_gitea-data
gitea_gitea-db

What it means: Data and database volumes exist.

Step 4: Nextcloud with MariaDB and Redis

Why: Upload a file.

What: Nextcloud needs a database and benefits from Redis for locking. Bind the UI to loopback for the lab.

Example 10: Write compose.nextcloud.yaml

Type this:

printf '%s\n' 'name: nextcloud' 'services:' '  db:' '    image: mariadb:11.4' '    environment:' '      MYSQL_ROOT_PASSWORD: ${NC_ROOT_PASSWORD}' '      MYSQL_DATABASE: nextcloud' '      MYSQL_USER: nextcloud' '      MYSQL_PASSWORD: ${NC_DB_PASSWORD}' '    volumes: [nc-db:/var/lib/mysql]' '    healthcheck:' '      test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]' '      interval: 5s' '      retries: 20' '' '  redis:' '    image: redis:7-alpine' '    volumes: [nc-redis:/data]' '' '  app:' '    image: nextcloud:30-apache' '    ports: ["127.0.0.1:8080:80"]' '    environment:' '      MYSQL_HOST: db' '      MYSQL_DATABASE: nextcloud' '      MYSQL_USER: nextcloud' '      MYSQL_PASSWORD: ${NC_DB_PASSWORD}' '      REDIS_HOST: redis' '    volumes: [nc-data:/var/www/html]' '    depends_on:' '      db: {condition: service_healthy}' '      redis: {condition: service_started}' '' 'volumes: {nc-db:, nc-redis:, nc-data:}' > compose.nextcloud.yaml
printf '%s\n' 'NC_ROOT_PASSWORD=root-pass' 'NC_DB_PASSWORD=nc-pass' >> .env && docker compose -f compose.nextcloud.yaml --env-file .env up -d

You will see:

... Started

What it means: Nextcloud on 127.0.0.1:8080.

Example 11: HTTP check

Type this:

curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8080

You will see:

302

What it means: Redirect to the setup or login page is normal.

Example 12: Show three volumes

Type this:

docker volume ls | grep nc-

You will see:

nextcloud_nc-data
nextcloud_nc-db
nextcloud_nc-redis

What it means: Files, DB and Redis each have a volume.

4. Uptime Kuma and nginx front door

Step 5: Uptime Kuma

Why: Watch the other three.

What: Kuma checks HTTP endpoints and can alert on Slack later. For the lab we only run it.

Example 13: Write compose.kuma.yaml

Type this:

printf '%s\n' 'name: kuma' 'services:' '  uptime-kuma:' '    image: louislam/uptime-kuma:1.23.15' '    ports: ["127.0.0.1:3001:3001"]' '    volumes: [kuma-data:/app/data]' '    restart: unless-stopped' '' 'volumes: {kuma-data:}' > compose.kuma.yaml && docker compose -f compose.kuma.yaml up -d

You will see:

... Started

What it means: Kuma on 127.0.0.1:3001.

Example 14: HTTP check

Type this:

curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:3001

You will see:

200

What it means: Status page is up.

Example 15: Show volume

Type this:

docker volume ls | grep kuma

You will see:

kuma_kuma-data

What it means: Monitor config lives in the volume.

Host: git.lab.localproxy_pass gitea:3000Host: drive.lab.localproxy_pass nextcloud:80Host: monitor.lab.localproxy_pass kuma:3001/etc/hostspoints names to 127.0.0.1
Picture 2: Host header routing

Step 6: nginx with server names

Why: One port 80 for all.

What: Add lab names to /etc/hosts. nginx picks the backend from the Host header.

Example 16: Write nginx.conf

Type this:

mkdir -p nginx && printf '%s\n' 'server {' '  listen 80;' '  server_name git.lab.local;' '  location / { proxy_pass http://host.docker.internal:3000; proxy_set_header Host $host; }' '}' 'server {' '  listen 80;' '  server_name drive.lab.local;' '  location / { proxy_pass http://host.docker.internal:8080; proxy_set_header Host $host; }' '}' 'server {' '  listen 80;' '  server_name monitor.lab.local;' '  location / { proxy_pass http://host.docker.internal:3001; proxy_set_header Host $host; }' '}' > nginx/default.conf && wc -l nginx/default.conf

You will see:

15 nginx/default.conf

What it means: Three server blocks by host name.

Example 17: Write compose.nginx.yaml

Type this:

printf '%s\n' 'name: edge' 'services:' '  nginx:' '    image: nginx:1.27-alpine' '    ports: ["80:80"]' '    volumes: [./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro]' '    extra_hosts: ["host.docker.internal:host-gateway"]' '    restart: unless-stopped' > compose.nginx.yaml && docker compose -f compose.nginx.yaml up -d

You will see:

... Started

What it means: Edge nginx on port 80.

Example 18: Add hosts and curl

Type this:

grep -q lab.local /etc/hosts || echo '127.0.0.1 git.lab.local drive.lab.local monitor.lab.local' | sudo tee -a /etc/hosts
curl -s -o /dev/null -w '%{http_code} %{url_effective}\n' -H 'Host: git.lab.local' http://127.0.0.1/

You will see:

200 http://127.0.0.1/

What it means: Host header reached Gitea through nginx.

5. Backups, mistakes and clean up

Step 7: Back up every volume

Why: tar from a throwaway container.

What: A busybox container mounts the volume and writes a tar to a bind mount. Banks insist on this before upgrades.

Example 19: Backup script idea

Type this:

mkdir -p backups && VOL=gitea_gitea-data && docker run --rm -v "$VOL":/data:ro -v "$PWD/backups":/out busybox:1.36 tar czf /out/gitea-data.tgz -C /data . && ls -lh backups/gitea-data.tgz

You will see:

-rw-r--r-- 1 root root ... backups/gitea-data.tgz

What it means: Gitea data archived.

Example 20: List backups

Type this:

ls backups

You will see:

gitea-data.tgz

What it means: One archive ready to copy off the box.

Example 21: Restore idea

Type this:

echo 'docker run --rm -v VOL:/data -v $PWD/backups:/in busybox tar xzf /in/gitea-data.tgz -C /data'

You will see:

docker run --rm -v VOL:/data -v $PWD/backups:/in busybox tar xzf /in/gitea-data.tgz -C /data

What it means: Same pattern restores into an empty volume.

Step 8: Common mistakes

Why: Open UIs and forgotten volumes.

What: Publishing Portainer on 0.0.0.0:9000 on a public IP is a breach waiting to happen. Forgetting volume backups loses Git history.

Example 22: Loopback on every admin UI

Type this:

grep -h '127.0.0.1' compose.*.yaml | sort -u | head -5

You will see:

ports: ["127.0.0.1:3000:3000"]
ports: ["127.0.0.1:3001:3001"]
ports: ["127.0.0.1:8080:80"]
ports: ["127.0.0.1:9000:9000"]

What it means: All admin ports on loopback.

Example 23: Count named volumes

Type this:

docker volume ls | grep -E 'gitea|nextcloud|kuma|portainer' | wc -l

You will see:

8

What it means: Eight volumes hold state.

Example 24: nginx tests config

Type this:

docker compose -f compose.nginx.yaml exec nginx nginx -t

You will see:

nginx: configuration file ... test is successful

What it means: Bad Host blocks fail here, not at runtime.

Step 9: Clean up

Why: Stop all five Compose projects.

What: down -v removes containers and volumes. Keep backups/ if you want the tar files.

Example 25: Stop edge and tools

Type this:

docker compose -f compose.nginx.yaml down -v
docker compose -f compose.kuma.yaml down -v
docker compose -f compose.nextcloud.yaml down -v

You will see:

... Removed

What it means: Edge, Kuma and Nextcloud gone.

Example 26: Stop Gitea and Portainer

Type this:

docker compose -f compose.gitea.yaml down -v
docker compose -f compose.portainer.yaml down -v

You will see:

... Removed

What it means: Gitea and Portainer gone.

Example 27: Confirm volumes gone

Type this:

docker volume ls | grep -E 'gitea|nextcloud|kuma|portainer' | wc -l

You will see:

0

What it means: Named volumes removed.

Step 10: One compose vs many

Why: Why separate files help.

What: Separate Compose files let you restart Nextcloud without touching Gitea. A single big file is fine once you know each tool.

Example 28: List compose files

Type this:

ls compose.*.yaml

You will see:

compose.gitea.yaml
compose.kuma.yaml
compose.nextcloud.yaml
compose.nginx.yaml
compose.portainer.yaml

What it means: Five small files.

Example 29: Restart only Kuma

Type this:

docker compose -f compose.kuma.yaml restart

You will see:

... Restarted

What it means: Other tools stay up.

Example 30: Name prefixes

Type this:

docker ps -a --format '{{.Names}}' | head -3

You will see:

gitea-gitea-1
kuma-uptime-kuma-1
...

What it means: Project name prefixes avoid clashes.

Step 11: SSH tunnel from a laptop

Why: Reach loopback UIs remotely.

What: On a real VM you keep UIs on 127.0.0.1 and open an SSH tunnel from your laptop. Never publish Portainer on a public IP.

Example 31: Tunnel idea

Type this:

echo 'ssh -L 9000:127.0.0.1:9000 ubuntu@vm-ip'

You will see:

ssh -L 9000:127.0.0.1:9000 ubuntu@vm-ip

What it means: Laptop browser opens localhost:9000 which forwards to the VM.

Example 32: Same for Gitea

Type this:

echo 'ssh -L 3000:127.0.0.1:3000 ubuntu@vm-ip'

You will see:

ssh -L 3000:127.0.0.1:3000 ubuntu@vm-ip

What it means: Git UI stays private on the server.

Example 33: Why not 0.0.0.0

Type this:

echo 'Public Portainer with docker.sock is a full host compromise'

You will see:

Public Portainer with docker.sock is a full host compromise

What it means: Loopback plus tunnel is the classroom rule.

Step 12: Health checks for the edge

Why: nginx and backends.

What: curl each Host name after nginx starts. Kuma can watch the same URLs later.

Example 34: Drive through nginx

Type this:

curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: drive.lab.local' http://127.0.0.1/

You will see:

302

What it means: Nextcloud answers through the edge.

Example 35: Monitor through nginx

Type this:

curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: monitor.lab.local' http://127.0.0.1/

You will see:

200

What it means: Kuma answers through the edge.

Example 36: Unknown host

Type this:

curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: other.lab.local' http://127.0.0.1/

You will see:

404

What it means: No server block means nginx default 404.

Ravindra Bagale's Tip

Self-host Git and files when a client forbids SaaS. Put every admin UI on 127.0.0.1 or behind SSO, and back up volumes before every upgrade.

Check yourself

Q1. Why bind Portainer to 127.0.0.1?

Because sock access equals root on Docker and must not be public.

Q2. What stores Gitea git data?

A named volume mounted at /data.

Q3. How does nginx choose Gitea versus Nextcloud?

The Host header, like git.lab.local versus drive.lab.local.

Q4. What is Uptime Kuma for?

Checking whether websites and ports stay up.

Q5. How do you back up a volume?

Run a throwaway container that tar-gzips the volume into a bind mount.

Q6. Why use separate Compose files?

So you can restart one tool without touching the others.

Friends, you ran Portainer, Gitea, Nextcloud and Uptime Kuma, put them behind nginx host names, and backed up a volume. Did you understand? Let us try it once more. If not, look at Pictures 1 and 2 and repeat the Host header curl. Next project: Jupyter Lab and a FastAPI model server.

Lab: try at home

  1. Start Portainer on 127.0.0.1:9000.
  2. Start Gitea with Postgres and open the wizard.
  3. Start Nextcloud with MariaDB and Redis.
  4. Start Uptime Kuma.
  5. Put nginx in front with lab.local names.
  6. Back up the Gitea data volume to backups/.
  7. Clean up all five Compose projects.

Recap

  • Self-host when SaaS is forbidden.
  • Admin UIs stay on 127.0.0.1.
  • Named volumes hold Git and files.
  • nginx routes by Host header.
  • Back up volumes before upgrades.
  • Separate Compose files ease restarts.

Ravi Sir's Cards for this lesson

मराठी cards