使用 Jenkins 构建 Node.js 和 React.js

Building Node.js and React.js using Jenkins

我正在关注 Jenkins 网站上的官方 tutorial。根据教程,我有一个 blueocean Docker 容器,它是 运行 Jenkinsfile 中的管道:

pipeline {
    agent {
        docker {
            image 'node:6-alpine' 
            args '-p 3000:3000' 
        }
    }
    stages {
        stage('Build') { 
            steps {
                sh 'npm install' 
            }
        }
    }
}

问题是管道在尝试拉取 Docker 图像时失败:

[ode-js-react-npm-app_master-6PEWX3VWDA4SAdDMJA4YKJCZSABJSAQCSGVYMKHINXGDDJLA] Running shell script
+ docker pull node:6-alpine
Warning: failed to get default registry endpoint from daemon (Cannot connect to the Docker daemon. Is the docker daemon running on this host?). Using system default: https://index.docker.io/v1/
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
script returned exit code 1

经过一些故障排除后,我意识到这是失败的,因为 Jenkins 试图拉取 Docker 容器本身,而不是主机。这不是我想要的,文档实际上指出:

This image parameter (of the agent section’s docker parameter) downloads the node:6-alpine Docker image (if it’s not already available on your machine) and runs this image as a separate container. This means that: You’ll have separate Jenkins and Node containers running locally in Docker. The Node container becomes the agent that Jenkins uses to run your Pipeline project. However, this container is short-lived - its lifespan is only that of the duration of your Pipeline’s execution.

有人可以解释我做错了什么以及为什么尝试将 Node.js Docker 图像拉入 Jenkins 而不是本地机器吗?我想要一个独立于编排应用程序的 Nodejs 容器的 Jenkins 容器。

我通过 运行 容器作为 root 解决了这个问题,否则它将无法访问 Docker 守护程序套接字 /var/run/docker.sock ...