Sid
Sid

Reputation: 181

How to use custom Dockerfile name in Jenkins pipeline?

I am using Jenkins to build and publish the docker image. However I need to use two docker file in one application just for my use case.

Requirement is can I build and published two different docker image from two different Docker file from one source, or i need to use custom file name like argument *-f* in docker command so that I can build two pipeline, in Jenkins?

By default Jenkins picks the file with *Dockerfile*

ex: docker build -t dockerfile -f Dockerfile-custom-name .

Upvotes: 2

Views: 3856

Answers (1)

Infinite Possibilities
Infinite Possibilities

Reputation: 7476

def projectImage = docker.build("imageName:tag", "-f Dockerfile-custom-name .")

This way you can even add --build-arg or any other argument before the -f. It is important to have the -f and . at the end of the second parameter.

Here's the documentation for it: Builds test-image from the Dockerfile found at ./dockerfiles/test/Dockerfile.


Edit (answer to comments):

Yes, my first solution proposal works in a Jenkins pipeline.

This should also work: sh '''docker build -t docker1-tag:latest us.gcr.io/project-name/docker1-tag:latest -f "Dockerfile1" .'''

The only downside of this one, is that you don't have the image in a variable, so you cannot use the run, withRun, push and other methods.

In your specific case, I would build the image with the name/tag of us.gcr.io/project-name/docker1-tag:latest, so I can call the push method and the image will be pushed to the registry. To have the second tag, a separate sh '''docker tag us.gcr.io/project-name/docker1-tag:latest docker1-tag:latest''' call would suffice.

Upvotes: 5

Related Questions