In this article we’re going to look at how to build an image in OpenShift using a Docker build, where your build instructions are in a Dockerfile.
Build an image using a Dockerfile in your application’s Git repository
Keeping your Dockerfile in a Git repository is a great option.
It’s easier to manage everything when you keep your build scripts in the same repository as your code.
Plus, you also get the advantage of being able to version-control your Dockerfile, just like your application’s code.
To use a Dockerfile from a Git repository, you need to specify
Docker, and then use the
dockerStrategy.dockerfilePath field to give the path to your Dockerfile.
Here’s an example:
apiVersion: build.openshift.io/v1 kind: BuildConfig metadata: name: mywebsite labels: app: mywebsite spec: source: type: Git git: uri: https://github.com/monodot/container-up contextDir: httpd-hello-world strategy: type: Docker dockerStrategy: dockerfilePath: Dockerfile # Look for Dockerfile in: gitUri/contextDir/dockerfilePath output: to: kind: ImageStreamTag name: mywebsite:latest
Docker build with an image stream as a FROM image
I like image streams, they’re a nice feature of OpenShift. They allow you to create a local “pointer” to a set of image tags.
When you use an image stream, you don’t need to hardcode the full registry URL everywhere, including your BuildConfig.
OpenShift will resolve the image and use it as the from image in your Docker build.
When you use an image stream in a BuildConfig, you can also set an image change event as a trigger – for example, to trigger a new build when a new image is pushed to a tag.
First, let’s set up an image stream to use as an example.
I create a local imagestream called
httpd, which points to Bitnami’s Apache image on Docker Hub. Here’s an example ImageStream YAML definition for that:
apiVersion: image.openshift.io/v1 kind: ImageStream metadata: name: httpd spec: lookupPolicy: local: false tags: - name: latest from: kind: DockerImage name: bitnami/apache referencePolicy: type: Source
Next, create the BuildConfig, to perform a Docker build in OpenShift.
The image stream details go in the
.spec.strategy.dockerStrategy.from section, like this example:
apiVersion: build.openshift.io/v1 kind: BuildConfig metadata: name: mywebsite labels: app: mywebsite spec: source: type: Git git: uri: https://github.com/monodot/container-up contextDir: httpd-hello-world strategy: type: Docker dockerStrategy: dockerfilePath: Dockerfile from: kind: ImageStreamTag # Use an imagestream tag as 'FROM' namespace: toms-project # Which project? name: httpd:latest # Use the imagestream created above output: to: kind: ImageStreamTag name: mywebsite:latest triggers: - type: ImageChange # Trigger a build on image change
Now, when the build runs, OpenShift will replace the
FROM line in your Dockerfile with the image from the image stream.
In the logs, you can see OpenShift replacing the FROM instruction:
Docker build with an inline Dockerfile
The final option I want to show you is writing a Dockerfile inside the BuildConfig itself; known as an inline Dockerfile.
You can include a complete Dockerfile inside the BuildConfig object.
When this approach is combined with
spec.source.type=Git, OpenShift will check out code from your Git repository,
cd into the directory, then run a Docker build using your inline Dockerfile.
You can use a
| pipe symbol in YAML to mark the start of a multiline string with line breaks, so you can write a complete Dockerfile.
Here’s the example:
apiVersion: build.openshift.io/v1 kind: BuildConfig metadata: name: mywebsite spec: source: type: Git git: uri: https://github.com/monodot/container-up # clone this code contextDir: httpd-hello-world # cd to this directory dockerfile: | FROM bitnami/apache:latest COPY ./public_html/ /app strategy: type: Docker output: to: kind: ImageStreamTag name: mywebsite:latest
The syntax looks a little weird, but this trick allows you to create a custom Docker build, without needing to store the Dockerfile in a Git repository.
Thanks for reading! I hope you found these example BuildConfigs useful.
Docker builds are an alternative to source-to-image (S2I) builds and you’ll probably find them useful when you’ve already written an app with a Dockerfile, and you simply want to move your app into OpenShift.
Once you’ve built your image, how about looking at some Kubernetes Deployment examples?