1、准备条件:安装docker,安装docker-compose,docker安装可自行百度,docker-compose安装由于太慢,我这里提供两个版本:win和linux版本的百度网盘版,大家可根据需要自行下载:
链接:https://pan.baidu.com/s/10W81TX6cWQqyi92xyeuZQQ
提取码:2evg
这里一linux系统为例:下载docker-compose-linux-x86_64后上传到linux 的/usr/bin 下,然后使用 mv docker-compose-linux-x86_64 docker-compose 命名后,使用chmod +x docker-compose 赋予该文件执行权限,再复制一份到/usr/local/bin下,至此,你可以使用docker-compose --version 验证docker-compose是否安装ok
2、jar包准备:新建一个文件夹,如docker_images,将需要的jar 以 文件夹 + jar + 该jar 的配置文件的形式,如spot-contract/spot-contract.jar 、spot-contract/application.yml,之后编写统一的Dockerfile文件:
FROM openjdk:8 WORKDIR /javaxh_docker/jdk VOLUME /tmp ARG JAR_FILE=./*.jar ADD ${JAR_FILE} app.jar ENTRYPOINT ["java", "-Dfile.encoding=UTF8", "-Duser.timezone=GMT+08", "-jar", "app.jar"]
将该Dockerfile复制一份到 spot-contract下,形成如下目录结构
|-docker_images
|----spot-contract
|--------spot-contract.jar
|--------Dockerfile
|--------application.yml
|----spot-base
|--------spot-base.jar
|--------Dockerfile
|--------application.yml
|----docker-compose.yml
使用docker build -t spot-contract:latest spot-contract/ 构建镜像
其他服务遵循同样的方式
3.编写docker-compose.yml
version: '3.3' services: web-sichuan: image: nginx:latest networks: - sichuan-network environment: - TZ=Asia/Shanghai ports: - "18089:10881" volumes: - "./sichuan-web/sichuan/:/usr/share/nginx/html" - "./sichuan-web/conf:/etc/nginx/conf.d" hdpm-platform-sc: image: tomcat:sc networks: - sichuan-network environment: - TZ=Asia/Shanghai ports: - "19288:9090" volumes: - "./tomcat-sc/apache-tomcat-sc/webapps/:/javaxh_docker/jdk/apache-tomcat-sc/webapps/" - "./tomcat-sc/apache-tomcat-sc/conf/:/javaxh_docker/jdk/apache-tomcat-sc/conf" spot-public-data-sichuan: image: spot-public-data:sc networks: - sichuan-network environment: - TZ=Asia/Shanghai command: ["--spring.profiles.active=prod"] ports: - "19224:8224" volumes: - "./spot-public-data-sichuan/application.yml:/javaxh_docker/jdk/config/application-prod.yml" eureka-server: image: eureka:common networks: - sichuan-network - fujian-network - mengxi-network environment: - TZ=Asia/Shanghai command: [ "--spring.profiles.active=prod" ] ports: - "18761:8761" volumes: - "./eureka/application-common.yml:/javaxh_docker/jdk/config/application-prod.yml" gateway: image: gateway:common networks: - sichuan-network - fujian-network - mengxi-network environment: - TZ=Asia/Shanghai command: [ "--spring.profiles.active=prod" ] ports: - "18769:8769" volumes: - "./gateway/application.yml:/javaxh_docker/jdk/config/application-prod.yml" process-common: image: hdpm-process:common networks: - sichuan-network - fujian-network - mengxi-network environment: - TZ=Asia/Shanghai command: [ "--spring.profiles.active=prod" ] ports: - "18024:8024" volumes: - "./process/mx/application.yml:/javaxh_docker/jdk/config/application-prod.yml" spot-trading-result-sichuan: image: spot-trading-result:sc networks: - sichuan-network - fujian-network - mengxi-network environment: - TZ=Asia/Shanghai command: [ "--spring.profiles.active=prod" ] ports: - "18191:8191" volumes: - "./spot-trading-result/spot-trading-result-sichuan/application.yml:/javaxh_docker/jdk/config/application-prod.yml" configserver: image: configserver:common networks: - sichuan-network - fujian-network - mengxi-network environment: - TZ=Asia/Shanghai command: [ "--spring.profiles.active=prod" ] ports: - "18090:8090" volumes: - "./configserver/application.yml:/javaxh_docker/jdk/config/application-prod.yml" spot-day-ahead: image: spot-day-ahead:common networks: - sichuan-network - fujian-network - mengxi-network environment: - TZ=Asia/Shanghai command: [ "--spring.profiles.active=prod" ] ports: - "18084:8084" volumes: - "./spot-day-ahead/application.yml:/javaxh_docker/jdk/config/application-prod.yml" spot-contract: image: spot-contract:common networks: - sichuan-network - fujian-network - mengxi-network environment: - TZ=Asia/Shanghai command: [ "--spring.profiles.active=prod" ] ports: - "18755:8755" volumes: - "./spot-contract/application.yml:/javaxh_docker/jdk/config/application-prod.yml" spot-base-data: image: spot-base-data:common networks: - sichuan-network - fujian-network - mengxi-network environment: - TZ=Asia/Shanghai command: [ "--spring.profiles.active=prod" ] ports: - "18755:8755" volumes: - "./spot-base-data/application.yml:/javaxh_docker/jdk/config/application-prod.yml" networks: sichuan-network: { } fujian-network: { } mengxi-network: { }
4、服务的启动与关闭
docker-compose -f docker-compose.yml up -d 启动所有服务
docker-compose -f docker-compose.yml down 关闭所有服务
docker-compose -f docker-compose.yml up -d spot-base 只启动spot-base服务
docker-compose -f docker-compose.yml restart spot-base 重启spot-base服务
docker-compose -f docker-compose.yml start spot-base 启动spot-base服务(需要先执行过up操作)
docker-compose -f docker-compose.yml stop spot-base 停止spot-base服务
5、关于服务配置文件
eureka: client: serviceUrl: defaultZone: http://eureka-server:8761/eureka/ healthcheck: enabled: true # 开启健康检查(需要spring-boot-starter-actuator依赖),配置项必须设置在application.yml中 instance: lease-renewal-interval-in-seconds: 30 # 续约更新时间间隔(默认30秒) lease-expiration-duration-in-seconds: 90 # 续约到期时间(默认90秒)
由于使用的是docker容器,docker-compose可通过服务名来通讯,也可使用127.0.0.1:映射到宿主机的端口访问
标签:compose,java,network,spot,application,docker,yml From: https://www.cnblogs.com/g177w/p/17513336.html