示例项目软件准备:
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
容器用途
- 标准化打包
- 隔离
- 标准化部署
DockerFile
用于构建镜像, 在每个子模块的根目录下有对应的Dockerfile
Account服务示例
FROM java:8-jdk-alpine
COPY ./target/account-svc-1.0.0.jar /usr/app/
WORKDIR /usr/app
RUN sh -c 'touch account-svc-1.0.0.jar'
ENTRYPOINT ["java", "-jar", "account-svc-1.0.0.jar"]
MyAccount单页应用示例
FROM node:alpine as builder
WORKDIR '/build'
COPY myaccount ./myaccount
COPY resources ./resources
COPY third_party ./third_party
WORKDIR '/build/myaccount'
RUN npm install
RUN npm rebuild node-sass
RUN npm run build
RUN ls /build/myaccount/dist
FROM nginx
EXPOSE 80
COPY --from=builder /build/myaccount/dist /usr/share/nginx/html
Docker Compose
用于定义和运行多容器应用的工具
可以将多容器应用的部署架构规范在docker-compose.yml中, 可以一键启停整个应用
个人理解: 只能说在本地或单机环境搞这个, 生产环境不会所有服务都部署在一台机, 可能需要多个compose文件就没啥卵用了
Docker Compose示例
在项目根目录下有个.env文件存放环境变量
SPRING_PROFILES_ACTIVE=test
SERVER_PORT=80
EMAIL_SERVICE_ENDPOINT=http://email-service
COMPANY_SERVICE_ENDPOINT=http://company-service
ACCOUNT_SERVICE_ENDPOINT=http://account-service
BOT_SERVICE_ENDPOINT=http://bot-service
SMS_SERVICE_ENDPOINT=http://sms-service
SENTRY_DSN=https://80aaf4ae889b414f9fe72e3904cd5246@sentry.io/1380198
SIGNING_SECRET=your_signing_secret
INTERCOM_ACCESS_TOKEN=YOUR_INTERCOM_ACCESS_TOKEN
INTERCOM_APP_ID=TBD
INTERCOM_SIGNING_SECRET=TBD
ALIYUN_ACCESS_KEY=YOUR_ALIYUN_ACCESS_KEY
ALIYUN_ACCESS_SECRET=YOUR_ALIYUN_ACCESS_SECRET
RECAPTCHA_PUBLIC=test-recaptcha-public
RECAPTCHA_PRIVATE=test-recaptcha-private
ACCOUNT_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/staffjoy_account?useUnicode=true&characterEncoding=utf-8
ACCOUNT_DATASOURCE_USERNAME=root
ACCOUNT_DATASOURCE_PASSWORD=root
COMPANY_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/staffjoy_company?useUnicode=true&characterEncoding=utf-8
COMPANY_DATASOURCE_USERNAME=root
COMPANY_DATASOURCE_PASSWORD=root
根目录下有docker-compose.yml
version: '3.7' #使用的docker-compose版本为3.7
services:
account-service:
build: ./account-svc #使用每个子模块根目录下的Dockerfile来构建镜像
image: boboweike/account-svc #镜像的名称
environment: #环境变量,通过外部传入
- SPRING_PROFILES_ACTIVE
- SERVER_PORT
- SIGNING_SECRET
- SENTRY_DSN
- EMAIL_SERVICE_ENDPOINT
- COMPANY_SERVICE_ENDPOINT
- BOT_SERVICE_ENDPOINT
- INTERCOM_ACCESS_TOKEN
- ACCOUNT_DATASOURCE_URL
- ACCOUNT_DATASOURCE_USERNAME
- ACCOUNT_DATASOURCE_PASSWORD
depends_on: #指定服务之前的依赖关系, 会先启动bot, email, 再启动account-service
- bot-service
- email-service
networks: #网络配置, 这个服务内部和外部都需可访问
- internal_access #见最下方配置
- external_access # db access
company-service:
build: ./company-svc
image: boboweike/company-svc
environment:
- SPRING_PROFILES_ACTIVE
- SERVER_PORT
- SIGNING_SECRET
- SENTRY_DSN
- EMAIL_SERVICE_ENDPOINT
- ACCOUNT_SERVICE_ENDPOINT
- BOT_SERVICE_ENDPOINT
- COMPANY_DATASOURCE_URL
- COMPANY_DATASOURCE_USERNAME
- COMPANY_DATASOURCE_PASSWORD
depends_on:
- bot-service
- email-service
networks:
- internal_access
- external_access # db access
bot-service:
build: ./bot-svc
image: boboweike/bot-svc
environment:
- SPRING_PROFILES_ACTIVE
- SERVER_PORT
- SENTRY_DSN
- EMAIL_SERVICE_ENDPOINT
- ACCOUNT_SERVICE_ENDPOINT
- COMPANY_SERVICE_ENDPOINT
- SMS_SERVICE_ENDPOINT
depends_on:
- email-service
# - sms-svc # commented for demo
networks:
- internal_access
email-service:
build: ./mail-svc
image: boboweike/mail-svc
environment:
- SPRING_PROFILES_ACTIVE
- SERVER_PORT
- SENTRY_DSN
- ALIYUN_ACCESS_KEY
- ALIYUN_ACCESS_SECRET
networks:
- internal_access
- external_access # aliyun access
# commented for demo
# sms-service:
# build: ./sms-svc
# image: sms-svc
# environment:
# - SPRING_PROFILES_ACTIVE=test
whoami-service:
build: ./whoami-svc
image: boboweike/whoami-svc
environment:
- SPRING_PROFILES_ACTIVE
- SERVER_PORT
- SENTRY_DSN
- INTERCOM_APP_ID
- INTERCOM_SIGNING_SECRET
- ACCOUNT_SERVICE_ENDPOINT
- COMPANY_SERVICE_ENDPOINT
depends_on:
- account-service
- company-service
networks:
- internal_access
# commented for demo
# ical-service:
# build: ./ical-svc
# image: ical-svc
# environment:
# - SPRING_PROFILES_ACTIVE=test
# depends_on:
# - company-service
www-service:
build: ./web-app
image: boboweike/www-svc
environment:
- SPRING_PROFILES_ACTIVE
- SERVER_PORT
- SENTRY_DSN
- SIGNING_SECRET
- ACCOUNT_SERVICE_ENDPOINT
- COMPANY_SERVICE_ENDPOINT
- EMAIL_SERVICE_ENDPOINT
- RECAPTCHA_PUBLIC
- RECAPTCHA_PRIVATE
depends_on:
- account-service
- company-service
- email-service
networks:
- internal_access
faraday-service:
build: ./faraday
image: boboweike/faraday-svc
ports: #端口映射, 需要将容器环境的80端口映射到宿主机
- 80:80
environment:
- SPRING_PROFILES_ACTIVE
- SERVER_PORT
- SENTRY_DSN
- SIGNING_SECRET
depends_on:
- account-service
- company-service
- www-service
- whoami-service
# - ical-service # commented for demo
- myaccount-service
- app-service
networks:
- internal_access
- external_access
myaccount-service:
build:
context: ./frontend
dockerfile: myaccount/Dockerfile
image: boboweike/myaccount-spa
networks:
- internal_access
app-service:
build:
context: ./frontend
dockerfile: app/Dockerfile
image: boboweike/app-spa
networks:
- internal_access
networks: #定义网络
internal_access: #内部访问
internal: true
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
在可用内存4g的宿主机上,可以同时运行两个指定3G内存的容器,只要两个容器实际使用的内存都不会超过3g(当然两者总和也不能超过4g,否则会被OS杀掉),你可以尝试下打开两个终端,同时运行:
docker run -m 20G -it ubuntu bashCPU/MEM限制,具体可以参考docker官方文档,写得很详细:
https://docs.docker.com/config/containers/resource_constraints/
部署本地
win安装Docker Desktop:
https://www.docker.com/products/docker-desktop
安装失败, 后续略