本人研究docker-compose中间花了大量的时间,网上一大堆,说的都不是很全面,决定贡献出来给大家
前提条件:宿主机安装jdk8,安装docker、安装docker-compose
关于安装docker、docker-compose的文章我前面已经说过了,可以回头再看看。
这里通过演示两个注册中心、一个配置服务:eureka-cluster、eureka-cluster2、config-center
目录结构如下:
1.编写Dockerfile文件
eureka
FROM java:8
# ARG JAR_FILE
# ARG LIB_FILE
ADD lib lib
ADD discovery-eureka-1.0.jar /app.jar
# ADD ${LIB_FILE} /lib
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Duser.timezone=GMT+08"]
CMD ["-Xms512m","-Xmx512m","-jar", "/app.jar","--spring.profiles.active=cluster","--server.port=8081","--eureka.client.service-url.defaultZone=http://root:12323232@172.16.11.6:8082/eureka/"]
eureka2
FROM java:8
# ARG JAR_FILE
# ARG LIB_FILE
ADD lib lib
ADD discovery-eureka-1.0.jar /app.jar
# ADD ${LIB_FILE} /lib
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Duser.timezone=GMT+08"]
CMD ["-Xms512m","-Xmx512m","-jar", "/app.jar","--spring.profiles.active=cluster","--eureka.instance.ipAddress=172.16.11.76","--server.port=8082","--eureka.client.service-url.defaultZone=http://root:12323232@172.16.11.6:8081/eureka/"]
config-center
FROM java:8
# ARG JAR_FILE
# ARG LIB_FILE
ADD lib lib
ADD config-center-1.0.jar /app.jar
# ADD ${LIB_FILE} /lib
# -- eureka.instance.ipAddress=172.16.11.76
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Duser.timezone=GMT+08"]
CMD ["-Xms512m","-Xmx512m","-jar", "/app.jar","--spring.profiles.active=native","--server.port=8889","--eureka.client.service-url.defaultZone=http://samc:samc@172.16.11.76:8081/eureka/,http://root:12323232@172.16.11.6:8082/eureka/"]
2.编写docker-compose
version: '3'
services:
euraka:
build: /home/cater/spring-java/eureka-cluster
ports:
- 8081:8081
volumes:
#将项目中的log4j日志目录,映射到宿主机的目录下,格式为 宿主机:容器目录
- "/application/logs/euraka:/application/logs/business-discovery"
euraka2:
build: /home/cater/spring-java/eureka-cluster2
ports:
- 8082:8082
depends_on:
- euraka
config-center:
build: /home/cater/spring-java/config-center
ports:
- 8889:8889
depends_on:
- euraka
- euraka2
4 整体结构如下:
5.进入docker-compose.yml所在的目录
运行命令:
docker-compose up -d
即可构建和运行项目
注意:ENTRYPOINT和CMD的效果不同,可以直接使用CMD替代
1 ENTRYPOINT是追加的方式,遇到数组是不能覆盖
--spring.profiles.active=native
而项目中的yml的配置 为spring.profiles.active=dev,那么配置在ENTRYPOINT中的效果就是:spring.profiles.active=native,dev ,是追加的方式
如果配置在CMD中,是覆盖的效果,即:spring.profiles.active=dev
2 eureka.instance.ipAddress 不能指定主机的ip
dockerfile中
如 eureka.instance.ipAddress=172.16.11.6,不能指定为主机的ip,在docker中这个选项略去