【问题标题】:Deploy/run the app from Jenkins pipeline with agent docker使用代理 docker 从 Jenkins 管道部署/运行应用程序
【发布时间】:2019-08-22 03:29:48
【问题描述】:

我正在尝试构建/测试/部署示例(节点)应用程序的示例管道(使用声明性)。

我正在使用 agent docker 在容器中运行作业...配置和测试阶段工作正常:

pipeline {
  agent {
    docker { image 'node:latest' }
  }
  stages {
    stage('config') {
      steps {
        sh 'npm install'
      }
    }
    stage('run test') {
      steps {
        sh 'npm test'
      }
    }

  }

问题是如何添加deployment 阶段本质上是构建一个 docker 映像并像这样运行它:

docker build -t myapp .
docker run -d myapp

(假设这是我想要部署/运行应用程序的方式......并拥有一个 Dockerfile)

问题是如何部署示例应用程序(使用 docker 命令)...在此处添加 deploy 阶段将不起作用,因为我使用的是 docker agent(我猜它在 docker 内运行 docker,这听起来错了!)

我是否应该开始一项新工作 - 在管道成功后 - 使用 agent any 运行部署命令?还是有更好的方法来运行应用程序?

【问题讨论】:

    标签: docker deployment jenkins-pipeline dockerfile jenkins-declarative-pipeline


    【解决方案1】:

    Docker 里面的 Docker 如果做对了就不会错! 您可以在容器节点上挂载父套接字:

    /var/run/docker.sock:/var/run/docker.sock
    

    在 Jenkins 中,转到 Manage -> Configure System -> 滚动到 Cloud -> 点击 Docker Agent templates -> Container settings...

    您会看到如下图所示的内容:

    为代理使用这样的图像:

    FROM jenkins/jnlp-slave
    USER root
    RUN apt-get update
    RUN apt-get -y  install \
                       apt-transport-https \
                       ca-certificates \
                       curl \
                       gnupg2 \
                       software-properties-common
    RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
    RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian stretch stable"
    RUN apt-get update
    RUN apt-get -y  install \
                   docker-ce \
                   docker-ce-cli \
                   containerd.io
    

    并在作业中执行 docker 使用:

    withDockerContainer(image: IMAGE_NAME)
    {
        ...
    }
    

    有用的链接:

    https://adamcod.es/2017/08/19/docker-patterns-socket-mount.html https://docs.docker.com/v17.09/engine/reference/commandline/dockerd/#examples

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 2019-08-04
      • 2017-06-14
      相关资源
      最近更新 更多