基于容器的项目启动
java -Dcluster.name=eom -Dcluster.env=test -Dservice.name=eom-data-service -jar target/eom-data-service.jar
docker配置
1. pom配置docker插件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <plugin > <groupId > com.spotify</groupId > <artifactId > dockerfile-maven-plugin</artifactId > <version > 1.4.13</version > <configuration > <repository > ${project.artifactId}</repository > <dockerfile > Dockerfile</dockerfile > <contextDirectory > ${project.basedir}</contextDirectory > <buildArgs > <JAR_FILE > ${project.build.finalName}</JAR_FILE > </buildArgs > <tag > ${project.version}</tag > </configuration > </plugin >
2. 打包\构建镜像
1 mvn clean package dockerfile:build -Dmaven.test.skip=true
3. DockerFile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 FROM openjdk:11.0.4 as builder EXPOSE 8080 RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone ARG JAR_FILE WORKDIR /${JAR_FILE} ADD ./target/${JAR_FILE}.jar ./app.jar VOLUME /${JAR_FILE}/logs ENV JAVA_OPS -server -Xms1024m -Xmx2048m -XX:CompressedClassSpaceSize=128m -XX:MetaspaceSize=200m -XX:MaxMetaspaceSize=200m \ -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=logs/${JAR_FILE}.hprof ENV GC_LOG -Xlog:gc* \ -XX:+PrintGCDateStamps \ -XX:+PrintTenuringDistribution \ -XX:+PrintHeapAtGC \ -XX:+PrintReferenceGC \ -XX:+PrintGCApplicationStoppedTime \ -XX:+UseGCLogFileRotation \ -XX:NumberOfGCLogFiles=10 \ -XX:GCLogFileSize=10M \ -XX:+IgnoreUnrecognizedVMOptions \ -Xlog:gc:logs/${JAR_FILE}.gc.log CMD mkdir -p /${JAR_FILE}/logs && mkdir -p /${JAR_FILE}/config && java ${JAVA_OPS} ${GC_LOG} -Djava.security.egd=file:/dev/./urandom -Dserver.port=8080 -Dcluster.name=$cluster_name -Dcluster.env=$cluster_env -Dservice.name=$service_name -Dservice.serial=0.0.1 -jar app.jar
DockerFile
中的 JAR_FILE
指定的是项目名 eom-data-service
, 在 docker
容器中 /eom-data-service
即为工作目录, app.jar
和 logs
目录都在该目录下。业务日志配置 /eom-data-service/logs
目录, 这样所有日志都在同一目录了, 挂载到宿主机磁盘即可。
docker
容器中可以统一强制运行在 8080
端口, 根据业务需要run镜像时候通过 -p
对外暴露
4. 登录harbor及推送镜像到仓库
5. docker执行
docker run --rm -d --name=eom-data-service -p 7002:8080 -v /Users/cuishiying/Desktop/logs:/eom-data-service/logs -e cluster_name=eom -e cluster_env=test -e service_name=eom-data-service eom-data-service:0.0.5
手动构建docker镜像
1 2 docker build -t eom-data-service:0.0.1 . docker build -t eom-data-service:latest .
登录harbor仓库
1 docker login 10.10.10.108:80
为docker仓库镜像打tag
docker tag 镜像名:标签 私服地址/仓库项目名/镜像名:标签
1 2 docker tag eom-data-service:0.0.1 10.10.10.108:80/test/eom-data-service:0.0.1 docker tag eom-data-service:0.0.1 10.10.10.108:80/test/eom-data-service:latest
将docker镜像推送到harbor仓库
docker push 私服地址/仓库项目名/镜像名:标签
1 2 docker push 10.10.10.108:80/test/eom-data-service:0.0.1 docker push 10.10.10.108:80/test/eom-data-service:latest
war包Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 FROM tomcat:9-jre11 EXPOSE 8080 8443 RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN cp -r webapps.dist/* /usr/local/tomcat/webapps/ ENV APP_NAME xxx-web WORKDIR /${APP_NAME} RUN mkdir conf ADD conf.properties ./conf/conf.properties ENV XXX_HOME=/${APP_NAME}/ XXX_CONFIG_BASE=/${APP_NAME}/ ADD target/*.war /usr/local/tomcat/webapps/ ENV JAVA_OPTS="-server -Xms1024m -Xmx2048m -XX:CompressedClassSpaceSize=128m -XX:MetaspaceSize=200m -XX:MaxMetaspaceSize=200m \ -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=logs/${APP_NAME}.hprof" ENV GC_LOG="-Xlog:gc* -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintHeapAtGC -XX:+PrintReferenceGC \ -XX:+PrintGCApplicationStoppedTime -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=10M -XX:+IgnoreUnrecognizedVMOptions -Xlog:gc:logs/${APP_NAME}.gc.log" CMD ["catalina.sh", "run"]
启动docker
1 docker run --rm -d --name=xxx-web -p 8080:8080 -v /Users/cuishiying/Desktop/logs:/usr/local/tomcat/logs -v /Users/cuishiying/Desktop/config:/xxx-web/conf xxx-web:latest
访问 curl http://localhost:8080/xxx-web/index
即可看到项目首页