Hostinger Docker: React + NestJS + PostgreSQL
Setup chi tiết cho một website chạy toàn bộ frontend, backend và PostgreSQL bằng Docker Compose trên Hostinger VPS.
Tổng quan kiến trúc
Phù hợp khi bạn có một website React + NestJS + PostgreSQL và muốn deploy sạch bằng Docker Compose.
Khuyến nghị frontend và API dùng cùng domain: example.com và
example.com/api.
Prerequisites
Chuẩn bị những thứ này trước khi chạy template.
Cấu trúc template
hostinger-docker-nest-react-postgres/
├── docker-compose.prod.yml
├── .env.example
├── deploy.sh
├── frontend/
│ ├── Dockerfile
│ ├── nginx.conf
│ └── .dockerignore
├── backend/
│ ├── Dockerfile
│ └── .dockerignore
├── nginx/
│ └── host-site.conf
├── scripts/
│ └── backup-postgres.sh
└── .github/
└── workflows/
└── deploy.yml
Các bước setup
Đi theo đúng thứ tự để dễ debug.
0.0.0.0, không chỉ localhost.
docker compose -f docker-compose.prod.yml up -d --build.
NestJS main.ts
await app.listen( process.env.PORT ?? 3000, '0.0.0.0', );
DATABASE_URL
DATABASE_URL=postgresql://myapp_user:STRONG_PASSWORD@db:5432/myapp
Build production
docker compose \ -f docker-compose.prod.yml \ up -d --build docker compose \ -f docker-compose.prod.yml \ ps
Kiểm tra sau setup
# containers docker compose -f docker-compose.prod.yml ps # backend logs docker compose -f docker-compose.prod.yml logs -f backend # database docker compose -f docker-compose.prod.yml exec db \ psql -U myapp_user -d myapp # frontend local binding curl http://127.0.0.1:8080 # public curl -I https://example.com curl https://example.com/api/health
Lỗi thường gặp
Backend container chạy nhưng frontend gọi API lỗi
Kiểm tra frontend Nginx proxy đang dùng http://backend:3000, không phải
localhost.
Backend không kết nối PostgreSQL
DATABASE_URL phải dùng hostname db. Kiểm tra DB health và credentials.
Refresh React Router bị 404
Nginx cần try_files $uri $uri/ /index.html;.
Build bị exit code 137
Thường là thiếu RAM. Thêm swap, nâng VPS hoặc chuyển build sang GitHub Actions.