1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-08 02:42:44 +00:00

fix(docker): docker-compose for prod+development

This commit is contained in:
Saad Jutt
2021-11-15 00:52:29 +05:00
parent 5e7cecf3ea
commit 4a363c5b97
12 changed files with 93 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
**/.classpath
**/.dockerignore
**/.env
!.env
**/.git
**/.gitignore
**/.project
@@ -20,4 +21,12 @@
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
api/build
api/coverage
api/build
api/node_modules
api/public
api/web
web/build
web/node_modules
README.md

5
.env.example Normal file
View File

@@ -0,0 +1,5 @@
PORT=<port for sasjs server (api)>
PORT_WEB=<port for sasjs web component(react)>
ACCESS_TOKEN_SECRET=<secret>
REFRESH_TOKEN_SECRET=<secret>
AUTH_CODE_SECRET=<secret>

10
DockerfileProd Normal file
View File

@@ -0,0 +1,10 @@
FROM node:lts-alpine
WORKDIR /usr/server/
COPY . .
RUN cd web && npm ci --silent
RUN cd web && REACT_APP_CLIENT_ID=clientID1 npm run build
RUN cd api && npm ci --silent
# RUN chown -R node /usr/server/api
# USER node
WORKDIR /usr/server/api
CMD ["npm","run","start"]

View File

@@ -1,5 +1,6 @@
MODE=[server]
CORS=[enable]
PORT_WEB=[port for sasjs web component(react)]
ACCESS_TOKEN_SECRET=<secret>
REFRESH_TOKEN_SECRET=<secret>
AUTH_CODE_SECRET=<secret>

View File

@@ -3,7 +3,6 @@ WORKDIR /usr/server/api
COPY ["package.json","package-lock.json", "./"]
RUN npm ci
COPY . .
EXPOSE 5000
# RUN chown -R node /usr/server/api
# USER node
CMD ["npm","start"]

View File

@@ -13,10 +13,12 @@ dotenv.config()
const app = express()
const { MODE, CORS } = process.env
const { MODE, CORS, PORT_WEB } = process.env
if (MODE?.trim() !== 'server' || CORS?.trim() === 'enable') {
console.log('All CORS Requests are enabled')
app.use(cors({ credentials: true, origin: 'http://localhost:3000' }))
app.use(
cors({ credentials: true, origin: `http://localhost:${PORT_WEB ?? 3000}` })
)
}
app.use(express.json({ limit: '50mb' }))

40
docker-compose.prod.yml Normal file
View File

@@ -0,0 +1,40 @@
version: '3.4'
services:
sasjs_server_prod:
image: sasjs_server_prod
build:
context: .
dockerfile: DockerfileProd
environment:
MODE: server
CORS: disable
PORT: ${PORT_API}
ACCESS_TOKEN_SECRET: ${ACCESS_TOKEN_SECRET}
REFRESH_TOKEN_SECRET: ${REFRESH_TOKEN_SECRET}
AUTH_CODE_SECRET: ${AUTH_CODE_SECRET}
DB_CONNECT: mongodb://mongodb:27017/sasjs
expose:
- ${PORT_API}
ports:
- ${PORT_API}:${PORT_API}
links:
- mongodb
mongodb:
image: mongo:latest
ports:
- 27017:27017
volumes:
- data:/data/db
mongo-seed-users:
build: ./mongo-seed/users
links:
- mongodb
mongo-seed-clients:
build: ./mongo-seed/clients
links:
- mongodb
volumes:
data:

View File

@@ -7,10 +7,16 @@ services:
environment:
MODE: server
CORS: enable
PORT: 5000
PORT: ${PORT_API}
PORT_WEB: ${PORT_WEB}
ACCESS_TOKEN_SECRET: ${ACCESS_TOKEN_SECRET}
REFRESH_TOKEN_SECRET: ${REFRESH_TOKEN_SECRET}
AUTH_CODE_SECRET: ${AUTH_CODE_SECRET}
DB_CONNECT: mongodb://mongodb:27017/sasjs
expose:
- ${PORT_API}
ports:
- 5000:5000
- ${PORT_API}:${PORT_API}
volumes:
- ./api:/usr/server/api
links:
@@ -19,8 +25,13 @@ services:
sasjs_server_web:
image: sasjs_server_web
build: ./web
environment:
REACT_APP_PORT_API: ${PORT_API}
PORT: ${PORT_WEB}
expose:
- ${PORT_WEB}
ports:
- 3000:3000
- ${PORT_WEB}:${PORT_WEB}
volumes:
- ./web:/usr/server/web

View File

@@ -1 +1,2 @@
REACT_APP_PORT_API=[place sasjs server port]
REACT_APP_CLIENT_ID=<place clientId here>

View File

@@ -3,7 +3,6 @@ WORKDIR /usr/server/web
COPY ["package.json","package-lock.json", "./"]
RUN npm ci
COPY . .
EXPOSE 3000
# RUN chown -R node /usr/server/api
# USER node
CMD ["npm","start"]

View File

@@ -7,8 +7,12 @@ const headers = {
Accept: 'application/json',
'Content-Type': 'application/json'
}
const { NODE_ENV, REACT_APP_PORT_API } = process.env
const baseUrl =
process.env.NODE_ENV === 'development' ? 'http://localhost:5000' : undefined
NODE_ENV === 'development'
? `http://localhost:${REACT_APP_PORT_API ?? 5000}`
: ''
const getAuthCode = async (credentials: any) => {
return fetch(`${baseUrl}/SASjsApi/auth/authorize`, {
method: 'POST',

View File

@@ -43,8 +43,11 @@ export default function useTokens() {
}
}
const { NODE_ENV, REACT_APP_PORT_API } = process.env
const baseUrl =
process.env.NODE_ENV === 'development' ? 'http://localhost:5000' : undefined
NODE_ENV === 'development'
? `http://localhost:${REACT_APP_PORT_API ?? 5000}`
: ''
const isAbsoluteURLRegex = /^(?:\w+:)\/\//