简体   繁体   English

如何在Jenkins管道中从github中的Dockerfile创建Docker容器?

[英]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.

相关问题 将配置文件从 Dockerfile 插入到声明性 Jenkins 管道 Docker 容器中 - Insert a config file into a declarative Jenkins pipeline Docker container from Dockerfile 如何创建子域以公开运行到Jenkins管道中的docker容器中的应用程序 - How can I create a subdomain to expose an application running into a docker container in Jenkins pipeline 如何在 jenkins 管道内的 docker 构建命令上指向具体的 Dockerfile? - How to point on the concrete Dockerfile for the docker build command inside the jenkins pipeline? 如何使用 Jenkins 管道从 docker-compose 创建 docker 映像? - How can I use a Jenkins pipeline to create a docker image from docker-compose? 如何在基于 dockerfile 的容器中运行 Jenkins 管道? - How to run Jenkins pipeline in a container based on a dockerfile? 如何从 Azure 容器注册表中的映像创建 Dockerfile? - How can I create a Dockerfile FROM an Image in Azure Container Registry? Jenkins 管道如何获取 docker 容器内执行的测试结果? - How can the Jenkins pipeline fetch the test results executed inside docker container? 如何从 docker-compose 文件创建 DockerFile - How can I create DockerFile from docker-compose file 詹金斯管道的docker容器内的sudo权限 - sudo permission inside docker container for jenkins pipeline 如何使用 mysql 和 php docker 容器创建 jenkins 管道 - How to create jenkins pipeline with mysql and php docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM