From 556bb979e19ccd4360f890b1f3bbe117932bcf0f Mon Sep 17 00:00:00 2001 From: guang Date: Sat, 24 Jun 2023 22:25:06 +0800 Subject: [PATCH] feat: dockerfile -test --- README.md | 1 + dockerfile-test/.eslintrc.js | 25 ++++++++ dockerfile-test/.gitignore | 35 +++++++++++ dockerfile-test/.prettierrc | 4 ++ dockerfile-test/222.Dockerfile | 25 ++++++++ dockerfile-test/333.Dockerfile | 13 ++++ dockerfile-test/444.Dockerfile | 5 ++ dockerfile-test/555.Dockerfile | 5 ++ dockerfile-test/Dockerfile | 17 +++++ dockerfile-test/README.md | 1 + dockerfile-test/aaa.tar.gz | Bin 0 -> 168 bytes dockerfile-test/aaa/111 | 1 + dockerfile-test/aaa/222 | 1 + dockerfile-test/nest-cli.json | 8 +++ dockerfile-test/package.json | 70 +++++++++++++++++++++ dockerfile-test/src/app.controller.spec.ts | 22 +++++++ dockerfile-test/src/app.controller.ts | 12 ++++ dockerfile-test/src/app.module.ts | 10 +++ dockerfile-test/src/app.service.ts | 8 +++ dockerfile-test/src/main.ts | 8 +++ dockerfile-test/test.js | 2 + dockerfile-test/test/app.e2e-spec.ts | 24 +++++++ dockerfile-test/test/jest-e2e.json | 9 +++ dockerfile-test/tsconfig.build.json | 4 ++ dockerfile-test/tsconfig.json | 21 +++++++ 25 files changed, 331 insertions(+) create mode 100644 dockerfile-test/.eslintrc.js create mode 100644 dockerfile-test/.gitignore create mode 100644 dockerfile-test/.prettierrc create mode 100644 dockerfile-test/222.Dockerfile create mode 100644 dockerfile-test/333.Dockerfile create mode 100644 dockerfile-test/444.Dockerfile create mode 100644 dockerfile-test/555.Dockerfile create mode 100644 dockerfile-test/Dockerfile create mode 100644 dockerfile-test/README.md create mode 100644 dockerfile-test/aaa.tar.gz create mode 100644 dockerfile-test/aaa/111 create mode 100644 dockerfile-test/aaa/222 create mode 100644 dockerfile-test/nest-cli.json create mode 100644 dockerfile-test/package.json create mode 100644 dockerfile-test/src/app.controller.spec.ts create mode 100644 dockerfile-test/src/app.controller.ts create mode 100644 dockerfile-test/src/app.module.ts create mode 100644 dockerfile-test/src/app.service.ts create mode 100644 dockerfile-test/src/main.ts create mode 100644 dockerfile-test/test.js create mode 100644 dockerfile-test/test/app.e2e-spec.ts create mode 100644 dockerfile-test/test/jest-e2e.json create mode 100644 dockerfile-test/tsconfig.build.json create mode 100644 dockerfile-test/tsconfig.json diff --git a/README.md b/README.md index ca263d1..b1847d9 100644 --- a/README.md +++ b/README.md @@ -68,3 +68,4 @@ [使用 docker-compose 部署 redis、mysql、nest](./docker-compose-test) +[dockerfile 的技巧](./dockerfile-test) \ No newline at end of file diff --git a/dockerfile-test/.eslintrc.js b/dockerfile-test/.eslintrc.js new file mode 100644 index 0000000..259de13 --- /dev/null +++ b/dockerfile-test/.eslintrc.js @@ -0,0 +1,25 @@ +module.exports = { + parser: '@typescript-eslint/parser', + parserOptions: { + project: 'tsconfig.json', + tsconfigRootDir: __dirname, + sourceType: 'module', + }, + plugins: ['@typescript-eslint/eslint-plugin'], + extends: [ + 'plugin:@typescript-eslint/recommended', + 'plugin:prettier/recommended', + ], + root: true, + env: { + node: true, + jest: true, + }, + ignorePatterns: ['.eslintrc.js'], + rules: { + '@typescript-eslint/interface-name-prefix': 'off', + '@typescript-eslint/explicit-function-return-type': 'off', + '@typescript-eslint/explicit-module-boundary-types': 'off', + '@typescript-eslint/no-explicit-any': 'off', + }, +}; diff --git a/dockerfile-test/.gitignore b/dockerfile-test/.gitignore new file mode 100644 index 0000000..22f55ad --- /dev/null +++ b/dockerfile-test/.gitignore @@ -0,0 +1,35 @@ +# compiled output +/dist +/node_modules + +# Logs +logs +*.log +npm-debug.log* +pnpm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# OS +.DS_Store + +# Tests +/coverage +/.nyc_output + +# IDEs and editors +/.idea +.project +.classpath +.c9/ +*.launch +.settings/ +*.sublime-workspace + +# IDE - VSCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json \ No newline at end of file diff --git a/dockerfile-test/.prettierrc b/dockerfile-test/.prettierrc new file mode 100644 index 0000000..dcb7279 --- /dev/null +++ b/dockerfile-test/.prettierrc @@ -0,0 +1,4 @@ +{ + "singleQuote": true, + "trailingComma": "all" +} \ No newline at end of file diff --git a/dockerfile-test/222.Dockerfile b/dockerfile-test/222.Dockerfile new file mode 100644 index 0000000..b815259 --- /dev/null +++ b/dockerfile-test/222.Dockerfile @@ -0,0 +1,25 @@ +FROM node:18-alpine3.14 as build-stage + +WORKDIR /app + +COPY package.json . + +RUN npm install + +COPY . . + +RUN npm run build + +# production stage +FROM node:18-alpine3.14 as production-stage + +COPY --from=build-stage /app/dist /app +COPY --from=build-stage /app/package.json /app/package.json + +WORKDIR /app + +RUN npm install --production + +EXPOSE 3000 + +CMD ["node", "/app/main.js"] \ No newline at end of file diff --git a/dockerfile-test/333.Dockerfile b/dockerfile-test/333.Dockerfile new file mode 100644 index 0000000..fdbe9c9 --- /dev/null +++ b/dockerfile-test/333.Dockerfile @@ -0,0 +1,13 @@ +FROM node:18-alpine3.14 + +ARG aaa +ARG bbb + +WORKDIR /app + +COPY ./test.js . + +ENV aaa=${aaa} \ + bbb=${bbb} + +CMD ["node", "/app/test.js"] diff --git a/dockerfile-test/444.Dockerfile b/dockerfile-test/444.Dockerfile new file mode 100644 index 0000000..5c2e3af --- /dev/null +++ b/dockerfile-test/444.Dockerfile @@ -0,0 +1,5 @@ +FROM node:18-alpine3.14 + +ENTRYPOINT ["echo", "光光"] + +CMD ["到此一游"] \ No newline at end of file diff --git a/dockerfile-test/555.Dockerfile b/dockerfile-test/555.Dockerfile new file mode 100644 index 0000000..8bcfb7a --- /dev/null +++ b/dockerfile-test/555.Dockerfile @@ -0,0 +1,5 @@ +FROM node:18-alpine3.14 + +ADD ./aaa.tar.gz /aaa + +COPY ./aaa.tar.gz /bbb diff --git a/dockerfile-test/Dockerfile b/dockerfile-test/Dockerfile new file mode 100644 index 0000000..1e2c397 --- /dev/null +++ b/dockerfile-test/Dockerfile @@ -0,0 +1,17 @@ +FROM node:18-alpine3.14 + +WORKDIR /app + +COPY package.json . + +RUN npm config set registry https://registry.npmmirror.com/ + +RUN npm install + +COPY . . + +RUN npm run build + +EXPOSE 3000 + +ENTRYPOINT [ "node", "./dist/main.js" ] \ No newline at end of file diff --git a/dockerfile-test/README.md b/dockerfile-test/README.md new file mode 100644 index 0000000..b6fc4c6 --- /dev/null +++ b/dockerfile-test/README.md @@ -0,0 +1 @@ +hello \ No newline at end of file diff --git a/dockerfile-test/aaa.tar.gz b/dockerfile-test/aaa.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..a485f2ccadf9032e054cd0a79613562c3fa4acca GIT binary patch literal 168 zcmV;Z09XGXiwFRu^_FA+1MQSC4#F@DMRSy#ptX(ToX1PMF#x?kp{@#3m|PX%xBMZN z{FASrVN}&IxdH&Sl)S2#0 { + let appController: AppController; + + beforeEach(async () => { + const app: TestingModule = await Test.createTestingModule({ + controllers: [AppController], + providers: [AppService], + }).compile(); + + appController = app.get(AppController); + }); + + describe('root', () => { + it('should return "Hello World!"', () => { + expect(appController.getHello()).toBe('Hello World!'); + }); + }); +}); diff --git a/dockerfile-test/src/app.controller.ts b/dockerfile-test/src/app.controller.ts new file mode 100644 index 0000000..cce879e --- /dev/null +++ b/dockerfile-test/src/app.controller.ts @@ -0,0 +1,12 @@ +import { Controller, Get } from '@nestjs/common'; +import { AppService } from './app.service'; + +@Controller() +export class AppController { + constructor(private readonly appService: AppService) {} + + @Get() + getHello(): string { + return this.appService.getHello(); + } +} diff --git a/dockerfile-test/src/app.module.ts b/dockerfile-test/src/app.module.ts new file mode 100644 index 0000000..8662803 --- /dev/null +++ b/dockerfile-test/src/app.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { AppController } from './app.controller'; +import { AppService } from './app.service'; + +@Module({ + imports: [], + controllers: [AppController], + providers: [AppService], +}) +export class AppModule {} diff --git a/dockerfile-test/src/app.service.ts b/dockerfile-test/src/app.service.ts new file mode 100644 index 0000000..927d7cc --- /dev/null +++ b/dockerfile-test/src/app.service.ts @@ -0,0 +1,8 @@ +import { Injectable } from '@nestjs/common'; + +@Injectable() +export class AppService { + getHello(): string { + return 'Hello World!'; + } +} diff --git a/dockerfile-test/src/main.ts b/dockerfile-test/src/main.ts new file mode 100644 index 0000000..13cad38 --- /dev/null +++ b/dockerfile-test/src/main.ts @@ -0,0 +1,8 @@ +import { NestFactory } from '@nestjs/core'; +import { AppModule } from './app.module'; + +async function bootstrap() { + const app = await NestFactory.create(AppModule); + await app.listen(3000); +} +bootstrap(); diff --git a/dockerfile-test/test.js b/dockerfile-test/test.js new file mode 100644 index 0000000..dc84f81 --- /dev/null +++ b/dockerfile-test/test.js @@ -0,0 +1,2 @@ +console.log(process.env.aaa); +console.log(process.env.bbb); diff --git a/dockerfile-test/test/app.e2e-spec.ts b/dockerfile-test/test/app.e2e-spec.ts new file mode 100644 index 0000000..50cda62 --- /dev/null +++ b/dockerfile-test/test/app.e2e-spec.ts @@ -0,0 +1,24 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { INestApplication } from '@nestjs/common'; +import * as request from 'supertest'; +import { AppModule } from './../src/app.module'; + +describe('AppController (e2e)', () => { + let app: INestApplication; + + beforeEach(async () => { + const moduleFixture: TestingModule = await Test.createTestingModule({ + imports: [AppModule], + }).compile(); + + app = moduleFixture.createNestApplication(); + await app.init(); + }); + + it('/ (GET)', () => { + return request(app.getHttpServer()) + .get('/') + .expect(200) + .expect('Hello World!'); + }); +}); diff --git a/dockerfile-test/test/jest-e2e.json b/dockerfile-test/test/jest-e2e.json new file mode 100644 index 0000000..e9d912f --- /dev/null +++ b/dockerfile-test/test/jest-e2e.json @@ -0,0 +1,9 @@ +{ + "moduleFileExtensions": ["js", "json", "ts"], + "rootDir": ".", + "testEnvironment": "node", + "testRegex": ".e2e-spec.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + } +} diff --git a/dockerfile-test/tsconfig.build.json b/dockerfile-test/tsconfig.build.json new file mode 100644 index 0000000..64f86c6 --- /dev/null +++ b/dockerfile-test/tsconfig.build.json @@ -0,0 +1,4 @@ +{ + "extends": "./tsconfig.json", + "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] +} diff --git a/dockerfile-test/tsconfig.json b/dockerfile-test/tsconfig.json new file mode 100644 index 0000000..95f5641 --- /dev/null +++ b/dockerfile-test/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "module": "commonjs", + "declaration": true, + "removeComments": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "allowSyntheticDefaultImports": true, + "target": "ES2021", + "sourceMap": true, + "outDir": "./dist", + "baseUrl": "./", + "incremental": true, + "skipLibCheck": true, + "strictNullChecks": false, + "noImplicitAny": false, + "strictBindCallApply": false, + "forceConsistentCasingInFileNames": false, + "noFallthroughCasesInSwitch": false + } +}