本文前提:

1. 已经安装好Docker环境

2. 已经安装好Maven环境,没有安装的请参考:http://www.bianxiaofeng.com/f/article/62.html

创建一个SpringBoot项目

创建一个Springbootboot项目,为了方便测试,写一个方法:

    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
        return "Hello Docker";
    }

添加Docker支持

在pom文件中添加Docker镜像名称

 <properties>
    <docker.image.prefix>springboot</docker.image.prefix>
</properties>

然后在pom文件中添加Docker构建插件

            <!-- Docker maven plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</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>
            <!-- Docker maven plugin -->

ps:dockerDirectory中的内容为后面dockerfile文件地址

在src/main/docker下穿件Dockerfile文件,Dockerfile文件用于描述如何构建镜像

#使用Java8 做为基础镜像,如果没有Java8的镜像,会从Docker仓库中自动下载
FROM java:8
#由于SpringBoot使用Tomcat作为内置服务器,Tomcat的工作目录为/temp,所以此命令就是做一个映射
VOLUME /temp
#其中dockerdeployment-0.0.1-SNAPSHOT.jar是构建出来的jar包的名称,此命令就是拷贝文件然后重命名为app.jar
ADD dockerdeployment-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

使用Docker部署

ps:在到此步骤之前,需要确认项目是没有问题的,即能够运行

将该项目文件(整个项目)拷贝到服务器的任意目录下,我这里拷贝的是:/home/dimple/test/目录,项目名称为dockerdeployment,然后进入到该目录:

输入以下命令构建镜像:

mvn package docker:build

看到以下结果表示成功:】

[INFO] Building image springboot/dockerdeployment
Step 1/6 : FROM java: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/6 : VOLUME /temp

 ---> Running in 896ef26a580f
Removing intermediate container 896ef26a580f
 ---> 717864a692ca
Step 3/6 : ADD dockerdeployment-0.0.1-SNAPSHOT.jar app.jar

 ---> f970a322e59a
Step 4/6 : RUN sh -c 'touch /app.jar'

 ---> Running in 7ab217070e9a
Removing intermediate container 7ab217070e9a
 ---> ebe71390a282
Step 5/6 : ENV JAVA_OPTS=""

 ---> Running in cea2dd14cbab
Removing intermediate container cea2dd14cbab
 ---> 1de12ae60650
Step 6/6 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

 ---> Running in 3a130dd2a191
Removing intermediate container 3a130dd2a191
 ---> 5b354a8a8d10
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 5b354a8a8d10
Successfully tagged springboot/dockerdeployment:latest
[INFO] Built springboot/dockerdeployment
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:33 min
[INFO] Finished at: 2019-07-19T10:34:05+08:00
[INFO] ------------------------------------------------------------------------

构建成功后,使用以下命令查看构建好的镜像:

root@iZwz99z2ljzabken0hvkjrZ:/home/dimple/test/dockerdeployment# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
springboot/dockerdeployment   latest              5b354a8a8d10        13 seconds ago      684MB
mysql                         5.7.26              e9c354083de7        28 hours ago        373MB
mysql                         latest              de764ad211de        28 hours ago        443MB
redis                         5.0.5               598a6f110d01        7 days ago          118MB
java                          8                   d23bdf5b1b1b        2 years ago         643MB

然后启动容器:

root@iZwz99z2ljzabken0hvkjrZ:/home/dimple/test/dockerdeployment# docker run -d -p 9999:8080 --name DockerTest springboot/dockerdeployment:latest 

docker命令不再解释,如果不熟悉请参考http://www.bianxiaofeng.com/f/article/60.html

然后输入进行测试:

root@iZwz99z2ljzabken0hvkjrZ:/home/dimple/test/dockerdeployment# curl 127.0.0.1:9999/hello