Doccker
-
DockerClient客户端
-
Docker Daemon守护进程
-
Docker Image镜像
-
DockerContainer容器
docker安装
ubuntu下的安装。使用snap应该是最简单的方法。
#安装docker
sudo snap install docker
#移除docker
sudo snap remove docker
查看snap的安装列表
可以看到本次安装的docker版本是18.06.1-ce
lgj@lgj-Lenovo-G470:~$ snap list docker Name Version Rev Tracking Publisher Notes docker 18.06.1-ce 384 stable canonical* -
测试docker是否安装成功
lgj@lgj-Lenovo-G470:~$ docker version Client: Version: 18.06.1-ce API version: 1.38 Go version: go1.10.4 Git commit: e68fc7a Built: Tue May 7 17:57:34 2019 OS/Arch: linux/amd64 Experimental: false Server: Engine: Version: 18.06.1-ce API version: 1.38 (minimum version 1.12) Go version: go1.10.4 Git commit: e68fc7a Built: Tue May 7 17:57:34 2019 OS/Arch: linux/amd64 Experimental: false
docker常用命令
#查看docker支持的所有命令
docker
#查看docker命令的使用方式
docker images --help
docker ps --help
#查看安装的镜像
docker images [OPTIONS] [REPOSITORY[:TAG]]
#查看运行的容器
docker ps
#查看所有的容器
docker ps -a
#删除镜像
docker rmi -f [REPOSITORY[:TAG]]
#删除容器
docker rm [OPTIONS] CONTAINER [CONTAINER...]
更多命令参考官方命令文档
docker的更多使用方法参考
<Docker开发实践><Docker开发指南><没什么难的Docker入门与开发实战><第一本DOCKER书>
Jenkins
Jenkins是一个独立的开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。前身是Hudson是一个可扩展的持续集成引擎。可用于自动化各种任务,如构建,测试和部署软件。Jenkins可以通过本机系统包Docker安装,甚至可以通过安装Java Runtime Environment的任何机器独立运行。
安装
选择下载war包
下载之后直接运行即可,指定端口为8097,访问地址为http://localhost:8097
java -jar jenkins.war --httpPort=8097
运行后会在用户目录下生成.jenkins工作目录。
首次登录需要用户名和密码
用户名为:admin
密码查看 .jenkins/secrets/initialAdminPassword 文件中。
lgj@lgj-Lenovo-G470:~$ cat .jenkins/secrets/initialAdminPassword 7d3bc703deb449debce0a25dfd3a00ad
配置
安装git插件
系统管理-插件管理
配置jdk和maven
系统管理-全局工具配置
可以选择本地或者自动安装
jemkins更多使用方式参考官方文档 <jenkins权威指南>
创建一个SpringBoot应用
只有一个controller类
@RestController @RequestMapping("/web") public class WebController { @RequestMapping("/demo") public String demo(){ return new Random().nextInt(100)+""; } }
访问端口为:8452
server: port: 8452
上传到git,仓库地址为https://github.com/lgjlife/web-demo
构建jenkins项目
首页-新建任务-构建一个maven项目(项目名称为:web-demo-docker)
创建成功后首页可以看到项目列表
项目配置
点击项目名称---配置---进入配置页面
1.配置git仓库
jenkins构建时会自动从git仓库拉取项目文件。
git@github.com:lgjlife/web-demo.git
注意url的写法,写错会自动报错。由于是从公共仓库拉取,所以没有进行认证配置。
本次获取的是master分支。
2.配置触发器
触发器就是定义何种条件下jenkins开始构建项目
这里选择轮询,*/1 * * * * 说明每隔一分钟就进行一次构建,这里只有在git仓库更新之后才起作用。也就是每分钟会去检测git仓库源码是否更新,不更新则不进行构建。
更多表达式的说明点击后面的?图标查看。
3.构建操作
这里选择运行两个shell脚本,脚本后面进行说明
jenkins项目配置完成。
编写Dockerfile文件
注意dockerfile位于主工程目录下。
POM文件配置
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- 指定该Main Class为全局的唯一入口 --> <mainClass>com.demo.web.WebApplication</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中--> </goals> </execution> </executions> </plugin>
<!-- docker 插件配置--> <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.4.10</version> <configuration> <repository>lgj/webdemo</repository> <tag>1.0</tag> <buildArgs> <JAR_FILE>/target/web-1.0.jar</JAR_FILE> </buildArgs> </configuration> </plugin> </plugins> </build>
说明:
dockerfile-maven-plugin ,maven 的docker插件
<repository>lgj/webdemo</repository>:镜像名称
<tag>1.0</tag>:镜像标签
一个镜像的完整标识是:镜像仓库/镜像名称:镜像标签
<JAR_FILE>/target/web-1.0.jar</JAR_FILE>:构建时的传入dockerfile的参数
dockerfile
#基于jdk8镜像进行构建,创建的镜像已经配置好java环境
FROM openjdk:8
#上述的pom中配置的JAR_FILE将会传入到该参数
#也可以在这里直接配置 ARG JAR_FILE=/target/web-1.0.jar
#用于配置spingboot应用maven打包生成的jar文件 ARG JAR_FILE
#在镜像中创建一个工作目录 run mkdir /worker
#将jar文件复制到工作目录中 add ${JAR_FILE} /worker
#暴露镜像的端口8452,其他端口不开放 expose 8452
#镜像启动时执行的命令,配置多条仅执行最后一条
#这里配置启动jar文件: java -jar /worker/web-1.0.jar entrypoint ["java","-jar","/worker/web-1.0.jar"]
构建脚本build.sh
用于根据dockerfile创建docker脚本
mvn clean mvn package -DskipTests docker rmi -f lgj/webdemo:1.0 mvn dockerfile:build docker images
先使用mvn进行打包
再删除旧的镜像,由于不能覆盖创建,因此需要删除再创建。
由于已经pom中已经配置docker插件,可以直接调用:mvn dockerfile:build 进行构建。
运行脚本run.sh
用于创建并运行docker容器
docker ps -a docker stop webdemo-8452 docker rm -f webdemo-8452 docker run -d --name webdemo-8452 -p 8452:8452 lgj/webdemo:1.0 docker ps -a
和创建镜像类似,运行之前必须先删除容器。
docker run 说明:
-d:以后台进程的方式运行。
--name :指定容器的名称为 webdemo-8452
-p : 指定主机和容器的端口映射。docker是基于主机运行的,数据--》主机端口--》容器端口。因此需要配置端口映射。一般配置成一样的。这里只需要访问springboot应用的8452,所以配置成8452.
lgj/webdemo:1.0: 镜像名称
所有文件编写完成后,重新pull到git仓库。
jenkins构建
点击项目页面的立即构建
可以看到下方的构建历史。
点击进去查看每次构建的具体情况。
点击控制台输出,可以查看构建过程的输出
构建过程:
1.拉取远端的git仓库文件到jenkins的工作目录/home/lgj/.jenkins/workspace/web-demo-docker
2. 执行配置的相关命令和操作。
输出 Finished: SUCCESS 则表示构建成功。
??? admin ?? ??? ????? /home/lgj/.jenkins/workspace/web-demo-docker ? No credentials specified > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url git@github.com:lgjlife/web-demo.git # timeout=10 Fetching upstream changes from git@github.com:lgjlife/web-demo.git > git --version # timeout=10 > git fetch --tags --force --progress git@github.com:lgjlife/web-demo.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision 074769c6e558772ddcaf133e92641995cbf3c7ef (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 074769c6e558772ddcaf133e92641995cbf3c7ef Commit message: "docker images" > git rev-list --no-walk 074769c6e558772ddcaf133e92641995cbf3c7ef # timeout=10 [web-demo-docker] $ /bin/sh -xe /tmp/jenkins5091262134747837972.sh + echo ======================================================================================= ======================================================================================= + pwd /home/lgj/.jenkins/workspace/web-demo-docker + ./build.sh [[1;34mINFO[m] Scanning for projects... [[1;34mINFO[m] [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] [1mBuilding web 1.0[m [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] [[1;34mINFO[m] [1m--- [0;32mmaven-clean-plugin:3.1.0:clean[m [1m(default-clean)[m @ [36mweb[0;1m ---[m [[1;34mINFO[m] Deleting /home/lgj/.jenkins/workspace/web-demo-docker/target [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] [1;32mBUILD SUCCESS[m [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] Total time: 2.595 s [[1;34mINFO[m] Finished at: 2019-06-02T13:07:18+08:00 [[1;34mINFO[m] Final Memory: 12M/188M [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] Scanning for projects... [[1;34mINFO[m] [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] [1mBuilding web 1.0[m [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] [[1;34mINFO[m] [1m--- [0;32mmaven-resources-plugin:3.1.0:resources[m [1m(default-resources)[m @ [36mweb[0;1m ---[m [[1;34mINFO[m] Using 'UTF-8' encoding to copy filtered resources. [[1;34mINFO[m] Copying 1 resource [[1;34mINFO[m] Copying 1 resource [[1;34mINFO[m] [[1;34mINFO[m] [1m--- [0;32mmaven-compiler-plugin:3.8.0:compile[m [1m(default-compile)[m @ [36mweb[0;1m ---[m [[1;34mINFO[m] Changes detected - recompiling the module! [[1;34mINFO[m] Compiling 2 source files to /home/lgj/.jenkins/workspace/web-demo-docker/target/classes [[1;34mINFO[m] [[1;34mINFO[m] [1m--- [0;32mmaven-resources-plugin:3.1.0:testResources[m [1m(default-testResources)[m @ [36mweb[0;1m ---[m [[1;34mINFO[m] Using 'UTF-8' encoding to copy filtered resources. [[1;34mINFO[m] skip non existing resourceDirectory /home/lgj/.jenkins/workspace/web-demo-docker/src/test/resources [[1;34mINFO[m] [[1;34mINFO[m] [1m--- [0;32mmaven-compiler-plugin:3.8.0:testCompile[m [1m(default-testCompile)[m @ [36mweb[0;1m ---[m [[1;34mINFO[m] No sources to compile [[1;34mINFO[m] [[1;34mINFO[m] [1m--- [0;32mmaven-surefire-plugin:2.22.1:test[m [1m(default-test)[m @ [36mweb[0;1m ---[m [[1;34mINFO[m] Tests are skipped. [[1;34mINFO[m] [[1;34mINFO[m] [1m--- [0;32mmaven-jar-plugin:3.1.0:jar[m [1m(default-jar)[m @ [36mweb[0;1m ---[m [[1;34mINFO[m] Building jar: /home/lgj/.jenkins/workspace/web-demo-docker/target/web-1.0.jar [[1;34mINFO[m] [[1;34mINFO[m] [1m--- [0;32mspring-boot-maven-plugin:2.1.0.RELEASE:repackage[m [1m(repackage)[m @ [36mweb[0;1m ---[m [[1;34mINFO[m] Layout: ZIP [[1;34mINFO[m] Replacing main artifact /home/lgj/.jenkins/workspace/web-demo-docker/target/web-1.0.jar [[1;34mINFO[m] [[1;34mINFO[m] [1m--- [0;32mspring-boot-maven-plugin:2.1.0.RELEASE:repackage[m [1m(default)[m @ [36mweb[0;1m ---[m [[1;34mINFO[m] Layout: ZIP [[1;34mINFO[m] Replacing main artifact /home/lgj/.jenkins/workspace/web-demo-docker/target/web-1.0.jar [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] [1;32mBUILD SUCCESS[m [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] Total time: 9.671 s [[1;34mINFO[m] Finished at: 2019-06-02T13:07:30+08:00 [[1;34mINFO[m] Final Memory: 32M/330M [[1;34mINFO[m] [1m------------------------------------------------------------------------[m Untagged: lgj/webdemo:1.0 [[1;34mINFO[m] Scanning for projects... [[1;34mINFO[m] [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] [1mBuilding web 1.0[m [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] [[1;34mINFO[m] [1m--- [0;32mdockerfile-maven-plugin:1.4.10:build[m [1m(default-cli)[m @ [36mweb[0;1m ---[m [[1;34mINFO[m] dockerfile: null [[1;34mINFO[m] contextDirectory: /home/lgj/.jenkins/workspace/web-demo-docker [[1;34mINFO[m] Building Docker context /home/lgj/.jenkins/workspace/web-demo-docker [[1;34mINFO[m] Path(dockerfile): null [[1;34mINFO[m] Path(contextDirectory): /home/lgj/.jenkins/workspace/web-demo-docker [[1;34mINFO[m] [[1;34mINFO[m] Image will be built as lgj/webdemo:1.0 [[1;34mINFO[m] [[1;34mINFO[m] Step 1/6 : FROM openjdk:8 [[1;34mINFO[m] [[1;34mINFO[m] Pulling from library/openjdk [[1;34mINFO[m] Digest: sha256:0e25c8428a56e32861fe996b528a107933155c98fb2a9998a4a4e9423aad734d [[1;34mINFO[m] Status: Image is up to date for openjdk:8 [[1;34mINFO[m] ---> 4a0a42e87cf3 [[1;34mINFO[m] Step 2/6 : ARG JAR_FILE [[1;34mINFO[m] [[1;34mINFO[m] ---> Using cache [[1;34mINFO[m] ---> dc2f959e4d9c [[1;34mINFO[m] Step 3/6 : run mkdir /worker [[1;34mINFO[m] [[1;34mINFO[m] ---> Using cache [[1;34mINFO[m] ---> 87e001b16580 [[1;34mINFO[m] Step 4/6 : add ${JAR_FILE} /worker [[1;34mINFO[m] [[1;34mINFO[m] ---> 7d6b791017fd [[1;34mINFO[m] Step 5/6 : expose 8452 [[1;34mINFO[m] [[1;34mINFO[m] ---> Running in f7989737ef59 [[1;34mINFO[m] Removing intermediate container f7989737ef59 [[1;34mINFO[m] ---> aa553eea45de [[1;34mINFO[m] Step 6/6 : entrypoint ["java","-jar","/worker/web-1.0.jar"] [[1;34mINFO[m] [[1;34mINFO[m] ---> Running in 7133f5214111 [[1;34mINFO[m] Removing intermediate container 7133f5214111 [[1;34mINFO[m] ---> 10e0d3d344d6 [[1;34mINFO[m] Successfully built 10e0d3d344d6 [[1;34mINFO[m] Successfully tagged lgj/webdemo:1.0 [[1;34mINFO[m] [[1;34mINFO[m] Detected build of image with id 10e0d3d344d6 [[1;34mINFO[m] Building jar: /home/lgj/.jenkins/workspace/web-demo-docker/target/web-1.0-docker-info.jar [[1;34mINFO[m] Successfully built lgj/webdemo:1.0 [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] [1;32mBUILD SUCCESS[m [[1;34mINFO[m] [1m------------------------------------------------------------------------[m [[1;34mINFO[m] Total time: 57.257 s [[1;34mINFO[m] Finished at: 2019-06-02T13:08:31+08:00 [[1;34mINFO[m] Final Memory: 25M/362M [[1;34mINFO[m] [1m------------------------------------------------------------------------[m REPOSITORY TAG IMAGE ID CREATED SIZE lgj/webdemo 1.0 10e0d3d344d6 2 seconds ago 504MB <none> <none> 67b2eee0f0b6 2 hours ago 504MB <none> <none> 88b1d5b63784 2 hours ago 504MB <none> <none> e60203504301 2 hours ago 504MB <none> <none> 11f593fd9537 2 hours ago 504MB openjdk 8 4a0a42e87cf3 3 days ago 488MB + ./run.sh CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d1a4fdf8f230 67b2eee0f0b6 "java -jar /worker/w���" 2 hours ago Up 2 hours 0.0.0.0:8452->8452/tcp webdemo-8452 webdemo-8452 7fe8d8d9cdff88f8281e8b0f6c8ff5e010844dd40b82829f5952d589c6d183b2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7fe8d8d9cdff lgj/webdemo:1.0 "java -jar /worker/w���" 2 seconds ago Up Less than a second 0.0.0.0:8452->8452/tcp webdemo-8452 Finished: SUCCESS
完成!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!