feat(docker): Deploy frontend from express
This commit is contained in:
29
Dockerfile
Normal file
29
Dockerfile
Normal file
@@ -0,0 +1,29 @@
|
||||
# --- Build frontend ---
|
||||
FROM node:24-slim AS frontend-builder
|
||||
WORKDIR /frontend
|
||||
COPY frontend/package*.json ./
|
||||
RUN npm install
|
||||
COPY frontend/ .
|
||||
RUN npm run build
|
||||
|
||||
# --- Backend setup ---
|
||||
FROM node:24-slim
|
||||
|
||||
# Install ping for backend
|
||||
RUN apt-get update && \
|
||||
apt-get install -y iputils-ping && \
|
||||
apt-get clean && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy backend code
|
||||
COPY backend/package*.json ./
|
||||
RUN npm install
|
||||
COPY backend/ .
|
||||
|
||||
# Copy built frontend into backend expected location
|
||||
COPY --from=frontend-builder /frontend/dist /frontend/dist
|
||||
|
||||
EXPOSE 5000
|
||||
|
||||
CMD ["node", "index.js"]
|
||||
@@ -3,11 +3,13 @@ const mysql = require('mysql2');
|
||||
const cors = require('cors');
|
||||
const { exec } = require('child_process');
|
||||
const app = express();
|
||||
const path = require('path');
|
||||
|
||||
const db = require('./db');
|
||||
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
const serveStatic = express.static(path.join(__dirname, '../frontend/dist'));
|
||||
|
||||
app.post('/api/login', (req, res) => {
|
||||
const { username, password } = req.body;
|
||||
@@ -83,4 +85,11 @@ app.get('/api/posts', (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
app.use((req, res, next) => {
|
||||
if (!req.path.startsWith('/api')) {
|
||||
return serveStatic(req, res, next);
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
app.listen(5000, () => console.log('Backend running on port 5000'));
|
||||
|
||||
@@ -15,8 +15,8 @@ services:
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
|
||||
backend:
|
||||
build: ./backend
|
||||
service:
|
||||
build: ./
|
||||
restart: always
|
||||
ports:
|
||||
- "5000:5000"
|
||||
@@ -25,11 +25,3 @@ services:
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy
|
||||
|
||||
frontend:
|
||||
build: ./frontend
|
||||
restart: always
|
||||
ports:
|
||||
- "3000:3000"
|
||||
depends_on:
|
||||
- backend
|
||||
|
||||
Reference in New Issue
Block a user