In this tutorial, we will go through quick start guide on how to build a sample lxc type application using docker development environment and package it to an IOx application. For more details on what are the different types of IOx application supported, refer this section.
Requirements
- The command "ioxclient docker package" when used to package for lxc type app, requires linux development environment (linux_386, linux_amd64)
- Minimum Docker version 'Docker CE 17.05' (API 1.29). This tutorial utilizes the multistage build feature which is introduced in the docker CE 17.05.
- If you are using the IOx SDE VM, upgrade the docker version to 17.05 or more. Refer the Docker documentation.
- Docker daemon up and running
Setup Docker development environment for IOx
Refer this section to setup docker daemon on your development machine for authentication with Cisco hosted DevHub repository.
Create Dockerfile
- Refer Minified docker base images from Cisco repository section to find complete list of cisco hosted base os images.
- For a given platform and architecture, it is recommended to choose corresponding cisco provided docker base rootfs image from cisco devhub repositroy, as this will create minimal footprint for final IOx app package. Use the path in "Docker base image" column to pull docker images with tags as needed. For example, cisco hosted docker base os image with "latest" tag for IR829 platform can be pulled using the below command in Dockerfile.
- Refer this section to find out various packages hosted as part of cisco devhub repository and how to install those packages on top of cisco hosted docker image.
- Let's create a simple c based hello world application which just logs the string "Hello World from IOx App" every few seconds to a persistent log file named helloworld.log.
IOx exposes location of peristent log directory for apps via environment variable CAF_APP_LOG_DIR. Default location of this dir is /data/logs. Refer this section for more environment variables exposed by IOx to applications.
Printing to console can overflow the buffer and hang the target application. It is recommended not to print infinitely to console.
Copy below contents into a file called helloworld.c.
For LXC type of application, we need to start the application as part of system init script. Hence we need to add an application init script to /etc/init.d and configure to start automatically. Copy below contents into a file called init_app.sh in the same directory as helloworld.c.
Here is the Dockerfile you would need for x86_64 architecture, building hello world application and starting by default as part of system init script. Here we are utilizing multi stage docker builds to avoid including build tools in the final image and there by reducing the overall footprint of the IOx application. Refer this page for more details on multi-stage docker builds.
Copy below contents into a file called Dockerfile in the same directory as helloworld.c.
Build Docker image
Now build docker image named helloworld with version 1.0 using previously created dockerfile. Prepend sudo if the docker build command fails due to permission restrictions.
Verify application dependencies in docker image - optional
You can verify if the docker image now has all the application's dependant libraries or modules by running docker container locally in developement environment. Execute below command to run docker container and getting access to shell session inside the container. For this tutorial, make sure that application binary is copied under /var/helloworld/ directory and init script init_app.sh is copied under /etc/init.d/ dir.
Create IOx package descriptor file
IOx package descriptor file contains metadata about the application, minimum resource requirements for running the application. Refer this section for more detailed description of various attributes and schema for package descriptor contents. Copy below contents into a file called package.yaml in the same directory as helloworld.c.
NOTE:
- "rootfs" is always "rootfs.img" for docker built apps.
- "target" is the boot-up script. This can be an OS boot init exec or a user customized startup script.
NOTE: if multiple network interfaces are required, the below package.yaml syntax is used:
Create final IOx application package
Download and install latest version of ioxclient for your development environment from this location. Setup ioxclient device profile by configuring device IP, credentials and SSH port.
Refer to Section "Configuring ioxclient" in "ioxclient Reference" for details.
Use below ioxclient command to build final IOx application package named package.tar. Need to pass same docker image name and version that has been previously used to build the image. Prepend sudo if the ioxclient command fails due to permission restrictions.
Deploy/Activate/Start the application
Now you can deploy the application onto a physical or virtual IOx device using either of the clients ioxclient or Local Manager or Fog Director. You can access device Local Manager UI using the URL path https://:8443 or https://:443. Lets use ioxclient to deploy this application to a device. Execute below ioxclient commands to setup device profile, install, activate and start the application.
Refer profiles and app management for more ioxclient details. Note: Here we are using default attributes for application activation.
Verify the application is running
Now lets console into the application and confirm that application is running successfully by logging the string at /data/logs/helloworld.log. Use below ioxclient command to console into the application. Login as root into the application and password is not required.