浏览代码

init docker file

nameczz 3 年之前
父节点
当前提交
71a27ec55d
共有 11 个文件被更改,包括 135 次插入27 次删除
  1. 4 0
      .dockerignore
  2. 35 0
      Dockerfile
  3. 12 0
      client/config-overrides.js
  4. 6 5
      client/package.json
  5. 2 0
      client/public/.env
  6. 4 0
      client/public/env-config.js
  7. 29 0
      client/public/env.sh
  8. 23 21
      client/public/index.html
  9. 1 0
      server/package.json
  10. 12 1
      server/src/app.module.ts
  11. 7 0
      server/yarn.lock

+ 4 - 0
.dockerignore

@@ -0,0 +1,4 @@
+/client/node_modules
+/server/node_modules
+
+npm-debug.log

+ 35 - 0
Dockerfile

@@ -0,0 +1,35 @@
+# => Building container
+FROM mhart/alpine-node:14 as builder
+WORKDIR /app
+COPY . .
+
+# => Building Client
+WORKDIR /app/client
+RUN yarn install
+RUN yarn build
+# # => Building Server
+WORKDIR /app/server
+RUN yarn install
+ENV NODE_ENV production
+ENV PORT 80
+RUN yarn build
+
+# => Copy to Final container
+FROM mhart/alpine-node:14
+WORKDIR /app
+COPY --from=builder /app/server/dist /app/dist
+COPY --from=builder /app/server/build /app/build
+# COPY --from=builder /app/server/node_modules /app/node_modules
+COPY --from=builder /app/server/package.json /app/package.json
+COPY --from=builder /app/server/yarn.lock /app/yarn.lock
+
+# => Reinstall production dependencies and clean cache
+RUN yarn install --production && yarn cache clean
+# Add bash
+RUN apk add --no-cache bash
+
+# Make our shell script executable
+RUN chmod +x /app/build/env.sh
+
+# RUN echo -e window.__version="{\"version\":\""$VERSION"\"}" > /app/build/version.js
+CMD [ "/bin/bash", "-c", "/app/build/env.sh && yarn start:prod" ]

+ 12 - 0
client/config-overrides.js

@@ -0,0 +1,12 @@
+const path = require('path');
+// const darkTheme = require("@ant-design/dark-theme").default;
+
+module.exports = {
+  // The paths config to use when compiling your react app
+  //  for development or production.
+  paths: function (paths, env) {
+    // ...add your paths config
+    paths.appBuild = path.resolve(__dirname, '../server/build');
+    return paths;
+  },
+};

+ 6 - 5
client/package.json

@@ -18,6 +18,7 @@
     "dayjs": "^1.10.5",
     "i18next": "^20.3.1",
     "react": "^17.0.2",
+    "react-app-rewired": "^2.1.8",
     "react-dom": "^17.0.2",
     "react-i18next": "^11.10.0",
     "react-router-dom": "^5.2.0",
@@ -26,10 +27,10 @@
     "web-vitals": "^1.0.1"
   },
   "scripts": {
-    "start": "react-scripts start",
-    "build": "react-scripts build",
-    "test": "react-scripts test",
-    "eject": "react-scripts eject"
+    "start": "react-app-rewired start",
+    "build": "react-app-rewired build",
+    "test": "react-app-rewired test",
+    "eject": "react-app-rewired eject"
   },
   "eslintConfig": {
     "extends": [
@@ -49,4 +50,4 @@
       "last 1 safari version"
     ]
   }
-}
+}

+ 2 - 0
client/public/.env

@@ -0,0 +1,2 @@
+URL=http://
+API_URL=http://127.0.0.1:3000

+ 4 - 0
client/public/env-config.js

@@ -0,0 +1,4 @@
+window._env_ = {
+  URL: 'http://',
+  API_URL: 'http://127.0.0.1:9000',
+};

+ 29 - 0
client/public/env.sh

@@ -0,0 +1,29 @@
+#!/bin/bash
+
+# Recreate config file
+rm -rf ./build/env-config.js
+touch ./build/env-config.js
+
+# Add assignment 
+echo "window._env_ = {" >> ./build/env-config.js
+
+# Read each line in .env file
+# Each line represents key=value pairs
+while read -r line || [[ -n "$line" ]];
+do
+  # Split env variables by character `=`
+  if printf '%s\n' "$line" | grep -q -e '='; then
+    varname=$(printf '%s\n' "$line" | sed -e 's/=.*//')
+    varvalue=$(printf '%s\n' "$line" | sed -e 's/^[^=]*=//')
+  fi
+
+  # Read value of current variable if exists as Environment variable
+  value=$(printf '%s\n' "${!varname}")
+  # Otherwise use value from .env file
+  [[ -z $value ]] && value=${varvalue}
+  
+  # Append configuration property to JS file
+  echo "  $varname: \"$value\"," >> ./build/env-config.js
+done < ./build/.env
+
+echo "}" >> ./build/env-config.js

