Search code examples
gitdockerjenkinsdockerfilelocal

Create a docker container inside a jenkins-container with needed files in a local directory or git repository in order to build the container


I am currently trying to run a jenkins-pipeline for building a docker-container out of a git repository.

However, the problem is, that the dockerfile in the repository needs special files (installer, licens-keys as txt.files and so on) which are technically provided in the repository. So my question is, how am I able to tell the docker-agent in the pipeline, that there is a local directory in which this files are located, or even tell it to pull these files from the repository and use them.

I know, that you are able to clone a repository, which might be a solution for the problem with getting the needed files, so I can build my project out of a local dockerfile instead, but I do not know how to tell jenkins to use a local dockerfile. The only thing I know is, how to specify a dockerfile in a repository.

Or is it possible, that jenkins uses the files directly out of the repository? Private repository by the way, but that should not be a problem, if I use my credentials in the jenkins-file.

The jenkins container has docker installed and the docker-socket is mounted to the host.

I appreciate every answer. Thank you


Solution

  • Yes, you can checkout your repository and then build your Docker images using the files in that repository. If your pipeline file is in the same repository and the Pipeline has been set up for using the Jenkinsfile from the repository, then the following works:

    node {
        checkout scm
        def myImage = docker.build("my-image:${env.BUILD_ID}")
        myImage.push()
    }
    

    If you need to checkout any other repositories, you can use the Pipeline Snippet generator to generate the correct pipeline syntax for you.

    Documentation on Building Docker images (note that the documentation uses the Scripted Pipeline syntax): https://www.jenkins.io/doc/book/pipeline/docker/#building-containers