Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .envexample
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NODE_ENV = development
PORT = 5000
MONGO_URI = mongodb+srv://YOURMONGOURI
JWT_SECRET = abc123
NODE_ENV=development
PORT=5000
MONGO_URI=your_mongodb_uri
JWT_SECRET=your_secret

4 changes: 4 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
npm-debug.log
.env
.git
15 changes: 15 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:18-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY backend ./backend

WORKDIR /app/backend

EXPOSE 5000

CMD ["node", "server.js"]
12 changes: 12 additions & 0 deletions backend/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:18-alpine
WORKDIR /app

COPY package.json package-lock.json ./
RUN npm ci --production --silent

COPY . .

ENV PORT=5000
EXPOSE 5000

CMD ["node", "backend/server.js"]
13 changes: 13 additions & 0 deletions backend/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Build and run the backend image

Build:
```sh
docker build -t notes-backend:latest -f docker/Dockerfile .
```

Run (example with envs):
```sh
docker run -e MONGO_URI="mongodb+srv://username:password@notesapp-cluster.mongodb.net/?appName=notesapp-cluster" -e JWT_SECRET="abc123" -p 5000:5000 notes-backend:latest
```

If you use Docker Compose, set the `MONGO_URI` env in the service or rely on a linked mongo service.
14 changes: 4 additions & 10 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path');
const express = require('express');
const colors = require('colors');
const dotenv = require('dotenv').config();
const dotenv = require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
const { errorHandler } = require('./middleware/errorMiddleware');
const connectDB = require('./config/db');
const port = process.env.PORT || 5000;
Expand All @@ -18,15 +18,9 @@ app.use('/api/users', require('./routes/userRoutes'));

// Serve frontend
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, '../frontend/build')));

app.get('*', (req, res) =>
res.sendFile(
path.resolve(__dirname, '../', 'frontend', 'build', 'index.html')
)
);
} else {
app.get('/', (req, res) => res.send('Please set to production'));
app.get('/', (req, res) => {
res.send('API is running...')
})
Comment on lines +21 to +23
}

app.use(errorHandler);
Expand Down
3 changes: 3 additions & 0 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
build
.git
1 change: 1 addition & 0 deletions frontend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_API_URL=http://localhost:5000
35 changes: 35 additions & 0 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# FROM node:18-alpine AS build
# WORKDIR /app
# COPY package*.json ./
# RUN npm install
# COPY . .
# RUN npm run build
# FROM nginx:alpine
# COPY --from=build /app/build /usr/share/nginx/html
# COPY nginx.conf /etc/nginx/conf.d/default.conf
# COPY docker/entrypoint.sh /entrypoint.sh
# RUN chmod +x /entrypoint.sh
# EXPOSE 80
# ENTRYPOINT ["/entrypoint.sh"]
# CMD ["nginx", "-g", "daemon off;"]
FROM node:18-alpine AS build

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

RUN npm run build

FROM nginx:alpine

COPY --from=build /app/build /usr/share/nginx/html

COPY nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
17 changes: 17 additions & 0 deletions frontend/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Runtime env injection for frontend Docker image

Usage (build):

```sh
docker build -t my-frontend:latest --build-arg REACT_APP_API_URL=http://api:5000 -f docker/Dockerfile .
```

Runtime (override value without rebuilding):

```sh
docker run -e REACT_APP_API_URL=http://api:5000 -p 80:80 my-frontend:latest
```

Notes:
- `entrypoint.sh` writes `/env.js` into the served files so the app can read `window._env_.REACT_APP_API_URL` at runtime.
- The frontend will prefer the runtime `window._env_.REACT_APP_API_URL` over the build-time `process.env.REACT_APP_API_URL`.
19 changes: 19 additions & 0 deletions frontend/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
server {
listen 80;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}

location /api/ {
proxy_pass https://notes-backend-app.azurewebsites.net;
proxy_http_version 1.1;
proxy_set_header Host notes-backend-app.azurewebsites.net;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_ssl_server_name on;
}
}
Loading