目的
由开发环境交测的时候,通过docker镜像简化环境搭建及项目部署的过程。
环境描述
项目开发环境: windowns7
在windowns7中通过VMware Workstation安装CentOS 7
在CentOS 7中安装docker
docker配置
1.docker正常安装。通过yum安装后最终信息如下:
[samhxm@localhost app]$ sudo docker version Client: Version: 1.12.3 API version: 1.24 Go version: go1.6.3 Git commit: 6b644ec Built: OS/Arch: linux/amd64 Server: Version: 1.12.3 API version: 1.24 Go version: go1.6.3 Git commit: 6b644ec Built: OS/Arch: linux/amd64
2.docker开启远程API。
通过yum安装的docker配置文件是/lib/systemd/system/docker.service
修改该文件的ExecStart参数为加入/usr/bin/dockerd -H TCP://0.0.0.0:2375 -H UNIX:///VAR/RUN/DOCKER.SOCK 最终配置如下:
[Unit] Description=Docker Application Container Engine Documentation=https://docs.docker.com After=network.target [Service] Type=notify # the default is not to use systemd for cgroups because the delegate issues still # exists and systemd currently does not support the cgroup feature set required # for containers run by docker ExecStart=/usr/bin/dockerd -H TCP://0.0.0.0:2375 -H UNIX:///VAR/RUN/DOCKER.SOCK ExecReload=/bin/kill -s HUP $MAINPID # Having non-zero Limit*s causes performance problems due to accounting overhead # in the kernel. We recommend using cgroups to do container-local accounting. LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity # Uncomment TasksMax if your systemd version supports it. # Only systemd 226 and above support this version. #TasksMax=infinity TimeoutStartSec=0 # set delegate yes so that systemd does not reset the cgroups of docker containers Delegate=yes # kill only the docker process, not all processes in the cgroup KillMode=process [Install] WantedBy=multi-user.target
3.重启docker
[samhxm@localhost ~]$ sudo systemctl daemon-reload
[samhxm@localhost ~]$ sudo systemctl restart docker
4.获取包含jre的基础镜像。如果仅仅是运行java程序,镜像中包含jre即可。我使用的是daocloud的java镜像。如果项目中需要其他环境依赖(数据库,MQ,Redis等)可以事先自行构建一个包含所需环境的镜像。
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
daocloud.io/library/java latest 861e95c114d6 4 weeks ago 643.1 MB
daocloud.io/library/mysql latest cf725f136fd2 7 weeks ago 383.4 MB
maven项目pom.xml配置
在项目的pom.xml中加入docker-maven-plugin插件,根据项目实际情况进行设置。
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <dockerHost>http://192.168.1.107:2375</dockerHost> <imageName>sam/spring-boot-dockerimage</imageName> <baseImage>daocloud.io/library/java:latest</baseImage> <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin>
配置项说明:
dockerHost: docker宿主机地址,其中端口号为docker远程API访问端口。
imageName: 构建镜像的repository,该值勿包含英文大写字母。
baseImage: 基础镜像。基于该镜像构建项目的运行环境。
entryPoint: 同dockerfile中的命令,还可以写其他在dockerfile中运行的命令。
resources: 项目的资源配置
运行maven命令构建项目,并生成docker镜像
E:workspace2dockerImage>mvn clean package docker:build
其中关键信息如下:
[INFO] Copying E:workspace2dockerImage argetdockerimage.jar -> E:workspace2dockerImage argetdockerdockerimage.jar [INFO] Building image sam/spring-boot-dockerimage Step 1 : FROM daocloud.io/library/java:latest ---> 861e95c114d6 Step 2 : ADD /dockerimage.jar // ---> d401cb3c9dfd Removing intermediate container 49fcdc6cb81e Step 3 : ENTRYPOINT java -jar /dockerimage.jar ---> Running in dd5ed0604906 ---> a295878e59f6 Removing intermediate container dd5ed0604906 Successfully built a295878e59f6 [INFO] Built sam/spring-boot-dockerimage [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 24.871 s [INFO] Finished at: 2016-12-10T17:18:55+08:00 [INFO] Final Memory: 29M/466M [INFO] ------------------------------------------------------------------------
检查docker宿主机是否已产生镜像
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE sam/spring-boot-dockerimage latest 804270c5ba56 7 seconds ago 658.9 MB daocloud.io/library/java latest 861e95c114d6 4 weeks ago 643.1 MB daocloud.io/library/mysql latest cf725f136fd2 7 weeks ago 383.4 MB
启动镜像
[samhxm@localhost ~]$ sudo docker run -it -p 8080:8080 sam/spring-boot-dockerimage:latest /bin/bash
通过docker宿主机ip和8080端口即可访问项目。