+ 23 - 21
client/public/index.html

@@ -1,21 +1,21 @@
 <!DOCTYPE html>
 <html lang="en">
-  <head>
-    <meta charset="utf-8" />
-    <link rel="icon" href="%PUBLIC_URL%/favicon.png" />
-    <meta name="viewport" content="width=device-width, initial-scale=1" />
-    <meta name="theme-color" content="#000000" />
-    <meta
-      name="description"
-      content="Web site created using create-react-app"
-    />
-    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
-    <!--
+
+<head>
+  <meta charset="utf-8" />
+  <link rel="icon" href="%PUBLIC_URL%/favicon.png" />
+  <meta name="viewport" content="width=device-width, initial-scale=1" />
+  <meta name="theme-color" content="#000000" />
+  <meta name="description" content="Web site created using create-react-app" />
+  <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
+  <!--
       manifest.json provides metadata used when your web app is installed on a
       user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
     -->
-    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
-    <!--
+  <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
+  <script src="%PUBLIC_URL%/env-config.js"></script>
+
+  <!--
       Notice the use of %PUBLIC_URL% in the tags above.
       It will be replaced with the URL of the `public` folder during the build.
       Only files inside the `public` folder can be referenced from the HTML.
@@ -24,12 +24,13 @@
       work correctly both with client-side routing and a non-root public URL.
       Learn how to configure a non-root public URL by running `npm run build`.
     -->
-    <title>Milvus Admin</title>
-  </head>
-  <body>
-    <noscript>You need to enable JavaScript to run this app.</noscript>
-    <div id="root"></div>
-    <!--
+  <title>Milvus Admin</title>
+</head>
+
+<body>
+  <noscript>You need to enable JavaScript to run this app.</noscript>
+  <div id="root"></div>
+  <!--
       This HTML file is a template.
       If you open it directly in the browser, you will see an empty page.
 
@@ -39,5 +40,6 @@
       To begin the development, run `npm start` or `yarn start`.
       To create a production bundle, use `npm run build` or `yarn build`.
     -->
-  </body>
-</html>
+</body>
+
+</html>

+ 1 - 0
server/package.json

@@ -26,6 +26,7 @@
     "@nestjs/jwt": "^7.2.0",
     "@nestjs/passport": "^7.1.5",
     "@nestjs/platform-express": "^7.6.15",
+    "@nestjs/serve-static": "^2.1.4",
     "@types/passport-jwt": "^3.0.5",
     "@types/passport-local": "^1.0.33",
     "class-transformer": "^0.4.0",

+ 12 - 1
server/src/app.module.ts

@@ -8,9 +8,20 @@ import { CollectionsModule } from './collections/collections.module';
 import { UsersService } from './users/users.service';
 import { UsersModule } from './users/users.module';
 import { AuthModule } from './auth/auth.module';
+import { ServeStaticModule } from '@nestjs/serve-static';
+import { join } from 'path';
 
 @Module({
-  imports: [MilvusModule, CollectionsModule, UsersModule, AuthModule],
+  imports: [
+    ServeStaticModule.forRoot({
+      rootPath: join(__dirname, '..', 'build'),
+      renderPath: '/', // only root render static html
+    }),
+    MilvusModule,
+    CollectionsModule,
+    UsersModule,
+    AuthModule,
+  ],
   controllers: [AppController],
   providers: [
     AppService,

+ 7 - 0
server/yarn.lock

@@ -642,6 +642,13 @@
     jsonc-parser "3.0.0"
     pluralize "8.0.0"
 
+"@nestjs/serve-static@^2.1.4":
+  version "2.1.4"
+  resolved "https://registry.yarnpkg.com/@nestjs/serve-static/-/serve-static-2.1.4.tgz#d25f7691b0cb19d3f12d161129dd1469dfdc880d"
+  integrity sha512-w2PpLKzQOB8rJ+vMOy28xm8jwE8VjJfA9U+KOm0H0OY62g2oOWJ+OQPSDogP7XxAzZwq+Bt8wNU2oS8+z6v6Zg==
+  dependencies:
+    path-to-regexp "0.1.7"
+
 "@nestjs/testing@^7.6.15":
   version "7.6.17"
   resolved "https://registry.yarnpkg.com/@nestjs/testing/-/testing-7.6.17.tgz#dab17527dbbc12c674b21de9527d280ee065932b"