1. 程式人生 > >將訊息中心改造成springboot專案,並整合maven打包docker image

將訊息中心改造成springboot專案,並整合maven打包docker image

一手動打包測試:

[[email protected] docker]# ll
總用量 260160
-rw-r--r-- 1 root root       305 12月 22 21:12 Dockerfile
-rw-r--r-- 1 root root 183246769 12月 22 19:53 jdk-8u121-linux-x64.tar.gz

-rw-r--r-- 1 root root  83150458 12月 22 11:48 new_msg_center-0.0.1-SNAPSHOT.jar

# Dockerfile
FROM centos
MAINTAINER zhaixl

ADD jdk-8u121-linux-x64.tar.gz ./
RUN mv jdk1.8.0_121 jdk8
ADD new_msg_center-0.0.1-SNAPSHOT.jar app.jar

ENV JAVA_HOME=/jdk8
ENV PATH=$JAVA_HOME/bin:$PATH
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

EXPOSE 8081
 

ENTRYPOINT ["java","-jar","/app.jar"]

[[email protected] docker]#docker build -t zhaixl/new_messagecenter ./

[[email protected] docker]#docker run -it -p 8081:8081 -d --name messagecenter3 zhaixl/new_messagecenter

二 maven 自動build Docker Image

1. build Docker Image  zhaixl/centos_jdk8
FROM centos

MAINTAINER zhaixl

ADD jdk-8u121-linux-x64.tar.gz ./
RUN mv jdk1.8.0_121 jdk8

ENV JAVA_HOME=/jdk8
ENV PATH=$JAVA_HOME/bin:$PATH
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

[

[email protected] docker]#docker build -t zhaixl/centos_jdk8 ./

2. maven build Docker Image  zhaixl/centos_jdk8
在專案目錄建立Dockerfile檔案: src/main/docker/Dockerfile(沒有後綴),內容如下:
#build Docker file
MAINTAINER zhaixl
FROM zhaixl/centos_jdk8
VOLUME /tmp
ADD new_msg_center-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

#其中 new_msg_center-0.0.1-SNAPSHOT.jar 是專案打包後 /target/ 裡面的檔名    
3.在pom檔案中新增docker支援

<properties>
   <docker.image.prefix>ramer</docker.image.prefix>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.4.11</version>
            <configuration>
                <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                <dockerDirectory>src/main/docker</dockerDirectory>
                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>
    </plugins>
</build>
4.打包,建立映象:

mvn package -Dmaven.test.skip=true docker:build
注意: 請確保maven已新增到path中;並且已安裝docker,如果沒有安裝,請訪問: https://www.docker.com/community-edition#/download 下載合適的版本.
5.打包日誌

Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/eclipse/jgit/org.eclipse.jgit/3.2.0.201312181205-r/org.eclipse.jgit-3.2.0.201312181205-r.jar (1.9 MB at 327 kB/s)
[INFO] Copying /src/new_message_center/target/new_msg_center-0.0.1-SNAPSHOT.jar -> /src/new_message_center/target/docker/new_msg_center-0.0.1-SNAPSHOT.jar
[INFO] Copying src/main/docker/Dockerfile -> /src/new_message_center/target/docker/Dockerfile
[INFO] Building image zhaixl/new_msg_center
Step 1/7 : FROM zhaixl/centos_jdk8
 ---> 8e09f23ce4ec
Step 2/7 : MAINTAINER zhaixl
 ---> Running in d81d4dc0b618
 ---> dac5fed94068
Removing intermediate container d81d4dc0b618
Step 3/7 : VOLUME /tmp
 ---> Running in ac50880fc440
 ---> 18d1e2680991
Removing intermediate container ac50880fc440
Step 4/7 : ADD new_msg_center-0.0.1-SNAPSHOT.jar app.jar
 ---> 7fd1ce2b56c8
Removing intermediate container fc7cb19f2610
Step 5/7 : RUN sh -c 'touch /app.jar'
 ---> Running in 0f3d9e1d1f8a
 ---> b177875a9537
Removing intermediate container 0f3d9e1d1f8a
Step 6/7 : ENV JAVA_OPTS ""
 ---> Running in 80e8e155d03b
 ---> 27b2bd554187
Removing intermediate container 80e8e155d03b
Step 7/7 : ENTRYPOINT sh -c java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar
 ---> Running in 8f9f73c5c311
 ---> 3f64d4a5bc4a
Removing intermediate container 8f9f73c5c311
Successfully built 3f64d4a5bc4a
Successfully tagged zhaixl/new_msg_center:latest
[INFO] Built zhaixl/new_msg_center
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:58 min
[INFO] Finished at: 2017-12-22T23:04:00+08:00
[INFO] Final Memory: 51M/121M
[INFO] ------------------------------------------------------------------------
6. 執行docker容器

##最好新增--name引數
docker run --name=springboot-docker-demo -p 8081:8081 -it zhaixl/new_msg_center

在瀏覽器訪問: http://192.168.72.11:8081/messagecenter/api/1.0.0/message/getMessage

docker run --name=springboot-docker-demo -p 8081:8081 -it -d zhaixl/new_msg_center

7. 檢視正在執行的容器

cmd: docker ps

8. 啟動/關閉/重啟/刪除docker容器

cmd: docker start/stop/restart/rm CONTAINER_ID/NAME 
其中: 
CONTAINER_ID: 是容器id,執行 docker ps 可檢視 
NAME: 是容器的名稱,也就是docker run –name後面的名字

深圳逆時針