美文网首页English BootCamp英语训练营
centos7 docker部署springboot项目(两种构

centos7 docker部署springboot项目(两种构

作者: jeffrey_hjf | 来源:发表于2019-11-04 18:32 被阅读0次
docker

构建打包环境

我们需要有一个 Docker 环境来打包 Spring Boot 项目,不推荐在 Windows 搭建 Docker 环境,因此我这里以 Centos 7 为例。

安装JDK

  • 下载jdk
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
  • 解压jdk
tar -zxvf jdk-8u101-linux-x64.tar.gz -C /usr/local/java/
  • 配置环境变量(在/etc/profile文件末尾添加)
打开 vim /etc/profile
添加一下内容

export JAVA_HOME=/usr/local/java/jdk1.8.0_211
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
  • 使/etc/profile生效
source /etc/profile
  • 验证
[root@localhost hellodocker]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

安装MAVEN

  • 下载MAVEN
wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz
  • 解压MAVEN
tar -zxvf apache-maven-3.6.2-bin.tar.gz -C /usr/local/maven/
  • 配置环境变量(在/etc/profile文件末尾添加)
export MAVEN_HOME=/usr/local/apache-maven-3.6.2
export PATH=${MAVEN_HOME}/bin:$PATH
  • 使/etc/profile生效
source /etc/profile
  • 验证
[root@localhost hellodocker]# mvn -version
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T23:06:16+08:00)
Maven home: /usr/local/maven/apache-maven-3.6.2
Java version: 1.8.0_211, vendor: Oracle Corporation, runtime: /usr/local/java/jdk1.8.0_211/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1062.4.1.el7.x86_64", arch: "amd64", family: "unix"

安装git

yum install git -y

安装docker

  • 卸载旧版本
$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
  • 安装DOCKER ENGINE-社区
$ sudo yum install docker-ce docker-ce-cli containerd.io
  • 启动Docker
$ sudo systemctl start docker
  • 设置开始/关闭 启动
systemctl enable docker.service
systemctl disable docker.service
  • 通过运行hello-world 映像来验证是否正确安装了Docker Engine-Community
sudo docker run hello-world
  • 重新启动docker
systemctl restart docker

注:如在有限网络条件,可通过rpm包安装,具体如下:

1. 转到 [https://download.docker.com/linux/centos/7/x86_64/stable/Packages/](https://download.docker.com/linux/centos/7/x86_64/stable/Packages/) 并下载`.rpm`要安装的Docker版本的文件。
2. 安装Docker Engine-Community,将下面的路径更改为您下载Docker软件包的路径。
    $ sudo yum install /path/to/package.rpm
3. 启动Docker
    $ sudo systemctl start docker
4. 通过运行hello-world 映像来验证是否正确安装了Docker Engine-Community
    $ sudo docker run hello-world

简单 Spring Boot 项目

  • 目录结构


    Spring Boot 项目目录结构
  • pom
    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.2.0.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
    </parent>


  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
  </dependencies>
  • controller
@RestController
public class UserController {

    @Value("${ceair.name}")
    private String name;

    @GetMapping("/helloDocker")
    public String helloDocker() {
        return "helloDocker" + "---" + name + "---" + Math.random();
    }
}

  • 启动类
@SpringBootApplication
public class SpringBootDockerhwApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDockerhwApplication.class, args);
    }
}

非Docker方式运行程序

  • 打包
mvn clean package -Dmaven.test.skip=true
  • 运行
java -jar spring-boot-docker-hw.jar 
  • 访问
http://192.168.248.131:8080/helloDocker

在docker开始部署springBoot项目(方法一)

  • 编写Dockerfile文件
# 基础镜像
FROM openjdk:8-jdk-alpine

# 作者信息
MAINTAINER "jeffrey"

# 添加一个存储空间
VOLUME /tmp

# 暴露8080端口
EXPOSE 8080

# 添加变量,如果使用dockerfile-maven-plugin,则会自动替换这里的变量内容
ARG JAR_FILE=spring-boot-docker-hw.jar

# 往容器中添加jar包
ADD ${JAR_FILE} app.jar

# 启动镜像自动运行程序
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/urandom","-jar","/app.jar"]
  • 部署Dockerfile文件
