tamasgal
tamasgal

Reputation: 26329

Jenkinsfile with Dockerfile in a specific folder

This is driving me nuts: I am trying to build a Docker image from a Dockerfile in a specific folder of my repository using a scripted Jenkins Pipeline, but it doesn't get the correct path.

This is the line in the Jenkinsfile:

def customImage = docker.build("km3pipe:${env.BUILD_ID}",
                               "-f ${dockerfile} ${DOCKER_FILES_DIR}")

As you can see in the Jenkinslog below, the variables are correctly resolved (I also double checked the workspace, everything is fine), but it seems to ignore the second argument .dockerfiles:

[py365] + docker build -t km3pipe:13 -f py365 ./dockerfiles
[py365] unable to prepare context: unable to evaluate symlinks in Dockerfile path: 
        lstat /var/lib/jenkins/workspace/f-docker-image-names-for-ci-2DCL5RJ7AMH7K
        IB6OCBTD57EC5DUGYZTQE5EQ6KGOSPXMUOVJP6Q/py365: no such file or directory

According to the docs (https://jenkins.io/doc/book/pipeline/docker/), this should work:

It is possible to pass other arguments to docker build by adding them to the second argument of the build() method. When passing arguments this way, the last value in the that string must be the path to the docker file.

This example overrides the default Dockerfile by passing the -f flag:

node {
    checkout scm
    def dockerfile = 'Dockerfile.test'
    def customImage = docker.build("my-image:${env.BUILD_ID}", "-f ${dockerfile} ./dockerfiles") 
}

Builds my-image:${env.BUILD_ID} from the Dockerfile found at ./dockerfiles/Dockerfile.test.

Upvotes: 3

Views: 7380

Answers (1)

Ignacio Millán
Ignacio Millán

Reputation: 8066

It is saying that there is no file named py365 in this folder. You're right the docker.build step is not using the path to run the build.

The documentation missed that you must include the directory in the dockerfile statement:

def customImage = docker.build("km3pipe:${env.BUILD_ID}",
                               "-f ${DOCKER_FILES_DIR}/${dockerfile} ${DOCKER_FILES_DIR}")

Upvotes: 5

Related Questions