diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d8e10c5 --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/backend/index.js b/backend/index.js index 74e3eb6..ff750fa 100644 --- a/backend/index.js +++ b/backend/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')); diff --git a/docker-compose.yaml b/docker-compose.yaml index 9d57b62..400d69c 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -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