Ever woken up in the middle of the night and pondered how can I push and pull Docker images between OpenShift and external repository such as Docker Hub? Well, rest easy, your night sweats are over. This lab is another in the OpenShift MiniLabs series. Now say hello to “Billie”.
Objective
This lab will step you through a demonstration of the portability and interoperability of Docker image assets built either inside or outside of OpenShift. We will do this by building an application and then running that inside or outside OpenShift from an image sourced from either a local or external Docker repository.
Setup
Initial Attempt
My start script to launch a local OpenShift cluster instance looks as follows. Clone down the git repo suggested as it has the Dockerfiles to build the various assets required. Make sure you have an oc client at version 3.6 or later.
$ cd ~/MLOps
$ git clone https://bitbucket.org/emergile/MLOps.git
$ oc version
$ cat start.sh
export HOME=/Users/stefanopicozzi
export PROFILE=MLOps
echo $HOME
echo $PROFILE
oc cluster up --metrics \
--public-hostname='127.0.0.1' \
--host-config-dir=$HOME/.oc/profiles/$PROFILE/config \
--use-existing-config=true
$ ./start.sh
Repeat Attempt
To reset your environment to repeat this tutorial do the following. Create yourself a home location such as suggested and a project called mlops if it doesn’t already exist. Remove the Docker images as suggested so you have a clean slate to start with.
$ mkdir ~/MLOps $ cd ~/MLOps $ rm -rf MLOps $ git clone https://bitbucket.org/emergile/MLOps.git $ oc login -u developer -p developer $ oc project mlops $ oc delete all -l name=tensorflow $ docker rmi -f $(docker images | grep "tensorflow" | awk '{print $3}') | grep -v "IMAGE"
Instructions
We are going to demonstrate the commands to push across registries by doing the following:
- Assign yourself the appropriate OpenShift registry privileges
- Pull down a remote image to your local Docker image
- Push that local image to your OpenShift registry
- Push the OpenShift image to a remote Docker Hub
1. Add Registry Policies
Form and record the OpenShift registry end point, e.g. 172.30.1.1:5000 as we will reference it later as $REGISTRY. The “oc policy” commands need only be executed once.
$ oc login -u system:admin $ oc get services -n default | grep registry $ oc policy add-role-to-user system:registry developer $ oc policy add-role-to-user admin developer -n mlops $ oc policy add-role-to-user system:image-builder developer
2. Pull Docker image to Local Registry
Let’s start by first pulling down a Docker image outside of OpenShift to your local Docker installation. Inspect the generated and image and verify it can run by testing the supplied samples.
$ cd ~/MLOps
$ docker pull tensorflow/tensorflow
$ docker images | grep tensorflow
$ docker run -it -p 8888:8888 tensorflow/tensorflow
3. Push Local Docker image to OpenShift Registry
Now login to the OpenShift registry using the $REGISTRY you captured earlier. Tag and push the local Docker image to the OpenShift registry. Verify by creating an app based on this image and then accessing it from a Browser using the generated route URL. Test the supplied samples.
$ oc login -u developer -p developer $ docker login -u $(oc whoami) -p $(oc whoami -t) $REGISTRY $ docker tag tensorflow/tensorflow $REGISTRY/mlops/tensorflow $ docker push $REGISTRY/mlops/tensorflow $ docker images | grep tensorflow $ oc project mlops $ oc new-app mlops/tensorflow -l name=tensorflow --name=tensorflow $ oc expose service tensorflow --port=8888
4. Push OpenShift Image to Remote Docker Hub
Let’s push our OpenShift image to our Docker Hub account. Login using your Docker credentials and then tag and push. Visit https://hub.docker.com and search to verify that your image is present.
$ oc login -u developer -p developer $ docker login -u stefanopicozzi $ docker tag $REGISTRY/mlops/tensorflow mlops/tensorflow $ docker push mlops/tensorflow
Trivia
Have more registry fun at https://blog.openshift.com/getting-started-docker-registry/.