Jenkins基础:Jenkinsfile使用实例:14:使用docker.image进行容器部署

在上一篇文章中我们介绍了在Jenkinsfile中镜像推送至本地私库或者dockerhub的方法,这篇文章将继续介绍如何使用docker.image将容器运行起来。

环境准备

本文使用Easypack的LTS Jenkins 2.176.1版,环境准备请参看

  • https://liumiaocn.blog.csdn.net/article/details/92764938

获取Jenkins-Crumb

使用如下示例代码获取Jenkins-Crumb,为使用API方式为示例作准备。

liumiaocn:jenkins liumiao$ jenkins_host_url=http://localhost:32002
liumiaocn:jenkins liumiao$ user_passwd="root:liumiaocn"
liumiaocn:jenkins liumiao$ jenkins_crumb=`curl -u $user_passwd ${jenkins_host_url}'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)' 2>/dev/null`
liumiaocn:jenkins liumiao$ echo $jenkins_crumb
Jenkins-Crumb:6af0183fd58dea3d16f393b9e3272747
liumiaocn:jenkins liumiao$ 

创建Job

使用如下代码示例创建Job

liumiaocn:jenkins liumiao$ ls demo/pipeline/docker/deploy/config.xml
demo/pipeline/docker/deploy/config.xml
liumiaocn:jenkins liumiao$ cat demo/pipeline/docker/deploy/config.xml
<?xml version='1.1' encoding='UTF-8'?>
<flow-definition plugin="workflow-job@2.32">
  <actions>
    <org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction plugin="pipeline-model-definition@1.3.8"/>
  </actions>
  <description>Pipeline Job Sample</description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <hudson.plugins.jira.JiraProjectProperty plugin="jira@3.0.7"/>
    <com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="gitlab-plugin@1.5.12">
      <gitLabConnection></gitLabConnection>
    </com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>
  </properties>
  <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.69">
  <script>node {
       stage('Prepare Dockerfile') {
         sh 'cp /tmp/Dockerfile .' 
       }
       stage('Build with default Dockerfile') {
         docker.build('my-nginx:v1')
       } 
       stage('Deploy my-nginx image for running') {
         docker.image('my-nginx:v1').withRun('-p 8090:80') { c ->
           sh 'curl http://192.168.31.242:8090'
         }
       }
  }</script>
  <sandbox>false</sandbox>
  </definition>
  <triggers/>
  <disabled>false</disabled>
</flow-definition>
liumiaocn:jenkins liumiao$ curl -X POST -u $user_passwd -H ${jenkins_crumb} -H "Content-Type:application/xml"  --data-binary  "@demo/pipeline/docker/deploy/config.xml" ${jenkins_host_url}/createItem?name=pipeline_job_deploy
liumiaocn:jenkins liumiao$ echo $?
0
liumiaocn:jenkins liumiao$

Jenkinsfile说明

本文示例所使用的Jenkinsfile信息如下所示,内容非常简单易读,简单说明如下:

  • node是结构,在其中可以指定stage等信息
  • stage为具体的节点,比如本文示例中模拟实际的准备Dockerfile、构建镜像等操作
  • withRun中一般设定运行时的参数相关的信息,比如本例中的port的mapping信息
node {
       stage('Prepare Dockerfile') {
         sh 'cp /tmp/Dockerfile .' 
       }
       stage('Build with default Dockerfile') {
         docker.build('my-nginx:v1')
       } 
       stage('Deploy my-nginx image for running') {
         docker.image('my-nginx:v1').withRun('-p 8090:80') { c ->
           sh 'curl http://192.168.31.242:8090'
         }
       }
  }

执行Job

  • Dockerfile准备
    Dockerfile内容如下所示
liumiaocn:jenkins liumiao$ docker exec -it jenkins_jenkins_1 sh
/ # cat /tmp/Dockerfile 
FROM nginx:latest
/ # 
  • 事前确认
liumiaocn:jenkins liumiao$ docker images |grep my-nginx
liumiaocn:jenkins liumiao$ 

使用如下命令或者直接在Jenkins上点击构建

liumiaocn:jenkins liumiao$ curl -X POST -u $user_passwd -H ${jenkins_crumb} ${jenkins_host_url}/job/pipeline_job_deploy/build
liumiaocn:jenkins liumiao$ echo $?
0
liumiaocn:jenkins liumiao$

确认执行结果

使用如下命令可以确认相关的执行日志信息

liumiaocn:jenkins liumiao$ curl -u $user_passwd ${jenkins_host_url}/job/pipeline_job_deploy/1/consoleText
Started by user root
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /data/jenkins/workspace/pipeline_job_deploy
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Prepare Dockerfile)
[Pipeline] sh
+ cp /tmp/Dockerfile .
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build with default Dockerfile)
[Pipeline] sh
+ docker build -t my-nginx:v1 .
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM nginx:latest
latest: Pulling from library/nginx
8d691f585fa8: Pulling fs layer
047cb16c0ff6: Pulling fs layer
b0bbed1a78ca: Pulling fs layer
b0bbed1a78ca: Verifying Checksum
b0bbed1a78ca: Download complete
047cb16c0ff6: Verifying Checksum
047cb16c0ff6: Download complete
8d691f585fa8: Download complete
8d691f585fa8: Pull complete
047cb16c0ff6: Pull complete
b0bbed1a78ca: Pull complete
Digest: sha256:77ebc94e0cec30b20f9056bac1066b09fbdc049401b71850922c63fc0cc1762e
Status: Downloaded newer image for nginx:latest
 ---> 5a9061639d0a
Successfully built 5a9061639d0a
Successfully tagged my-nginx:v1
[Pipeline] dockerFingerprintFrom
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy my-nginx image for running)
[Pipeline] sh
+ docker run -d -p 8090:80 my-nginx:v1
[Pipeline] dockerFingerprintRun
[Pipeline] sh
+ curl http://192.168.31.242:8090
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
100   612  100   612    0     0   5884      0 --:--:-- --:--:-- --:--:--  5828
[Pipeline] sh
+ docker stop 26e33aff07bb6902511ba5ac070b580cf5c0cd9876386561430d456aee010e68
26e33aff07bb6902511ba5ac070b580cf5c0cd9876386561430d456aee010e68
+ docker rm -f 26e33aff07bb6902511ba5ac070b580cf5c0cd9876386561430d456aee010e68
26e33aff07bb6902511ba5ac070b580cf5c0cd9876386561430d456aee010e68
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
liumiaocn:jenkins liumiao$

blueocean构建结果确认

在这里插入图片描述

构建后镜像确认

liumiaocn:jenkins liumiao$ docker images |grep my-nginx
my-nginx                                        v1                              5a9061639d0a        2 days ago          126MB
liumiaocn:jenkins liumiao$ 

注意事项

环境准备的docker-compose.yml中需要包含如下内容,以保证能够以容器方式运行的Jenkins能够进行镜像的生成。
- /var/run/docker.sock:/var/run/docker.sock
为了保证Jenkins-Crumb能够正常获取,不要设定如下Jenkins收起启动跳过插件内容的设定,以保证API的正常执行。
JAVA_OPTS=-Djenkins.install.runSetupWizard=false

使用withRun,从结果可以看到容器生成、使用curl确认运行状况、容器销毁的整个过程。而如果希望此容器运行而不会被停止,则可以直接使用如下命令替换即可

docker.image('my-nginx:v1').run('-p 8090:80')

参考内容

https://jenkins.io/doc/book/pipeline/docker/

淼叔 CSDN认证博客专家 神经网络 TensorFlow NLP
资深架构师,PMP、OCP、CSM、HPE University讲师,EXIN DevOps Professional与DevOps Master认证讲师,曾担任HPE GD China DevOps & Agile Leader,帮助企业级客户提供DevOps咨询培训以及实施指导。熟悉通信和金融领域,有超过十年金融外汇行业的架构设计、开发、维护经验,在十几年的IT从业生涯中拥有了软件开发设计领域接近全生命周期的经验和知识积累,著有企业级DevOps技术与工具实战。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页