[英]How can I create a docker container from a Dockerfile in github inside a Jenkins pipeline?
I have a Dockerfile in a github repo. 我在github存储库中有一个Dockerfile。 I need to setup my workspace in a certain way and for this I would like to use the Dockerfile.
我需要以某种方式设置我的工作区,为此,我想使用Dockerfile。
I cannot seem to find any clear example of a Jenkinsfile that will create the container as a separate stage of the pipeline. 我似乎找不到Jenkinsfile的任何清晰示例,它将创建容器作为管道的单独阶段。
How do I do this? 我该怎么做呢?
Given you're using pipeline (jenkinsfile) syntax, this is what building a docker image looks like (the simplest way of doing it): 鉴于您使用的是管道(jenkinsfile)语法,这就是构建docker镜像的样子(最简单的实现方式):
stage('Build Docker Image')
{
DOCKER_TAG = "my_tag"
docker.withRegistry("${my_docker_registry}")
{
sh "docker build -t ${DOCKER_IMAGE}:${DOCKER_TAG}"
}
}
If you want to build the container then test it, you will need to store a reference to the container, and you can do that with the following code: 如果要构建容器然后对其进行测试,则需要存储对该容器的引用,并且可以使用以下代码进行操作:
stage('Build Docker Image')
{
DOCKER_TAG = "my_tag"
docker.withRegistry("${my_docker_registry}")
{
var my_image = docker.build("${DOCKER_IMAGE}:${DOCKER_TAG}")
my_image.inside
{
sh "echo 'Hello World'"
}
my_image.push()
}
}
Note that if you are using a docker image to run your pipeline, you will need to expose the docker daemon through to the image to build your image (there is an "image all the way down" joke here somewhere...) 请注意,如果您使用的是docker映像来运行管道,则需要将docker守护程序公开到该映像以构建映像(此处某处有一个“映像一直向下”的笑话...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.