Tutorial: Build Docker app using Docker toolchain

Hello world - Build Docker file, Docker image for Catalyst 9000 app hosting

This tutorial guides you to build a sample C-based hello world application using the docker development environment for the Catalyst 9000 app hosting.

In this tutorial, we will go through quick start guide on how to build a sample docker application using docker development environment.

Create Hello World Program

Let's create a simple C-based hello world application which just logs the string "Hello World from Application" every few seconds to a persistent log file named helloworld.log.

# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <string.h>
# include<signal.h>

static FILE *fp = NULL;

static void sig_handler(int signo)
{
  if (signo == SIGINT || signo == SIGKILL || signo == SIGTERM) {
      printf("received signal = %d\n", signo);
      if (fp)
          fclose(fp);
      exit(0);
  }
}

int main()
{
    signal(SIGINT, sig_handler);
    signal(SIGKILL, sig_handler);
    signal(SIGTERM, sig_handler);
    const char *logfile = "helloworld.log";
    fp = fopen(logfile, "w+");
    while(1) {
        fprintf(fp, "Hello World from Application!!!\n");
        sleep(2);
        fflush(fp);
    }
    fclose(fp);
}

Create Dockerfile

A Dockerfile is a text file that defines a Docker image. You’ll use a Dockerfile to create your own custom Docker image, in other words to define your custom environment to be used in a Docker container.

FROM  alpine:3.4
RUN apk update
RUN apk add build-base
RUN mkdir -p /var/helloworld/
COPY helloworld.c /var/helloworld/
WORKDIR /var/helloworld/
RUN gcc helloworld.c -o helloworld
RUN chmod +x /var/helloworld/helloworld
CMD /var/helloworld/helloworld

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.

$ docker build -t helloworld:1.0 .

Create tar file

Using Docker Save command, create the tar file which can be installed on Catalyst 9000

$ docker save helloworld:1.0  -o helloworld.tar

To Install this application tarball on Catalyst 9000 refer to Getting Started