Using devfiles in odo

About the devfile in odo

The devfile is a portable file that describes your development environment. With the devfile, you can define a portable developmental environment without the need for reconfiguration.

With the devfile, you can describe your development environment, such as the source code, IDE tools, application runtimes, and predefined commands. To learn more about the devfile, see the devfile documentation.

With odo, you can create components from the devfiles. When creating a component by using a devfile, odo transforms the devfile into a workspace consisting of multiple containers that run on OKD, Kubernetes, or Docker. odo automatically uses the default devfile registry but users can add their own registries.

Creating a Java application by using a devfile

Prerequisites

  • You have installed odo.

  • You must know your ingress domain cluster name. Contact your cluster administrator if you do not know it. For example, apps-crc.testing is the cluster domain name for Red Hat CodeReady Containers.

Creating a project

Create a project to keep your source code, tests, and libraries organized in a separate single unit.

Procedure

  1. Log in to an OKD cluster:

    1. $ odo login -u developer -p developer
  2. Create a project:

    1. $ odo project create myproject

    Example output

    1. Project 'myproject' is ready for use
    2. New project created and now using project : myproject

Listing available devfile components

With odo, you can display all the components that are available for you on the cluster. Components that are available depend on the configuration of your cluster.

Procedure

  1. To list available devfile components on your cluster, run:

    1. $ odo catalog list components

    The output lists the available odo components:

    1. Odo Devfile Components:
    2. NAME DESCRIPTION REGISTRY
    3. java-maven Upstream Maven and OpenJDK 11 DefaultDevfileRegistry
    4. java-openliberty Open Liberty microservice in Java DefaultDevfileRegistry
    5. java-quarkus Upstream Quarkus with Java+GraalVM DefaultDevfileRegistry
    6. java-springboot Spring Boot® using Java DefaultDevfileRegistry
    7. nodejs Stack with NodeJS 12 DefaultDevfileRegistry
    8. Odo OpenShift Components:
    9. NAME PROJECT TAGS SUPPORTED
    10. java openshift 11,8,latest YES
    11. dotnet openshift 2.1,3.1,latest NO
    12. golang openshift 1.13.4-ubi7,1.13.4-ubi8,latest NO
    13. httpd openshift 2.4-el7,2.4-el8,latest NO
    14. nginx openshift 1.14-el7,1.14-el8,1.16-el7,1.16-el8,latest NO
    15. nodejs openshift 10-ubi7,10-ubi8,12-ubi7,12-ubi8,latest NO
    16. perl openshift 5.26-el7,5.26-ubi8,5.30-el7,latest NO
    17. php openshift 7.2-ubi7,7.2-ubi8,7.3-ubi7,7.3-ubi8,latest NO
    18. python openshift 2.7-ubi7,2.7-ubi8,3.6-ubi7,3.6-ubi8,3.8-ubi7,3.8-ubi8,latest NO
    19. ruby openshift 2.5-ubi7,2.5-ubi8,2.6-ubi7,2.6-ubi8,2.7-ubi7,latest NO
    20. wildfly openshift 10.0,10.1,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0,20.0,8.1,9.0,latest NO

Deploying a Java application using a devfile

In this section, you will learn how to deploy a sample Java project that uses Maven and Java 8 JDK using a devfile.

Procedure

  1. Create a directory to store the source code of your component:

    1. $ mkdir <directory-name>
  2. Create a component configuration of Spring Boot component type named myspring and download its sample project:

    1. $ odo create java-spring-boot myspring --starter

    The previous command produces the following output:

    1. Validation
    2. Checking devfile compatibility [195728ns]
    3. Creating a devfile component from registry: DefaultDevfileRegistry [170275ns]
    4. Validating devfile component [281940ns]
    5. Please use `odo push` command to create the component with source deployed

    The odo create command downloads the associated devfile.yaml file from the recorded devfile registries.

  3. List the contents of the directory to confirm that the devfile and the sample Java application were downloaded:

    1. $ ls

    The previous command produces the following output:

    1. README.md devfile.yaml pom.xml src
  4. Create a URL to access the deployed component:

    1. $ odo url create --host apps-crc.testing

    The previous command produces the following output:

    1. URL myspring-8080.apps-crc.testing created for component: myspring
    2. To apply the URL configuration changes, please use odo push
    You must use your cluster host domain name when creating the URL.
  5. Push the component to the cluster:

    1. $ odo push

    The previous command produces the following output:

    1. Validation
    2. Validating the devfile [81808ns]
    3. Creating Kubernetes resources for component myspring
    4. Waiting for component to start [5s]
    5. Applying URL changes
    6. URL myspring-8080: http://myspring-8080.apps-crc.testing created
    7. Syncing to component myspring
    8. Checking files for pushing [2ms]
    9. Syncing files to the component [1s]
    10. Executing devfile commands for component myspring
    11. Executing devbuild command "/artifacts/bin/build-container-full.sh" [1m]
    12. Executing devrun command "/artifacts/bin/start-server.sh" [2s]
    13. Pushing devfile component myspring
    14. Changes successfully pushed to component
  6. List the URLs of the component to verify that the component was pushed successfully:

    1. $ odo url list

    The previous command produces the following output:

    1. Found the following URLs for component myspring
    2. NAME URL PORT SECURE
    3. myspring-8080 http://myspring-8080.apps-crc.testing 8080 false
  7. View your deployed application by using the generated URL:

    1. $ curl http://myspring-8080.apps-crc.testing

Converting an S2I component into a devfile component

With odo, you can create both Source-to-Image (S2I) and devfile components. If you have an existing S2I component, you can convert it into a devfile component using the odo utils command.

Procedure

Run all the commands from the S2I component directory.

  1. Run the odo utils convert-to-devfile command, which creates devfile.yaml and env.yaml based on your component:

    1. $ odo utils convert-to-devfile
  2. Push the component to your cluster:

    1. $ odo push

    If the devfile component deployment failed, delete it by running: odo delete -a

  3. Verify that the devfile component deployed successfully:

    1. $ odo list
  4. Delete the S2I component:

    1. $ odo delete --s2i