Build/Create Docker Images Continuously with Jenkins
When using Docker to deploy an application, it is best to rebuild the application continuously — for example whenever the application is rebuilt. This way, one’s integration servers can be automatically up-to-date, and run end-to-end tests on the latest code.
Docker has great positive influence on development processes and continuous integration of code, in this sense.
The shortage of a formal instructions guide to creating Docker images caused me to waste more time than it should’ve been, on setting up this kind of important (but simple) process. For that reason, I decided to write it on my own, in hopes it will save the next person some time.
One-time preparations
Begin by installing the “docker-build-step” Jenkins plugin.
Installing Docker engine
One option is to have Jenkins automatically manage your Docker engine setup. For that, go to Jenkins’ “Global Tool Configurations” and choose to automatically install the latest Docker version from docker.io (“Install latest from docker.io”).

An alternative is to manually install Docker engine on your Jenkins Nodes.
For that, you will need to login to your relevant Jenkins Nodes, and install the engine (instructions here — choose your Node’s setup).
Allow Jenkins to control Docker engine
This is the tricky part, that misconfiguring can cause a lot of pain. Jenkins needs to run the Docker commands (such as build
) with the Docker engine. So, to have Jenkins control the Docker engine, you will need to go to Jenkins’ “Configure System” and there scroll down to “Docker Builder” section.

For the “Docker URL” field, you will need to fill in a TCP URL.
If you let Jenkins install Docker engine automatically on your Nodes, then the following should work: tcp://127.0.0.1:4243
or tcp://127.0.0.1:2375
(Use “Test Connection” button to see which works).
If you installed Docker engine manually, or the two previous options won’t work, you will need to configure the Docker engine service to allow this port.
As a super user, run: systemctl edit docker
Add the following text to the [Service]
section:
ExecStart=/usr/bin/docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
Save, and exit the editor. Then, run the following commands to refresh the configurations in the Docker engine service:
systemctl daemon-reload
systemctl restart docker
Now back on Jenkins put tcp://<node_ip>:2375
on the “Docker URL” field, and test that the configurations took affect, with “Test Connection” button. If all still won’t work — check your firewall settings.
More on ExecStart configurations:
https://success.docker.com/KBase/Using_systemd_to_control_the_Docker_daemon
The build job
Create a new freestyle Jenkins job.
Sync your Dockerfile code from your source-control (at least that’s my recommendation), and download all your Dockerfile requirements (e.g. your application artifact) from your repository manager (e.g. Artifactory).
On the “Build” section, add the “Execute Docker command” build-step.
Fill in all the relevant information (the context being where the Dockerfile requirements exist — mostly $WORKSPACE
).
Run the job.
Fixing “Build step failed with exception java.lang.NullPointerException: uri was not specified”
If you get the following exception on your build process, it means you misconfigured Jenkins control over the Docker engine. Please retry those steps.
ERROR: Build step failed with exception
java.lang.NullPointerException: uri was not specified
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:204)
at com.github.dockerjava.core.DockerClientConfig$DockerClientConfigBuilder.withUri(DockerClientConfig.java:406)
at org.jenkinsci.plugins.dockerbuildstep.DockerBuilder$DescriptorImpl.createDockerClient(DockerBuilder.java:123)
at org.jenkinsci.plugins.dockerbuildstep.DockerBuilder$DescriptorImpl.getDockerClient(DockerBuilder.java:201)
at org.jenkinsci.plugins.dockerbuildstep.DockerBuilder.perform(DockerBuilder.java:71)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
at hudson.model.Build$BuildExecution.build(Build.java:205)
at hudson.model.Build$BuildExecution.doRun(Build.java:162)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:537)
at hudson.model.Run.execute(Run.java:1744)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:374)
Build step 'Execute Docker container' marked build as failure
More reading on this exception:
https://stackoverflow.com/questions/30046507/using-docker-plugin-on-jenkins
https://stackoverflow.com/questions/38204371/jenkins-and-docker-uri-was-not-specified
https://stackoverflow.com/questions/38497408/jenkins-with-docker-plugin-java-lang-nullpointerexception-uri-was-not-specified