[root@localhost hellodocker]# ls
Dockerfile  spring-boot-docker-hw.jar
  • 制作镜像

执行docker build命令,docker就会根据Dockerfile里你定义好的命令进行构建新的镜像。
-t代表要构建的镜像的tag,.代表当前目录,也就是Dockerfile所在的目录。

docker build -t spring-boot-docker-hw .
  • 启动容器

-d 参数是让容器后台运行
-p 宿主机port:容器port,此时将宿主机中的8899端口映射到容器中的8080(项目中端口配置的是8080)端口

docker run -d -p 8899:8080 spring-boot-docker-hw
  • 访问网站
http://192.168.248.131:8899/helloDocker

在docker开始部署springBoot项目(方法二)

  • 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>${project.basedir}/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 -->
  • 上传工程

把整个工程代码拷到centos服务器上

  • 打包
mvn package docker:build
  • 查看镜像
[root@localhost spring-boot-docker-hw]# docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
springboot/spring-boot-docker-hw   latest              f4ea1294e6b7        2 seconds ago       135MB
openjdk                            8-jdk-alpine        a3562aa0b991        5 months ago        105MB
hello-world                        latest              fce289e99eb9        10 months ago       1.84kB
  • 运行镜像
docker run -p 8899:8080 -t springboot/spring-boot-docker-hw
  • 查看访问镜像
#查看
[root@localhost spring-boot-docker-hw]# docker ps
CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS              PORTS                    NAMES
99e39b06ee01        springboot/spring-boot-docker-hw   "java -Djava.securit…"   35 seconds ago      Up 33 seconds       0.0.0.0:8899->8080/tcp   busy_khorana

#访问
[http://192.168.248.131:8899/helloDocker](http://192.168.248.131:8899/helloDocker)

容器操作

  • 列出所有容器
[root@localhost spring-boot-docker-hw]# docker ps -a
CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS                       PORTS                    NAMES
99e39b06ee01        springboot/spring-boot-docker-hw   "java -Djava.securit…"   2 minutes ago       Up 2 minutes                 0.0.0.0:8899->8080/tcp   busy_khorana

  • 容器启动 / 停止 / 重启
docker start nginx1    # 启动
docker stop NAMES    # 停止
docker restart NAMES    # 重启
  • 停止容器
#单个容器停止
[root@localhost ~]# docker stop 7b
7b
#批量容器停止
docker stop `docker ps  -q`
  • 删除容器
#单个容器删除
[root@localhost ~]# docker rm 7b
7b
#批量删除容器
docker rm -f `docker container ls -a -q`
  • 查看镜像
[root@localhost ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
spring-boot-docker-hw   latest              2f54a680ed3a        25 hours ago        661MB
hello-world             latest              fce289e99eb9        10 months ago       1.84kB
java                    8                   d23bdf5b1b1b        2 years ago         643MB
  • 删除镜像
[root@localhost ~]# docker rmi 2f
Untagged: spring-boot-docker-hw:latest
Deleted: sha256:2f54a680ed3a18e7426b93a19bbe25921b73b3853bb2745877c54ed2cec67876
Deleted: sha256:1e0b4cd09a65d99b21759e705a96de70c2509c1c8a0f3779b9baeea32a9fff4d
Deleted: sha256:5ad59a32074598603a75ffc309bc21f7a82949a269b47674b20248128b8a382f
Deleted: sha256:d6d4528d3a377ea771a74aaf7114e75af58f02f9801d04c09d3e5b884f22ebe8
Deleted: sha256:85aa3b592aca0d5d0579fb5829f676fbb0aacc275d6b1eb2ccca1e1a4839e461
Deleted: sha256:c1748dc71a5d108e063514159a605dfa377201eb3ac8207e32de4281f20243b4
Deleted: sha256:8d5e63c3850ebf5f66859b50f80a6ebe17e9726058006041f389fe38891f15c8

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        10 months ago       1.84kB
java                8                   d23bdf5b1b1b        2 years ago         643MB

项目Git地址

GIT 下载

相关文章

网友评论

    本文标题:centos7 docker部署springboot项目(两种构

    本文链接:https://www.haomeiwen.com/subject/hirlbctx.html