$ oc new-project ocp-test --display-name="OCP Test" --description="This is OCP Test"
Create an App from a Docker image
In this recipe you will learn how to create a new project on OpenShift and how to create an application from an existing docker image.
Add a new project from command line
Upon project creation, OpenShift will automatically switch to the newly created project/namespace. If you wish to view the list of projects, run the following command:
$ oc get projects
If you have more than one project, you can switch to a different one by issuing
oc project <project name>. Although you don’t want to do it now.
You can also check the status of the project by running the following command. It says that the project is currently not running anything.
$ oc status In project OCP Test (ocp-test) on server https://master.na1.openshift.opentlc.com:443 You have no services, deployment configs, or build configs. Run 'oc new-app' to create an application.
Create an application from a Docker Image
Next we will create an application inside the above project using an existing docker image. We will be using a very simple docker image on dockerhub that just says "Welcome to OpenShift V3". Let us just use that for this exercise.
First create a new application using the docker image using the oc new-app command as shown below:
$ oc new-app redhatworkshops/welcome-php --name=welcome --> Found Docker image 3e73433 (7 months old) from Docker Hub for "redhatworkshops/welcome-php" chx/welcome-php-1:b68a2d86 -------------------------- Platform for building and running PHP 5.6 applications Tags: builder, php, php56, rh-php56 * An image stream will be created as "welcome:latest" that will track this image * This image will be deployed in deployment config "welcome" * Port 8080/tcp will be load balanced by service "welcome" * Other containers can access this service through the hostname "welcome" --> Creating resources ... imagestream "welcome" created deploymentconfig "welcome" created service "welcome" created --> Success Run 'oc status' to view your app.
The above command uses the docker image to deploy a docker container in a pod. If you quickly run
oc get pods you will notice that a deployer pod runs and it starts an application pod as shown below.
$ oc get pods NAME READY STATUS RESTARTS AGE welcome-1-xw3dn 1/1 Running 0 2m
Add a Route for your application
OpenShift also spins up a service for this application. Run the following command to view the list of services in the project.
$ oc get services NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE welcome 172.30.101.82 <none> 8080/TCP 4m
You will notice the
welcome service was created for this project.
However, there is no route for this application yet. So you cannot access this application from outside.
Now add a route to the service with the following command.
oc expose command will allow you to expose your service to the world so that you can access it from the browser.
$ oc get service NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE welcome 172.30.101.82 <none> 8080/TCP 8m
Try your application
To get the test HOST/PORT via
$ oc get route NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD welcome welcome-ocp-test.example.com welcome 8080-tcp None
Access the application: Now access the application using curl (looking for 200 status code) or from the browser and see the result
$ curl -Is welcome-ocp-test.apps.example.com HTTP/1.1 200 OK Date: Sun, 09 Jul 2017 12:55:32 GMT Server: Apache/2.4.18 (Red Hat) Content-Type: text/html; charset=UTF-8 Set-Cookie: 29bd1045b4b73d6a6c47bc9bded48f0e=4edfbf67d301fe8a2fdc3e8885379a15; path=/; HttpOnly Cache-control: private
Voila!! you created your first application using an existing docker image on OpenShift.
oc get all command to view all the components that were created in your project.
$ oc get all NAME DOCKER REPO TAGS UPDATED is/welcome 172.30.160.227:5000/ocp-test/welcome latest 12 minutes ago NAME REVISION DESIRED CURRENT TRIGGERED BY dc/welcome 1 1 1 config,image(welcome:latest) NAME DESIRED CURRENT READY AGE rc/welcome-1 1 1 1 12m NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD routes/welcome welcome-ocp-test.apps.example.com welcome 8080-tcp None NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/welcome 172.30.101.82 <none> 8080/TCP 12m NAME READY STATUS RESTARTS AGE po/welcome-1-xw3dn 1/1 Running 0 12m
Now you can delete all these components by running one command.
$ oc delete all --all imagestream "welcome" deleted deploymentconfig "welcome" deleted route "welcome" deleted service "welcome" deleted
You will notice that it has deleted the imagestream for the application, the deploymentconfig, the service and the route.
You can run
oc get all again to make sure the project is empty.
Congratulations!! You now know how to create a project, an application using an external docker image and navigate around. Get ready for more fun stuff!