Building Shopping Cart

There are several ways to deploy Docker images to OpenShift:

  • An external Docker registry, such as one that your organisation has set up

  • A public Docker registry

  • OpenShift’s built in Docker registry

  • Minishift’s VM Docker host

For this guide we are going to use OpenShift’s Docker registry with OpenShift, but we also provide instructions below for using Minishift.

Docker environment variable

For convenience, we’ll refer to the registry-url using the environment variable DOCKER_REPO_URL, so if you set that in your shell like so:

DOCKER_REPO_URL=docker-registry-default.myopenshift.example.com

You will be able to copy and paste commands from this guide.

Using the OpenShift Docker registry

Docker registry tags are typically used to enforce permissions. That is, a Docker tag should have the format of <registry-url>/<username>/<image>:<version>. The internal OpenShift registry maps the username part of the tag to a project or namespace. So, if you want to deploy an image to the myproject project, then you need to use that in place of the <username> in the tag.

Before you can push to OpenShift’s built-in docker registry, you need to log into it. This can be done using your OpenShift login token, which you can discover at any time by running oc whoami -t. To log in, run:

oc whoami -t | docker login -u $(oc whoami) --password-stdin $DOCKER_REPO_URL

Using Minishift with Docker

Minishift makes it straightforward to build images directly into the Minishift VMs docker host. This is done by running eval $(minishift docker-env), which sets up a number of environment variables so that the docker command will use that instead of the docker host on your host machine.

If you build your image directly in the Minishift VMs docker host, then technically, you don’t need to push to the OpenShift registry. The images are already in the Minishift VMs docker host ready to be run. However, if your deployment spec has an imagePullPolicy of Always, as is the default when using the latest tag, then regardless of whether the image is there or not, Kubernetes will first attempt to pull the image, and this will fail if you haven’t pushed it to any registry that it can see yet. For this reason, if using Minishift, we will still do the push step into the OpenShift built in registry, this has the advantage of being able to run the guide in a more realistic setup.

You still will need to setup the Minishift docker environment variables before you run this, since by default, the built in OpenShift docker registry is not exposed to your host machine, only the VM can see it.

The Minishift docker repository can be obtained by minishift openshift registry, so setup your Minishift environment, and initialize the DOCKER_REPO_URL environment variable to that:

eval $(minishift docker-env)
DOCKER_REPO_URL=$(minishift openshift registry)

What’s next

The next steps depend on whether you are using sbt or Maven: