SpringBoot + Docker achieve the project's Maven build & packaging container & mirror == run simple automated deployment

Copyright: please leave a message like Give me praise problematic -------------------------------- will be updated from time to time , because learning, so happy, because the share, so convenient! Reprint please indicate the source, ha ha! https://blog.csdn.net/Appleyk/article/details/87367281

 

First, what is the Docker

 

Reference article: Docker concept details

Reference: Docker Technology Introduction and combat (Second Edition - HD)


 

Two, Centos7 installation Docker

 

Online installation teach Cheng Bowen lot, I borrowed directly integrate a moment, here follow me again with the correct installation steps now!

 


 

(1) Preparation Tool

 

1.1 Preparation virtual machine software, can not go to my Baidu network disk download, address: VM WorkStation15 with [License] .zip

1.2 Preparation Centos7 virtual machine image, pure version] [pre-installed JDK8 Address: https://pan.baidu.com/s/1Tq151v2SckcwvOsB_ovExA

 

 

Note: The virtual machine images User: root, password: root123

 

 

 


 

1.3 rename a virtual machine image Project Name -> Docker (Test), and start

 

 


 

View Centos7 version 1.4 and Java

 

 

# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core) 

# java -version
openjdk version "1.8.0_65"
OpenJDK Runtime Environment (build 1.8.0_65-b17)
OpenJDK 64-Bit Server VM (build 25.65-b01, mixed mode)

 

(2) Installation Docker

 

Centos7上安装docker

Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企业版EE。

社区版是免费提供给个人开发者和小型团体使用的,企业版会提供额外的收费服务,比如经过官方测试认证过的基础设施、容器、插件等。

社区版按照stable和edge两种方式发布,每个季度更新stable版本,如17.06,17.09;每个月份更新edge版本,如17.09,17.10。

======================================================================================================

 一、安装docker

1、Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。

通过 uname -r 命令查看你当前的内核版本

 $ uname -r

======================================================================================================
2、使用 root 权限登录 Centos。确保 yum 包更新到最新。

$ sudo yum update

======================================================================================================

3、卸载旧版本(如果安装过旧版本的话)

$ sudo yum remove docker  docker-common docker-selinux docker-engine


注:推荐一种删除docker的方法:

yum remove docker docker-common docker-selinux docker-engine -y;
find /etc/systemd -name '*docker*' -exec rm -f {} \;
find /lib/systemd -name '*docker*' -exec rm -f {} \;

======================================================================================================

4、安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2

======================================================================================================

5、设置yum源,配置国内docker仓库 -- 配置了阿里云镜像勿需在配置其他加速器,否则启动会报错。

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

======================================================================================================

6、可以查看所有仓库中所有docker版本,并选择特定版本安装

$ yum list docker-ce --showduplicates | sort -r

======================================================================================================

7、安装docker

$ sudo yum install docker-ce  #由于repo中默认只开启stable仓库,故这里安装的是最新稳定版17.12.0
$ sudo yum install <FQPN>  # 例如:sudo yum install docker-ce-17.12.0.ce,一定要注意版本!

======================================================================================================

8、启动并加入开机启动

$ sudo systemctl start docker
$ sudo systemctl enable docker

======================================================================================================

9、验证安装是否成功(有client和service两部分表示docker安装启动都成功了)

$ docker version

======================================================================================================

 


 

2.1 View system kernel

 

 


 

2.2 update yum, this step a long time, be patient

 

 

 


 

Docker 2.3 uninstall the old version (if previously installed, then not, then ignore this operation)

 

See above command

 

 

Recommended use:

 


 

2.4 installation package required

 


 

2.5 Setting yum source configured to domestic docker warehouse

 

 

 

Address: http://mirrors.aliyun.com/docker-ce/linux/centos/

 

 


 

View all Docker version 2.6 warehouse

 


 

2.7 安装Docker

 

两种方式,推荐使用第二种

 

备注:使用第一种方式安装,可能会出现Docker版本过高,与当前系统不兼容的情况发生,可能出现的问题就是容器run不起来

$ sudo yum install docker-ce  #由于repo中默认只开启stable仓库,故这里安装的是最新稳定版17.12.0

 

强推:使用下面的这种方式,自行选择对应的版本进行安装!!!


$ sudo yum install <FQPN>  # 例如:sudo yum install docker-ce-17.12.0.ce

 

 

 

等待几分钟后,完成安装

 

 


 

2.8 启动Docker并加入开机启动

 


 

2.9 查看Docker版本

 

 


 

2.10.0 查看本地镜像(第一次安装,镜像列表空)

 

# docker images

 

 


 

2.10.1 在容器中运行hello-world程序

 

# docker run hello-world

 

 


 

2.10.2 再次查看本地Docker镜像

 


 

至此,关于Docker安装的部分就已经结束了,集群模式可以另行尝试,下面进入正题!

 


 

三、基于SpringBoot框架创建Hello-World项目

 

 

(1)IDEA项目结构图

 

 

 

Dockerfile文件(配置docker指令):

 

# 基础镜像使用java
FROM java:8
# VOLUME 指定了临时文件目录为/tmp。
# 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
VOLUME /tmp 
# 将jar包添加到容器中并更名为mydocker.jar
ADD springbootdocker-0.0.1-SNAPSHOT.jar mydocker.jar 
# 运行jar包
RUN bash -c 'touch /mydocker.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/mydocker.jar"]

 

 


 

Docker-Maven打包插件(POM)配置(完整版见GitHub项目源码)

 

 <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>
                <configuration>
                    <!-- 镜像名称  -->
                    <imageName>${project.artifactId}</imageName>
                    <!-- Dockerfile 文件目录 -->
                    <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
                    <!-- docker远程服务地址 ,前提是docker服务器需开启远程访问-->
                    <dockerHost>http://192.168.111.132:2375</dockerHost>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <!-- 资源所在目录 -->
                            <directory>${project.build.directory}</directory>
                            <!-- 生成的.jar文件 -->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                        <resource>
                            <targetPath>/</targetPath>
                            <!-- 资源所在目录 -->
                            <directory>${project.build.outputDirectory}</directory>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

 

 

 

项目GitHub地址:https://github.com/kobeyk/SpringBoot-Docker/tree/master

 

 


 

四、Jar包+Dockerfile文件构建Docker镜像

 

 

(1)SpringBoot项目打成Jar包

 

cmd命令:mvn clean package -DskipTests

 

 


 

 


 

(2)将Jar包和Dockerfile文件一起上传至docker服务器下的/opt/docker目录下

 

2.1 先创建/opt/docker目录

 


 

2.2 利用xftp工具上传两个文件,一个jar包,一个file

 

 


 

(3)使用docker build命令(附命令链接地址)构建镜像

 

(1)将dockerfile文件和生成好的jar 使用ftp工具上传到linux服务器 随便找个文件夹 放进去 jar和dockerfile在同级目录下

(2)使用 docker build -t name:tag .

(3)docker images 查看构建的镜像

(4)docker run ... 在容器中运行镜像

 

# docker build -t springboot-demo:v1.0

 

 

执行Dockerfile里面的指令,第一步,从docker中央仓库拉取基础镜像Java

 

 


镜像由N个层(layer)组成,上图中的"5040bd298390"为层的ID,所有层全部拉取完即意味着镜像拉取成功!


 

完成后的完整输出信息:

 

[root@bogon docker]# docker build -t springboot-demo:v1.0 .
Sending build context to Docker daemon  14.63MB
Step 1/5 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete 
fce5728aad85: Pull complete 
76610ec20bf5: Pull complete 
60170fec2151: Pull complete 
e98f73de8f0d: Pull complete 
11f7af24ed9c: Pull complete 
49e2d6393f32: Pull complete 
bb9cdec9c7f3: Pull complete 
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
 ---> d23bdf5b1b1b
Step 2/5 : VOLUME /tmp
 ---> Running in a2b77758757b
Removing intermediate container a2b77758757b
 ---> 5928f971308d
Step 3/5 : ADD springbootdocker-0.0.1-SNAPSHOT.jar mydocker.jar
 ---> a8a0723f8b41
Step 4/5 : RUN bash -c 'touch /mydocker.jar'
 ---> Running in ecf29bfa58a1
Removing intermediate container ecf29bfa58a1
 ---> 626d13af1992
Step 5/5 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/mydocker.jar"]
 ---> Running in 20bf8cb46ed9
Removing intermediate container 20bf8cb46ed9
 ---> 5ca9a30874b5
Successfully built 5ca9a30874b5
Successfully tagged springboot-demo:v1.0
[root@bogon docker]# 

 


 

(4)查看本地镜像

 

 


 

(5)在docker容器中运行springboot-demo

 

docker run命令(链接地址)

 

# docker run -d -p 8080:8080 springboot-demo:v1.0  -- 运行镜像
# docker ps -- 查看docker中最近运行的容器状态

 

 


 

(6)外部网络浏览器访问接口地址

 

 


 


 

至此,第一种在docker容器中运行springboot项目的方式结束了,接下来,我们看第二种方式

 


 

 

  1. 五、直接在项目中使用Maven命令远程构建Docker镜像

  2.  

 

(1)执行Maven命令

 

 


 

打包过程输出信息如下:

 

[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.2.0:build (default-cli) on project springbootdocker: Exception caught: Timeout: GET http://192.168.111.184:2375/version: com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException: com.spotify.docker.client.shaded.org.apache.http.conn.ConnectTimeoutException: Connect to 192.168.111.132:2375 [/192.168.111.184] failed: connect timed out -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

 

莫慌,由于docker未设置远程访问,因此,错误提示我们: Connect to 192.168.111.132:2375 [/192.168.111.184] failed: connect timed out

 

(2) 设置docker远程访问

 

注意防火墙(要么关闭,要么开放指定端口)

开启docekr远程访问

[root@localhost frinder]# vi /usr/lib/systemd/system/docker.service


在:ExecStart=/usr/bin/dockerd 后面追加:-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

 

 

追加完成后,保存退出

 


 

(3) 重新加载docker守护进程并重启docker服务

 

# systemctl daemon-reload
# systemctl restart docker

 

 

 

 

防火墙开放端口:2375

 

# sudo firewall-cmd --zone=public --add-port=2375/tcp --permanent

然后更新防火墙设置,使配置生效

# firewall-cmd --reload

 


 

 


 

(4)再次打包远程构建docker镜像

 

  成功!!!

 

 

 


 

(5)docker服务器查看本地镜像

 

 

 


 

(6)启动镜像:springbootdocker

 

 

-p 8081:8080 --> 将容器的8080端口映射成宿主主机的8081端口

 


 

(7)本地网络浏览器访问接口

 

 

 


 

(7) a start image (the same image construction project) again: springboot-Demo: V1.0

 

 


 

(8) access interface, a 8081, a 8082

 

 

 

 


 

(9) View specified container (SpringBoot embedded tomcat) real-time log

 

# Docker logs container ID

 

 


 

(10) Close the specified container (host port of the host to close the container map corresponding to Case 8082)

 

# Docker stop container ID

 

 


 

(11) browser to access port 8082 of interface (API)

 

 

 


 

So far, docker container how to run SpringBoot be pulled clear of the project, follow-up will continue in-depth study ....

 

Guess you like

Origin blog.csdn.net/Appleyk/article/details/87367281