示例项目软件准备:
SwitchHosts下载: https://github.com/oldj/SwitchHosts/releases/download/v4.0.3/SwitchHosts_installer_4.0.3.6070.exe
SkyWalking6.1下载:
https://archive.apache.org/dist/skywalking/6.1.0/
mysql5.7以及workBench下载(下这个installer然后选mysql以及workBench的安装)
https://downloads.mysql.com/archives/get/p/25/file/mysql-installer-community-5.7.34.0.msi

Docker

容器用途

  • 标准化打包
  • 隔离
  • 标准化部署

image.png

DockerFile

用于构建镜像, 在每个子模块的根目录下有对应的Dockerfile
Account服务示例

  1. FROM java:8-jdk-alpine
  2. COPY ./target/account-svc-1.0.0.jar /usr/app/
  3. WORKDIR /usr/app
  4. RUN sh -c 'touch account-svc-1.0.0.jar'
  5. ENTRYPOINT ["java", "-jar", "account-svc-1.0.0.jar"]

MyAccount单页应用示例

  1. FROM node:alpine as builder
  2. WORKDIR '/build'
  3. COPY myaccount ./myaccount
  4. COPY resources ./resources
  5. COPY third_party ./third_party
  6. WORKDIR '/build/myaccount'
  7. RUN npm install
  8. RUN npm rebuild node-sass
  9. RUN npm run build
  10. RUN ls /build/myaccount/dist
  11. FROM nginx
  12. EXPOSE 80
  13. COPY --from=builder /build/myaccount/dist /usr/share/nginx/html

Docker Compose

用于定义和运行多容器应用的工具
可以将多容器应用的部署架构规范在docker-compose.yml中, 可以一键启停整个应用

个人理解: 只能说在本地或单机环境搞这个, 生产环境不会所有服务都部署在一台机, 可能需要多个compose文件就没啥卵用了

image.png
Docker Compose示例
在项目根目录下有个.env文件存放环境变量

  1. SPRING_PROFILES_ACTIVE=test
  2. SERVER_PORT=80
  3. EMAIL_SERVICE_ENDPOINT=http://email-service
  4. COMPANY_SERVICE_ENDPOINT=http://company-service
  5. ACCOUNT_SERVICE_ENDPOINT=http://account-service
  6. BOT_SERVICE_ENDPOINT=http://bot-service
  7. SMS_SERVICE_ENDPOINT=http://sms-service
  8. SENTRY_DSN=https://80aaf4ae889b414f9fe72e3904cd5246@sentry.io/1380198
  9. SIGNING_SECRET=your_signing_secret
  10. INTERCOM_ACCESS_TOKEN=YOUR_INTERCOM_ACCESS_TOKEN
  11. INTERCOM_APP_ID=TBD
  12. INTERCOM_SIGNING_SECRET=TBD
  13. ALIYUN_ACCESS_KEY=YOUR_ALIYUN_ACCESS_KEY
  14. ALIYUN_ACCESS_SECRET=YOUR_ALIYUN_ACCESS_SECRET
  15. RECAPTCHA_PUBLIC=test-recaptcha-public
  16. RECAPTCHA_PRIVATE=test-recaptcha-private
  17. ACCOUNT_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/staffjoy_account?useUnicode=true&characterEncoding=utf-8
  18. ACCOUNT_DATASOURCE_USERNAME=root
  19. ACCOUNT_DATASOURCE_PASSWORD=root
  20. COMPANY_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/staffjoy_company?useUnicode=true&characterEncoding=utf-8
  21. COMPANY_DATASOURCE_USERNAME=root
  22. COMPANY_DATASOURCE_PASSWORD=root

