Packaging an App as a Docker Image


This section will describe how to:
  1. Package an alwaysAI app in a self-contained docker image.

  2. Run the app as a docker container.

Create the Dockerfile

Upon initial app setup, the alwaysAI CLI creates a dockerfile used to create the runtime environment for your app. To package an alwaysAI app into a standalone docker image, you will need to make a new dockerfile. The simplest way to get started is to copy your app’s Dockerfile:

$ cp Dockerfile Dockerfile.standalone

Now open the newly created Dockerfile.standalone in your favorite editor. It will contain a FROM statement, as well as any other lines you’ve added to customize your app runtime environment. Follow these steps to modify the dockerfile for your standalone docker image:

  1. Create the application working directory by adding the WORKDIR line. All the following commands in the dockerfile will run in this directory:

     WORKDIR /app
    
  2. Next, copy the contents of the app directory to the docker image:

     COPY . ./
    
  3. If you added a requirements.txt file, install the dependencies from the file:

     RUN pip3 install -r requirements.txt
    
  4. Finally, use CMD to run the application when the container starts:

     CMD ["python3", "app.py"]
    

If you’re using a starter app, your final Dockerfile.standalone might look like this:

FROM alwaysai/edgeiq:0.7.20
WORKDIR /app
COPY . ./
CMD ["python3", "app.py"]

To learn more about these commands, visit the Dockerfile reference page.

Build the Docker Image

To build the image, you first need to deploy the app to the device you’d like to run the app on. Run the configure CLI command to select the target device:

$ aai app configure

Next, run the deploy CLI command to copy Dockerfile.standalone over to the device:

$ aai app deploy

If you’re packaging your app on a remote device, run the following command to work directly on the target device:

aai app shell --no-container

Build the docker image on the target device:

$ docker build -t <image_name> -f Dockerfile.standalone .
  • The -t option tags the docker image being built with the provided name.

  • The image name can be anything you choose. To make it easy to keep track of your image, you can use the app name.

You can learn more about these options from the Docker image reference page.

You should be able to see your image using the docker image ls command. Your output might look like this:

$ docker image ls
REPOSITORY       TAG      IMAGE ID      CREATED       SIZE
<image_name>     latest   e45e70a16ca0  1 minute ago  1.33GB
alwaysai/edgeiq  0.7.20   351b87b17198  2 hours ago   1.32GB

Run Your App

To run the app in a docker container, use the following command on the target device:

docker run --rm --network=host --privileged -v /dev:/dev <image_name>
  • The --rm flag tells docker to remove the container when the app shuts down.

  • The --network==host flag tells docker to map the device’s network interfaces into the container. This enables access to the internet and the Streamer from outside the container.

  • The --privileged flag is needed when working with USB devices.

  • The -v /dev:/dev flag mounts the devices directory into the container so that cameras and USB devices can be accessed.

To learn more about these options, visit the Docker Run reference page.

Once your app is up and running you can manage it with docker container commands. A couple useful commands are:

  • docker container ls will give a list of all running containers.

  • docker container kill <id> will stop the container of the given ID.

Learn more about these commands at the Docker container reference page.

To access the Streamer, use the device’s hostname or IP address in your development machine’s browser. For example:

http://raspberrypi:5000