How to add docker client to Jenkins

There are times you need to build docker images inside Jenkins. Well, actually that a common practice nowadays. However, without setting up docker client properly, you might now be able to do so.

When running Jenkins without docker client, you may encounter errors like this:

java.io.IOException: error=2, No such file or directory
	at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
	at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:340)
	at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:271)
	at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
Caused: java.io.IOException: Cannot run program "docker": error=2, No such file or directory
	at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
	at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
	at hudson.Proc$LocalProc.<init>(Proc.java:254)
	at hudson.Proc$LocalProc.<init>(Proc.java:223)
	at hudson.Launcher$LocalLauncher.launch(Launcher.java:997)
	at hudson.Launcher$ProcStarter.start(Launcher.java:509)
	at hudson.Launcher$ProcStarter.join(Launcher.java:520)
	at org.jenkinsci.plugins.docker.commons.impl.RegistryKeyMaterialFactory.materialize(RegistryKeyMaterialFactory.java:101)
	at org.jenkinsci.plugins.docker.workflow.AbstractEndpointStepExecution2.doStart(AbstractEndpointStepExecution2.java:53)
	at org.jenkinsci.plugins.workflow.steps.GeneralNonBlockingStepExecution.lambda$run$0(GeneralNonBlockingStepExecution.java:77)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)

This post’s only purpose is to help you setup docker client correctly inside a Jenkins container so you can start building your images right away.

Step 1: Download the Docker client

There is a dedicated page to download the Docker client. Since I’m using Jenkins lts and it’s running Debian, I can get all versions here:

https://download.docker.com/linux/static/stable/x86_64/

Choose your version and copy the link.

Step 2: Install Docker client for Jenkins container

Now, with the link, exec into Jenkins container:

docker exec -it jenkins bash

Next, use wget to download the docker binary. If you don’t have wget installed, simply run:

apt install wget

And download the archive file:

wget https://download.docker.com/linux/static/stable/x86_64/docker-20.10.9.tgz

Then extract it:

tar -xf docker-20.10.9.tgz

Now, it will extract a folder called docker. However, you just need one binary file named docker inside that folder.

The last step is to move that file to /usr/local/bin (Create this folder if it doesn’t exist)

mv ./docker/docker /usr/local/bin

Now Jenkins can build docker images without problems:

Build docker inside jenkins sucessfully

Conclusion

With a few very simple steps, you can now build docker images inside Jenkins.


Leave a Reply

Your email address will not be published.