springboot + docker 部署测试环境

1,530 阅读3分钟

1.简介:

  • springboot:略
  • dockerDocker属于Linux容器的一种封装,提供简单易用的容器使用接口。Docker将应用程序与该程序的依赖,打包在一个文件里面。运行这个文件,就会生成一个虚拟容器。程序在这个虚拟容器里运行,就好像在真实的物理机上运行一样。有了Docker,就不用担心环境问题。Docker是开发人员和系统管理员使用容器开发、部署和运行应用程序的平台。使用Linux容器来部署应用程序称为集装箱化。
    1. 镜像(Image):类似于虚拟机的镜像,可以将他理解为一个面向Docker引擎的只读模板,包含了文件系统。应用运行是需要环境的,而镜像就是来提供这种环境。
    2. Docker容器(Container):Docker利用容器来运行和隔离应用。容器是从镜像创建的应用运行实例,可以将其启动、开始、停止、删除,而这些容器都是相互隔离、互不可见的。
      可以把每个容器看作一个简易版的Linux系统环境(包括了root用户权限、进程空间、用户空间和网络空间),以及与运行在其中的应用程序打包而成的应用盒子。镜像自身是只读的。容器从镜像启动的时候,Docker会在镜像的最上层创建一个可写层,镜像本身将保持不变。就像用ISO装系统之后,ISO并没有什么变化一样。
    3. Docker仓库(Repository):类似与代码仓库,是Docker集中存放镜像文件的场所。参考git。

2.开始使用(以黄金大玩家项目为例子)

  • centos测试环境机器安装docker;

### 卸载残留旧版本 
$ sudo yum remove docker	\
		  docker-client	\
		  docker-client-latest	\
		  docker-common	\
		  docker-latest	\
		  docker-latest-logrotate	\
		  docker-logrotate	\
		  docker-selinux	\
		  docker-engine-selinux	\
		  docker-engine

### yum 安装依赖包
$ sudo yum install -y yum-utils \
                      device-mapper-persistent-data	\
                      lvm2

### 安装源(中国科学技术大学源)
$ sudo yum config-manager \
            --add -repo \
	    https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce .repo


### 安装源(官方源,非常慢)
### $ sudo yum config-manager \ 
###            --add -repo \
###            https://download.docker.com/linux/centos/docker-ce.repo

### 更新yum软件源缓存,并安装docker-ce
$ sudo yum makecache fast 
$ sudo yum install docker-ce

### 启动Docker CE
$ sudo systemctl enable docker 
$ sudo systemctl start docker

### Docker hello-world
$ docker run hello-world

Unable to find image 'hello-world:latest' locally 
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete 
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9f de470971e499788 
Status: Downloaded newer image for hello-world:latest
Hello from Docker! 
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps: 
1. The Docker client contacted the Docker daemon. 
2. The Docker daemon pulled the "hello-world" image from the Do cker Hub.    (amd64) 
3. The Docker daemon created a new container from that image wh ich runs the    executable that produces the output you are currently readin g. 
4. The Docker daemon streamed that output to the Docker client, which sent it    to your terminal.
To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID : https://hub.docker.com/
For more examples and ideas, visit: https://docs.docker.com/get-started/

### 至此处 docker安装完成!
  • IDEA安装docker插件

  file -> setting -> Plugins -> search , 搜索docker,安装docker integration,重启IDEA

  • docker整合黄金大玩家项目
  1. 在项目根目录创建文件夹docker,该文件夹与pom文件同级,在docker下创建文件Dockerfile,在该文件配置相关信息,详情查询指令详解。

    FROM java:8
    VOLUME /tmp
    ADD *.jar app.jar
    EXPOSE 8555
    ENTRYPOINT ["java","-jar","/app.jar"]

  2. IDEA配置docker相关参数


  3. 待定,是否使用maven来构建docker镜像并上传到私服仓库。目前使用的原理是,maven编译源码,使用docker构建镜像运行在测试环境的docker容器。
  4. 使用idea远程部署到测试机器



至此,黄金大玩家项目部署到远程测试黄金完成!

3.这样做的解决的问题及相关遇到的问题

  • 黄金大玩家是属于一个客户的一个小活动,一个客户会有多个小活动。我们有多个客户就会有很多活动,可能会有不同客户功能相似的项目,只使用一台测试机器,非常混乱。使用虚拟化容器管理项目,不同项目相互独立于一个容器,约等于拥有多套测试环境,降低耦合度。
  • 开发测试的时候,在本地搭一套环境,单元测试没有问题。部署到测试环境后,项目跑不起来,docker使用镜像,开发测试使用的是同一个镜像,解决这个问题。