From 4a363c5b9796283199debcc8afa810c6f561f8e6 Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Mon, 15 Nov 2021 00:52:29 +0500 Subject: [PATCH] fix(docker): docker-compose for prod+development --- .dockerignore | 9 ++++++++ .env.example | 5 +++++ DockerfileProd | 10 +++++++++ api/.env.example | 1 + api/Dockerfile | 1 - api/src/app.ts | 6 +++-- docker-compose.prod.yml | 40 +++++++++++++++++++++++++++++++++ docker-compose.yml | 17 +++++++++++--- web/.env.example | 1 + web/Dockerfile | 1 - web/src/components/login.tsx | 6 ++++- web/src/components/useTokens.ts | 5 ++++- 12 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 .env.example create mode 100644 DockerfileProd create mode 100644 docker-compose.prod.yml diff --git a/.dockerignore b/.dockerignore index a59a775..3e15b46 100644 --- a/.dockerignore +++ b/.dockerignore @@ -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 diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..f9b5cb7 --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +PORT= +PORT_WEB= +ACCESS_TOKEN_SECRET= +REFRESH_TOKEN_SECRET= +AUTH_CODE_SECRET= \ No newline at end of file diff --git a/DockerfileProd b/DockerfileProd new file mode 100644 index 0000000..b41b3b1 --- /dev/null +++ b/DockerfileProd @@ -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"] diff --git a/api/.env.example b/api/.env.example index 845c2d6..ef835a5 100644 --- a/api/.env.example +++ b/api/.env.example @@ -1,5 +1,6 @@ MODE=[server] CORS=[enable] +PORT_WEB=[port for sasjs web component(react)] ACCESS_TOKEN_SECRET= REFRESH_TOKEN_SECRET= AUTH_CODE_SECRET= diff --git a/api/Dockerfile b/api/Dockerfile index cb1fe84..9311227 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -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"] diff --git a/api/src/app.ts b/api/src/app.ts index 7f1efc8..1b78f2f 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -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' })) diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..c809b32 --- /dev/null +++ b/docker-compose.prod.yml @@ -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: diff --git a/docker-compose.yml b/docker-compose.yml index 7964737..10c7fa6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/web/.env.example b/web/.env.example index 94ecfa6..ea0ff24 100644 --- a/web/.env.example +++ b/web/.env.example @@ -1 +1,2 @@ +REACT_APP_PORT_API=[place sasjs server port] REACT_APP_CLIENT_ID= \ No newline at end of file diff --git a/web/Dockerfile b/web/Dockerfile index 02df291..bda41a9 100644 --- a/web/Dockerfile +++ b/web/Dockerfile @@ -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"] diff --git a/web/src/components/login.tsx b/web/src/components/login.tsx index 0cb8a8e..83c99c2 100644 --- a/web/src/components/login.tsx +++ b/web/src/components/login.tsx @@ -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', diff --git a/web/src/components/useTokens.ts b/web/src/components/useTokens.ts index 82e2193..a9d0ccb 100644 --- a/web/src/components/useTokens.ts +++ b/web/src/components/useTokens.ts @@ -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+:)\/\//