docker實現多個jar部署在一個容器中
在之前的文章中介紹瞭如何在docker中部署多個實例,這篇文章講演化後的第二版本的docker部署多jar包。
Dockerfile
centos7+java環境
FROM centos:7
MAINTAINER linwenyao
WORKDIR /usr
RUN mkdir /usr/local/java
ADD jdk-8u181-linux-x64.tar.gz /usr/local/java/
ENV JAVA_HOME /usr/local/java/jdk1.8.0_181
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH
添加語言包:
FROM registry.cn-hangzhou.aliyuncs.com/lin-service/java-env:1.0.0
MAINTAINER linwenyao
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& yum -y update \
&& yum -y install kde-l10n-Chinese \
&& yum -y reinstall glibc-common \
&& localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8 \
&& echo 'LANG="zh_CN.UTF-8"' > /etc/locale.conf \
&& source /etc/locale.conf \
&& yum clean all
ENV LANG=zh_CN.UTF-8 \
LC_ALL=zh_CN.UTF-8
另外把maven的pom.xml文件添加如下配置:
//添加環境變量
<profiles>
<profile>
<id>cloud</id>
<properties>
<spring.profiles.active>cloud</spring.profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<spring.profiles.active>test</spring.profiles.active>
</properties>
</profile>
<profile>
<id>local</id>
<properties>
<spring.profiles.active>local</spring.profiles.active>
</properties>
</profile>
</profiles>
pom.xml中還要添加如下配置:
//修改配置文件中的標識符,因爲在.yml中$標識符被佔用,所以需要改成#
<properties>
<resource.delimiter>#</resource.delimiter>
</properties>
//定義佔位符
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources.${spring.profiles.active}</directory>
<filtering>false</filtering>
</resource>
</resources>
</build>
並且springboot的配置文件bootstrap.yml改成:
spring:
profiles:
active: #spring.profiles.active#
重點是#spring.profiles.active#,#是標識符,spring.profiles.active是佔位符
然後就是生成最後的容器了:
FROM registry.cn-hangzhou.aliyuncs.com/lin-service/java-env:1.1.0
MAINTAINER linwenyao
COPY xxx/target/xxx.jar /app1.jar
COPY xxx/xxx/target/xxx.jar /app2.jar
COPY xxx/xxx/target/xxx.jar /app3.jar
COPY xxx/xxx/target/xxx.jar /app4.jar
RUN echo -e \
"#!/bin/sh\n"\
"nohup java -jar /app1.jar > 1.log &\n"\
"nohup java -jar /app2.jar > 2.log &\n"\
"nohup java -jar /app3.jar >3.log &"\
>> /usr/bin/start.sh
RUN chmod +x /usr/bin/start.sh
CMD nohup sh -c "start.sh && java -jar /app4.jar"
jenkins的配置也要改,首先maven的build命令:
-Pxxx就是使用profiles中配置的環境變量名稱
然後就是build:
docker build -t registry.cn-hangzhou.aliyuncs.com/xxx:1.1.0 .
docker run -d --name xxx -p 8105:8105 registry.cn-hangzhou.aliyuncs.com/xxx:1.1.0
docker push registry.cn-hangzhou.aliyuncs.com/xxx:1.1.0
經過時間演變,我研發出了一種把多個jar放在一個docker中的方式還更實用,佔用更少空間,佔更少內存的方法。就是第三個版本,將會在後面文章中寫出來。