Overview
This document describes how to use docker tooling to develop a C based application which is packaged as a Container image.
The application contains a simple C program that prints the current system time at 1 second intervals - 10 times.
Goals
- Demonstrate various development tasks and concepts to implement a C based Container application package using docker tooling.
- Application build process using docker build environment.
Sample Code
This sample application code is maintained here.
Clone this repo and use branch master
. Find the application under directory 'simple-c-app'.
Some parts of the code may be reproduced in this document to give more context. But always use the above git repo and branch for the latest code.
Procedure Overview
- Implementing a simple C application
- Creating a docker image to setup the build environment
- Run the image locally and compile C application
- Creating a docker image with application binary contents
- Testing the application locally using docker tools
- Creating an IOx application package from the docker image
- Deploying and testing on the target platform
Procedure
This section describes above steps with more details.
Before going through the tutorial, get familiar with cisco provided artifacts for an IOx app development.
Here, we have used 2 Dockerfiles for creating a C application package:
- One Dockerfile for creating a docker image for the build environment. This is used for compiling a C application. This Dockerfile basically pulls a docker base image and installs the toolchain.
- Other Dockerfile is for creating a docker image for deployment. This Dockerfile basically pulls a docker base image and copies the application binaries.
Note that the application examples are demonstrated for IR800 platform, but the same procedure will be applicable for all other supported IOx platforms. Developer needs to use the right docker base image as per the IOx platform. Refer for images supported platforms & corresponding base images
Implementing a simple C application
Writing a simple C application which prints the current system time at 1 second intervals - 10 times.
Creating a project directory
Writing a C application
Here is the sample code:
Writing a loop application
Docker type applications are not based on /sbin/init to make the container alive and to start the init scripts. So, we need to use some loop application which will run in the background to make the container alive. This will be used in a Dockerfile as an application entry point. You will understand more details in the subsequent sections how to use this loop application.
Here is a simple loop application:
Note that you can also use a simple shell script to create a loop app instead of using this C based loop application.
Creating a docker image to setup the build environment
Write a Dockerfile
This Dockerfile does the following tasks:
- Pulls the docker base image
- Installs the docker toolchain to compile the C application
Here is a Dockerfile:
Creating a docker image
Login to the devhub using docker daemon before building a docker image. Instructions for devhub login
Now let us build an image from this Dockerfile.
Check if the image is successfully created using following command:
Run the image locally and compile C application
Let us run the image locally to compile C application.
Note that here we are volume mounting the directory (-v ${PWD}/apps:/opt/share) from host pc in to docker container. This will help to get the compiled binary files to host machine.
Creating a docker image with application binary contents
Now we are ready with the application binaries. Next step is creating a docker image with these application binaries.
Write a Dockerfile
This Dockerfile does the following tasks:
- Pulls the docker base image
- Copies the application binary contents on to base image
- Specifies the application entry point
Here is a Dockerfile content:
Creating a docker image
Now let us build an image from this Dockerfile.
Check if the image created successfully
Package descriptor file
Package descriptor file specifies requirements for the application. For docker type of applications, creating a package
descriptor file is optional. ioxclient creates a default package descriptor file while creating IOx docker
application package. If the developer want to add custom entries in a package descriptor file, he can add those
entries using docker's LABEL directive. Ioxclient uses those labels, add it in a package descriptor
file during creation of an IOx docker application package.
Refer ioxclient page to learn how to use docker Label's
and package.yaml.
Here is a sample application descriptor (package.yaml) file looks like:
Note: If you are using application descriptor file for non x86 architecture, Refer the docker images table for appropriate value of "cpuarch" in yaml file
Ex: For ie4k platform use the 'cpuarch' as 'ppc' in following yaml file
Few things to notice:
- Descriptor schema version is 2.2. This is the minimum version that supports docker style apps.
- The required port (8000) to be opened is specified under network->ports.
rootfs.tar
is the name of the file containing the docker image (output of docker save
command). More details in the next sections.
- Command to be run when starting up the app is ["/opt/apps/loop_app"]. Note that here loop_app is a loop application binary which is used to make the container alive. This is because container is not based on /sbin/init so that application will stop if we don’t use the loop application.
Creating an IOx application package
Now that we have created the required docker image (ir800-base-image), it is time to create an IOx application package from these artifacts.
ioxclient
(>= 1.4.0) provides a convenience command that generates an IOx application package from a docker image and package.yaml file.
Refer this page for ioxclient usage: ioxclient
Create an empty directory ex. 'app_package' and execute the ioxclient command in this directory
Make sure the profile for target platform is created and activated before creating package
Let us use this command.
The package.tar
is an IOx application package that can be used to deploy on an IOx platform.
Here is a simple-c-app directory structure on Host PC after successful building of IOx application package:
Ensure that your target platform supports docker style applications. Refer to Platform Matrix for details about your platform. This tutorial uses IR829 as the target IOx platform.
We will use ioxclient
to demonstrate deployment and lifecycle management of this application. You can do the same using Local Manager or Fog Director.
Add your device to ioxclient
Activate the app
Start the app
Access the app console
Summary
As part of this tutorial we saw:
- Using docker tools and base image to build a C app.
- Packaging a docker image into an IOx application package.
- Deploying this package to an IR800 router and testing the application functionality.