docker - Jenkins: Connect to a Docker container from a stage that is run with an agent (another Docker container)
问题描述
I am in the process of reworking a pipeline to use Declarative Pipelines approach so that I will be able to use Docker images on each stage.
At the moment I have the following working code which performs integration tests connecting to a DB which is run in a Docker container.
node {
// checkout, build, test stages...
stage('Integration Tests') {
docker.image('mongo:3.4').withRun(' -p 27017:27017') { c ->
sh "./gradlew integrationTest"
}
}
Now with Declarative Pipelines the same code would look somehow like this:
pipeline {
agent none
stages {
// checkout, build, test stages...
stage('Integration Test') {
agent { docker { image 'openjdk:11.0.4-jdk-stretch' } }
steps {
script {
docker.image('mongo:3.4').withRun(' -p 27017:27017') { c ->
sh "./gradlew integrationTest"
}
}
}
}
}
}
Problem: The stage is now run inside a Docker container and running docker.image()
leads to docker: not found
error in the stage (it is looking for docker inside the openjdk
image which is now used).
Question: How to start a DB container and connect to it from a stage in Declarative Pipelines?
解决方案
What essentially you are trying is to use is DIND.
You are using a jenkins slave that is essentially created using docker agent { docker { image 'openjdk:11.0.4-jdk-stretch' } }
Once the container is running you are trying to execute a docker command. the error docker: not found
is valid as there is no docker cli installed. You need to update the dockerfile/create a custom image having openjdk:11.0.4-jdk-stretch and docker dameon installed.
Once the daemon is installed you need to volume mount the /var/run/docker.sock so that the daemon will talk to the host docker daemon via socket.
The user should be root or a privileged user to avoid permission denied issue.
推荐阅读
- sql - 过滤结果以仅显示一列中具有重复记录的结果
- regex - 具有多个级别的 Countif 和 ArrayFormula
- c++ - 如何从向量中正确返回 unique_ptr?
- python - How do you add with variables in python
- php - 为什么 PDO fetchAll() 两次返回相同的结果?
- batch-file - 如何处理字符串搜索/替换中的特殊字符(<、>、"、=)
- apache-spark - 如何从 Spark 数据框中随机选择行,而基于列的条件也必须成立
- ios - Xcode -FirebaseCore 词法或预处理器问题“GoogleUtilities/GULAppEnvironmentUtil.h”文件未找到
- java - 数据流和 Bigtable 依赖冲突
- laravel - 如何在 laravel 中将数据从视图发送到控制器