根目录下有docker-compose.yml

  1. version: '3.7' #使用的docker-compose版本为3.7
  2. services:
  3. account-service:
  4. build: ./account-svc #使用每个子模块根目录下的Dockerfile来构建镜像
  5. image: boboweike/account-svc #镜像的名称
  6. environment: #环境变量,通过外部传入
  7. - SPRING_PROFILES_ACTIVE
  8. - SERVER_PORT
  9. - SIGNING_SECRET
  10. - SENTRY_DSN
  11. - EMAIL_SERVICE_ENDPOINT
  12. - COMPANY_SERVICE_ENDPOINT
  13. - BOT_SERVICE_ENDPOINT
  14. - INTERCOM_ACCESS_TOKEN
  15. - ACCOUNT_DATASOURCE_URL
  16. - ACCOUNT_DATASOURCE_USERNAME
  17. - ACCOUNT_DATASOURCE_PASSWORD
  18. depends_on: #指定服务之前的依赖关系, 会先启动bot, email, 再启动account-service
  19. - bot-service
  20. - email-service
  21. networks: #网络配置, 这个服务内部和外部都需可访问
  22. - internal_access #见最下方配置
  23. - external_access # db access
  24. company-service:
  25. build: ./company-svc
  26. image: boboweike/company-svc
  27. environment:
  28. - SPRING_PROFILES_ACTIVE
  29. - SERVER_PORT
  30. - SIGNING_SECRET
  31. - SENTRY_DSN
  32. - EMAIL_SERVICE_ENDPOINT
  33. - ACCOUNT_SERVICE_ENDPOINT
  34. - BOT_SERVICE_ENDPOINT
  35. - COMPANY_DATASOURCE_URL
  36. - COMPANY_DATASOURCE_USERNAME
  37. - COMPANY_DATASOURCE_PASSWORD
  38. depends_on:
  39. - bot-service
  40. - email-service
  41. networks:
  42. - internal_access
  43. - external_access # db access
  44. bot-service:
  45. build: ./bot-svc
  46. image: boboweike/bot-svc
  47. environment:
  48. - SPRING_PROFILES_ACTIVE
  49. - SERVER_PORT
  50. - SENTRY_DSN
  51. - EMAIL_SERVICE_ENDPOINT
  52. - ACCOUNT_SERVICE_ENDPOINT
  53. - COMPANY_SERVICE_ENDPOINT
  54. - SMS_SERVICE_ENDPOINT
  55. depends_on:
  56. - email-service
  57. # - sms-svc # commented for demo
  58. networks:
  59. - internal_access
  60. email-service:
  61. build: ./mail-svc
  62. image: boboweike/mail-svc
  63. environment:
  64. - SPRING_PROFILES_ACTIVE
  65. - SERVER_PORT
  66. - SENTRY_DSN
  67. - ALIYUN_ACCESS_KEY
  68. - ALIYUN_ACCESS_SECRET
  69. networks:
  70. - internal_access
  71. - external_access # aliyun access
  72. # commented for demo
  73. # sms-service:
  74. # build: ./sms-svc
  75. # image: sms-svc
  76. # environment:
  77. # - SPRING_PROFILES_ACTIVE=test
  78. whoami-service:
  79. build: ./whoami-svc
  80. image: boboweike/whoami-svc
  81. environment:
  82. - SPRING_PROFILES_ACTIVE
  83. - SERVER_PORT
  84. - SENTRY_DSN
  85. - INTERCOM_APP_ID
  86. - INTERCOM_SIGNING_SECRET
  87. - ACCOUNT_SERVICE_ENDPOINT
  88. - COMPANY_SERVICE_ENDPOINT
  89. depends_on:
  90. - account-service
  91. - company-service
  92. networks:
  93. - internal_access
  94. # commented for demo
  95. # ical-service:
  96. # build: ./ical-svc
  97. # image: ical-svc
  98. # environment:
  99. # - SPRING_PROFILES_ACTIVE=test
  100. # depends_on:
  101. # - company-service
  102. www-service:
  103. build: ./web-app
  104. image: boboweike/www-svc
  105. environment:
  106. - SPRING_PROFILES_ACTIVE
  107. - SERVER_PORT
  108. - SENTRY_DSN
  109. - SIGNING_SECRET
  110. - ACCOUNT_SERVICE_ENDPOINT
  111. - COMPANY_SERVICE_ENDPOINT
  112. - EMAIL_SERVICE_ENDPOINT
  113. - RECAPTCHA_PUBLIC
  114. - RECAPTCHA_PRIVATE
  115. depends_on:
  116. - account-service
  117. - company-service
  118. - email-service
  119. networks:
  120. - internal_access
  121. faraday-service:
  122. build: ./faraday
  123. image: boboweike/faraday-svc
  124. ports: #端口映射, 需要将容器环境的80端口映射到宿主机
  125. - 80:80
  126. environment:
  127. - SPRING_PROFILES_ACTIVE
  128. - SERVER_PORT
  129. - SENTRY_DSN
  130. - SIGNING_SECRET
  131. depends_on:
  132. - account-service
  133. - company-service
  134. - www-service
  135. - whoami-service
  136. # - ical-service # commented for demo
  137. - myaccount-service
  138. - app-service
  139. networks:
  140. - internal_access
  141. - external_access
  142. myaccount-service:
  143. build:
  144. context: ./frontend
  145. dockerfile: myaccount/Dockerfile
  146. image: boboweike/myaccount-spa
  147. networks:
  148. - internal_access
  149. app-service:
  150. build:
  151. context: ./frontend
  152. dockerfile: app/Dockerfile
  153. image: boboweike/app-spa
  154. networks:
  155. - internal_access
  156. networks: #定义网络
  157. internal_access: #内部访问
  158. internal: true
  159. external_access: #外部访问,比如db


1、docker-compose只能在一台宿主机上执行么?
2、假设宿主机内存为4G,有两个docker容器,每台都指定使用3G内存,那么这两个容器可以正常运行么?
3、docker容器对宿主机硬件资源(CPU,内存)的占用需要如何设置呢? 作者回复: 你好,回答你的问题:
1. docker-compose主要是在单机上玩的。但是现在v3版本的docker-compose.yml文件也支持在docker swarm集群上运行,要用docker stack命令,同时对yml文件也有一些限制,参考:https://stackoverflow.com/questions/40737389/docker-compose-deploying-service-in-multiple-hosts

  1. 在可用内存4g的宿主机上,可以同时运行两个指定3G内存的容器,只要两个容器实际使用的内存都不会超过3g(当然两者总和也不能超过4g,否则会被OS杀掉),你可以尝试下打开两个终端,同时运行:
    docker run -m 20G -it ubuntu bash

  2. CPU/MEM限制,具体可以参考docker官方文档,写得很详细:
    https://docs.docker.com/config/containers/resource_constraints/

部署本地

win安装Docker Desktop:
https://www.docker.com/products/docker-desktop
安装失败, 后续略