This blog will cover how to deploy an application from an existing container image using
oc in the command line for OpenShift. The first part of the series covered deploying from the web console UI.
Deleting the Application
Instead of deploying the existing container image from the web console, you can use the command line. Before doing that, let’s delete the application we have already deployed.
To do this from the web console, you could visit each resource type created, and delete them one at a time. The simpler way to delete an application is from the command line, using the
To see a list of all the resources that have been created in the project so far, you can run the command:
oc get all -o name
This will display output similar to the following:
imagestreams/blog-django-py deploymentconfigs/blog-django-py replicationcontrollers/blog-django-py-1 routes/blog-django-py services/blog-django-py pods/blog-django-py-1-9fshs
If you have only created one application, you would know that all the resources listed are related to it. When you have multiple applications deployed, you need to identify those which are specific to the application that you’re deleting. You can do this by applying a command to a subset of resources using a label selector.
To determine which labels have been added to the resources, select one and display the details on it. To look at the Route which was created, run the following command:
oc describe route/blog-django-py
This should display output similar to:
Name: blog-django-py Namespace: myproject Created: 17 minutes ago Labels: app=blog-django-py Annotations: openshift.io/host-generated=true Requested Host: blog-django-py-myproject.2886795279-80-ollie02.environments.katacoda.com exposed on router router 17 minutes ago Path: <none> TLS Termination: <none> Insecure Policy: <none> Endpoint Port: 8080-tcp Service: blog-django-py Weight: 100 (100%) Endpoints: 172.18.0.3:8080
In this case, when deploying the existing container image via the OpenShift web console, OpenShift has applied automatically to all resources the label
app=blog-django-py. You can confirm this by running this command:
oc get all --selector app=blog-django-py -o name
Having a way of selecting just the resources for the one application, you can now schedule them for deletion by running this command:
oc delete all --selector app=blog-django-py
To confirm that the resources have been deleted, run again the command:
oc get all -o name
If you still see any resources listed for the application, keep running this command until it shows they have all been deleted. You may find that the resources may not have been deleted immediately when you only schedule them for deletion, and how quickly they can be deleted will depend upon how quickly the application can be shut down.
Although label selectors can be used to qualify what resources are being queried or deleted, be aware that it may not always be the
app label that you need to use. When an application is created from a template, the labels applied and their names are dictated by the template. As a result, a template may use a different labeling convention. Always use
oc describe to verify what labels have been applied and use
oc get all --selector to verify what resources are matched before deleting any resources.
Deployment with the Command Line
You have now deleted the application, so let’s deploy the same existing container image, but this time, we’ll use the
oc command line program.
The name of the image you used previously was:
If you have been given the name of an image to deploy and want to verify that it’s valid from the command line, you can use the
oc new-app --search command. For this image, run:
oc new-app --search openshiftkatacoda/blog-django-py
This should display output similar to this:
Docker images (oc new-app --docker-image=<docker-image> [--code=<source>]) ----- openshiftkatacoda/blog-django-py Registry: Docker Hub Tags: latest
This confirms that the image is found on the Docker Hub Registry.
To deploy the image, you can run the following command:
oc new-app openshiftkatacoda/blog-django-py
This will display output like this:
--> Found Docker image d7eda0c (22 hours old) from Docker Hub for "openshiftkatacoda/blog-django-py" Python 3.5 ---------- ... Tags: builder, python, python35, rh-python35 * An image stream will be created as "blog-django-py:latest" that will track this image * This image will be deployed in deployment config "blog-django-py" * Port 8080/tcp will be load balanced by service "blog-django-py" * Other containers can access this service through the hostname "blog-django-py" --> Creating resources ... imagestream "blog-django-py" created deploymentconfig "blog-django-py" created service "blog-django-py" created --> Success Run 'oc status' to view your app.
OpenShift will assign a default name based on the name of the image, in this case,
blog-django-py. You can specify a different name to be given to the application, and the resources created, by supplying the
--name option along with the name you wish to use as an argument. Keep in mind that changing the name will change the value of the
app label and when deleting the application you will then need to use this name.
As with deploying an existing container image from the web console, it’s not exposed outside of the OpenShift cluster by default. To expose the application created so it’s available outside of the OpenShift cluster, you can run this command:
oc expose service/blog-django-py
Switch to the OpenShift web console to verify that the application has been deployed. Click on the URL displayed on the Overview page for the project to visit the application.
Alternatively, to view the hostname assigned to the route created from the command line, you can run this command:
oc get route/blog-django-py
Summary of Part Two
In this blog, we went over deploying an existing container image in OpenShift using
oc in the command line. We deployed an image directly from an external image registry. In the next part of the series, we’ll cover importing an image into the OpenShift internal image registry, and then deploying the image from the internal image registry.
Here’s a summary of the key commands used. To see more information on each
oc command, run it with the
oc new-app <docker-image> --name <name: Deploy an application from a container image found on an external image registry. If there is any ambiguity as to the source of the image, use the
Would You Like to Learn More?
This post is based on one of OpenShift’s interactive learning scenarios. To try it and our other tutorials without needing to install OpenShift, visit: https://learn.openshift.com
Do you have an OpenShift Online account? There’s no reason to wait. Get your applications running in minutes with no installation needed. Sign up for the free trial of OpenShift Online.
What other topics would you like to see in the future on this blog? We’re happy to make tutorials about anything that helps you with your OpenShift experience. Comment and let us know!