Search code examples
dockerjenkinsdockerfilejenkins-pipelinedocker-build

Jenkins: Pipeline Docker Job with Jenkins file - Docker groovy syntax


I'm an issue finding any proper example of how to build, register, and run a docker image via a jenkinsfile for a Pipeline job. Here is what I have so far:

node {
    def myService

    stage('Checkout Project') {
        checkout scm
    }

    stage('Build Binary') {
        sh "mvn package -f pom.xml -Dmaven.test.skip=true"
    }

    stage('Build Image') {
        sh "pwd"
        myService = docker.build('myService -f ${pwd}/Dockerfile')
    }

    stage('Test Image') {
        sh "echo Tests Passed :)"
    }

    stage('Run uShip Docker Container') {
        myService.run()
    }
}

first issue I'm is that when the docker.build() fires, it cannot find my docker file which I find odd because the command runs the same directory of the Dockerfile. Next I cannot any consistent example of how to register, push, and run the image. Any help would be greatly appreciated!

UPDATE: Well fixed the issue with docker not being able to locate the docker file. It was a naming issue; my docker file was names "DockerFile" instead of "Dockerfile". Renamed and it worked, but now I'm getting another error:

Invalid repository name (myService), only [a-z0-9-_.] are allowed

Not sure what's this is referring to.

UPDATE: In addition to my initial question, does a docker-compose file have to be of type .yml or can it be a .properties file?


Solution

  • The docker.build command expects a image name with an optional tag as parameter.

    From the documentation:

    Name components may contain lowercase letters, digits and separators. A separator is defined as a period, one or two underscores, or one or more dashes.

    So the name myService is invalid because of the uppercase S

    Also, the docker.build command looks for a Dockerfile on the same location, so you don't need the -f ${pwd}/Dockerfile on the parameter

    docker.build('myservice') should work