alpine-docker / greengrass

AWS IoT Greengrass Docker Software
https://docs.aws.amazon.com/greengrass/latest/developerguide/what-is-gg.html#gg-docker-download
Other
2 stars 2 forks source link

Running AWS IoT Greengrass in a Docker Container

Overview

AWS IoT Greengrass can run in a Docker container. You can use the Dockerfile in this package to build a container image that runs on x86_64 platforms. The resulting Greengrass Docker image is 532 MB in size.

Prerequisites

Linux Configuration

If you're using a Linux computer to run Docker, you must enable symlink and hardlink protection and IPv4 network forwarding. On Mac and Windows computers, these settings are already enabled in the virtual machine that Docker runs in.

Run the following commands in the host computer's terminal.

Enable Symlink and Hardlink Protection (Linux only)

The symlink and hardlink protection settings must be enabled to run the AWS IoT Greengrass core software.

echo 1 > /proc/sys/fs/protected_hardlinks
echo 1 > /proc/sys/fs/protected_symlinks
echo '# AWS Greengrass' >> /etc/sysctl.conf
echo 'fs.protected_hardlinks = 1' >> /etc/sysctl.conf
echo 'fs.protected_symlinks = 1' >> /etc/sysctl.conf

sysctl -p

Note: You may need to use sudo to run these commands.

Enable IPv4 Network Forwarding (Linux only)

IPv4 network forwarding must be enabled for AWS IoT Greengrass cloud deployment and MQTT communications to work.

sudo nano /etc/sysctl.conf
# set this net.ipv4.ip_forward = 1
sudo sysctl -p

Note: You can use the editor of your choice in place of nano.

If you don't enable this setting, you receive an error message such as WARNING: IPv4 is disabled. Networking will not work. For more information, see "Configure namespaced kernel parameters (sysctls) at runtime" (https://docs.docker.com/engine/reference/commandline/run/#configure-namespaced-kernel-parameters-sysctls-at-runtime).

Reduce the Size of the AWS IoT Greengrass Docker Image (Optional)

Currently, the Greengrass Docker image is about 532 MB. Most of this size is attributed to the heavy amazonlinux Docker base image that AWS IoT Greengrass runs on.

Use following techniques to reduce the size of your Greengrass Docker image. Otherwise, continue to the "Running AWS IoT Greengrass in a Docker Container" procedure.

AWS IoT Greengrass Docker Image Size Comparison

| Base Image | Installed Dependencies | Platform | Size | Instructions | |---------------+------------------------+----------+-------+-------------------------------------| | AmazonLinux:2 | Python, Nodejs, Java | x86_64 | 532MB | Default Dockerfile | | AmazonLinux:2 | Python | x86_64 | 370MB | Reduce Lambda Runtime Installations | | Alpine:3.8 | Python | x86_64 | 95MB | Change the Base Docker Image |

Running AWS IoT Greengrass in a Docker Container

The following steps show how to build the Docker image from the Dockerfile and configure AWS IoT Greengrass to run in a Docker container.

Step 1. Build the AWS IoT Greengrass Docker Image

On Linux or Mac OSX

1- Download and decompress the aws-greengrass-docker-1.9.2 package.

2- In a terminal, run the following commands in the location where you decompressed the aws-greengrass-docker-1.9.2 package.

docker login
cd ~/Downloads/aws-greengrass-docker-1.9.2 
docker build -t "x86_64/aws-iot-greengrass:1.9.2" ./

Note: If you have docker-compose installed, you can run the following commands instead:

docker login
cd ~/Downloads/aws-greengrass-docker-1.9.2 
docker-compose -f docker-compose.yml build

Note: If you want to use a smaller size docker image, run below command:

docker-compose -f docker-compose.alpine-x86-64.yml build

3- Verify that the Greengrass Docker image was built.

docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
x86-64/aws-iot-greengrass           1.9.2               3f152d6707c8        17 seconds ago      532MB

On RaspberryPi for armv7l platform

If you want to build docker images for armv7l platform, follow below steps on RaspberryPi with Docker and Docker-compose installed.

1- Download and decompress the aws-greengrass-docker-1.9.2 package.

2- In a terminal, run the following commands in the location where you decompressed the aws-greengrass-docker-1.9.2 package.

docker login
cd ~/Downloads/aws-greengrass-docker-1.9.2 
docker build -t "armv7l/aws-iot-greengrass:1.9.2" -f Dockerfile.alpine-armv7l ./

Note: If you have docker-compose installed, you can run the following commands instead:

docker login
cd ~/Downloads/aws-greengrass-docker-1.9.2 
docker-compose -f docker-compose.alpine-armv7l.yml build

3- Verify that the Greengrass Docker image was built.

docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
armv7l/aws-iot-greengrass           1.9.2               3f152d6707c8        17 seconds ago      532MB

On a Windows Computer

1- Download and decompress the aws-greengrass-docker-1.9.2 package using a utility like WinZip or 7-Zip.

2- Using Notepad++, convert the greengrass-entrypoint.sh file to use Unix-style line endings. For more information, see "Converting from Windows-style to UNIX-style line endings" (https://support.nesi.org.nz/hc/en-gb/articles/218032857-Converting-from-Windows-style-to-UNIX-style-line-endings).
Otherwise, you will get this error while running the build Docker image: [FATAL tini (6)] exec /greengrass-entrypoint.sh failed: No such file or directory.

a. Open greengrass-entrypoint.sh in Notepad++.
b. In the "Edit" menu, choose "EOL Conversion", and then choose "UNIX (LF)".
c. Save the file.

3- In a command prompt, run the following command in the location where you decompressed the aws-greengrass-docker-1.9.2 package.

docker login
cd C:\Users\%USERNAME%\Downloads\aws-greengrass-docker-1.9.2
docker build -t "x86_64/aws-iot-greengrass:1.9.2" ./

Note: If you have docker-compose installed, you can run the following commands instead:

docker login
cd C:\Users\%USERNAME%\Downloads\aws-greengrass-docker-1.9.2
docker-compose -f docker-compose.yml build

4- Verify that the Greengrass Docker image was built.

docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
x86-64/aws-iot-greengrass           1.9.2               3f152d6707c8        17 seconds ago      532MB

Step 2. Run AWS IoT Greengrass Locally

On Linux or Mac OSX

1- Use the AWS IoT Greengrass console to create a Greengrass group. Follow the steps in "Configure AWS IoT Greengrass on AWS IoT" (https://docs.aws.amazon.com/greengrass/latest/developerguide/gg-config.html). This process includes downloading certificates and the core configuration file.
Skip step 8b of the procedure because AWS IoT Greengrass core and its runtime dependencies are already set up in the Docker image.

2- Decompress the certificates and config file that you downloaded into your working directory where Dockerfile and docker-compose.yml are located. For example: (replace guid in your command)

cp ~/Downloads/guid-setup.tar.gz ~/Downloads/aws-greengrass-docker-1.9.2
cd ~/Downloads/aws-greengrass-docker-1.9.2
tar xvzf guid-setup.tar.gz

3- Download the root CA certificate into the directory where you decompressed the certificates and configuration file. The certificates enable your device to communicate with AWS IoT using the MQTT messaging protocol over TLS. For more information, including how to choose the appropriate root CA certificate, see the documentation on "Server Authentication in AWS IoT Core" (https://docs.aws.amazon.com/iot/latest/developerguide/managing-device-certs.html).

Important: Your root CA certificate must match your endpoint, which uses Amazon Trust Services (ATS) server authentication (preferred) or legacy server authentication. You can find your endpoint on the Settings page in the AWS IoT Core console.

4- Run the following command to confirm that the root.ca.pem file is not empty.

cat ~/Downloads/aws-greengrass-docker-1.9.2/certs/root.ca.pem

On a Windows Computer

1- Use the AWS IoT Greengrass console to create a Greengrass group. Follow the steps in "Configure AWS IoT Greengrass on AWS IoT" (https://docs.aws.amazon.com/greengrass/latest/developerguide/gg-config.html). This process includes downloading certificates and the core configuration file.
Skip step 8b of the procedure because AWS IoT Greengrass core and its runtime dependencies are already set up in the Docker image.

2- Decompress the certificates and config file that you downloaded into your working directory where Dockerfile and docker-compose.yml are located. Use a utility like WinZip or 7-Zip to decompress <guid>-setup.tar.gz to C:\Users\%USERNAME%\Downloads\aws-greengrass-docker-1.9.2\.

3- Download the root CA certificate into the directory where you decompressed the certificates and configuration file. The certificates enable your device to communicate with AWS IoT using the MQTT messaging protocol over TLS. For more information, including how to choose the appropriate root CA certificate, see the documentation on "Server Authentication in AWS IoT Core" (https://docs.aws.amazon.com/iot/latest/developerguide/managing-device-certs.html).

Important: Your root CA certificate must match your endpoint, which uses Amazon Trust Services (ATS) server authentication (preferred) or legacy server authentication. You can find your endpoint on the Settings page in the AWS IoT Core console.

Note: If you don't have curl installed, follow these steps:

4- Run the following command to confirm that the root.ca.pem file is not empty.

type C:\Users\%USERNAME%\Downloads\aws-greengrass-docker-1.9.2\certs\root.ca.pem

Step 3. Run the Docker Container

On Linux or Mac OSX

1- In the terminal, run the following command:

The output should look like this example:

Setting up greengrass daemon
Validating hardlink/softlink protection
Waiting for up to 30s for Daemon to start

Greengrass successfully started with PID: 10

Note: This command starts AWS IoT Greengrass and bind-mounts the certificates and config file. This will keep the interactive shell open, so that the Greengrass container can be removed/released later. You can find "Debugging" steps below if the container doesn't open the shell and exits immediately.

On RaspberryPi for armv7l platform

1- In the terminal, run the following command:

The output should look like this example:

Setting up greengrass daemon
Validating hardlink/softlink protection
Waiting for up to 30s for Daemon to start

Greengrass successfully started with PID: 10

Note: This command starts AWS IoT Greengrass and bind-mounts the certificates and config file. This will keep the interactive shell open, so that the Greengrass container can be removed/released later. You can find "Debugging" steps below if the container doesn't open the shell and exits immediately.

On a Windows Computer

1- In the command prompt, run the following command:

docker run --rm --init -it --name aws-iot-greengrass --entrypoint /greengrass-entrypoint.sh -v c:/Users/%USERNAME%/Downloads/aws-greengrass-docker-1.9.2/certs:/greengrass/certs -v c:/Users/%USERNAME%/Downloads/aws-greengrass-docker-1.9.2/config:/greengrass/config -p 8883:8883 x86-64/aws-iot-greengrass:1.9.2

Note: If you have docker-compose installed, you can run the following commands instead:

cd C:/Users/%USERNAME%/Downloads/aws-greengrass-docker-1.9.2
docker-compose -f docker-compose.yml down
docker-compose -f docker-compose.yml up

Docker will prompt you to share your C:\ drive with the Docker daemon. Allow it to bind-mount the C:\ directory inside the Docker container. For more information, see "Shared drives" (https://docs.docker.com/docker-for-windows/#shared-drives) in the Docker documentation.

The output should look like this example:

Setting up greengrass daemon
Validating hardlink/softlink protection
Waiting for up to 30s for Daemon to start

Greengrass successfully started with PID: 10

Note: This command starts AWS IoT Greengrass and bind-mounts the certificates and config file. This will keep the interactive shell open, so that the Greengrass container can be removed/released later. You can find "Debugging" steps below if the container doesn't open the shell and exits immediately.

Step 4: Configure "No container" Containerization for the Greengrass Group

When you run AWS IoT Greengrass in a Docker container, all Lambda functions must run without containerization. In this step, you set the the default containerization for the group to "No container". You must do this before you deploy the group for the first time.

1- In the AWS IoT console, choose "Greengrass", and then choose "Groups".
2- Choose the group whose settings you want to change.
3- Choose "Settings".
4- Under "Lambda runtime environment", choose "No container".

For more information, see "Setting Default Containerization for Lambda Functions in a Group" (https://docs.aws.amazon.com/greengrass/latest/developerguide/lambda-group-config.html#lambda-containerization-groupsettings).

Note: By default, Lambda functions use the group containerization setting. If you override the "No container" setting for any Lambda functions when AWS IoT Greengrass is running in a Docker container, the deployment fails.

Step 5: Deploy Lambda Functions to the AWS IoT Greengrass Docker Container

You can deploy long-lived Lambda functions to the Greengrass Docker container.

1- Follow the steps in "Module 3 (Part 1): Lambda Functions on AWS IoT Greengrass" (https://docs.aws.amazon.com/greengrass/latest/developerguide/module3-I.html) to deploy a long-lived Hello-World Lambda function to the container.

Debugging the Docker Container

To debug issues with the container, you can persist the runtime logs or attach an interactive shell.

Persist Greengrass Runtime Logs outside the Greengrass Docker Container

You can run the AWS IoT Greengrass Docker container after bind-mounting the /greengrass/ggc/var/log directory to persist logs even after the container has exited or is removed.

On Linux or Mac OSX

Run the following command in the terminal.

docker run --rm --init -it --name aws-iot-greengrass \
--entrypoint /greengrass-entrypoint.sh \
-v ~/Downloads/aws-greengrass-docker-1.9.2/certs:/greengrass/certs \
-v ~/Downloads/aws-greengrass-docker-1.9.2/config:/greengrass/config \
-v ~/Downloads/aws-greengrass-docker-1.9.2/log:/greengrass/ggc/var/log \
-p 8883:8883 \
<platform>/aws-iot-greengrass:1.9.2

You can then check your logs at ~/Downloads/aws-greengrass-docker-1.9.2/log on your host to see what happened while Greengrass was running inside the Docker container.

On a Windows Computer

Run the following command in the command prompt.

cd C:\Users\%USERNAME%\Downloads\aws-greengrass-docker-1.9.2
mkdir log
docker run --rm --init -it --name aws-iot-greengrass --entrypoint /greengrass-entrypoint.sh -v c:/Users/%USERNAME%/Downloads/aws-greengrass-docker-1.9.2/certs:/greengrass/certs -v c:/Users/%USERNAME%/Downloads/aws-greengrass-docker-1.9.2/config:/greengrass/config -v c:/Users/%USERNAME%/Downloads/aws-greengrass-docker-1.9.2/log:/greengrass/ggc/var/log -p 8883:8883 x86-64/aws-iot-greengrass:1.9.2

You can then check your logs at c:/Users/%USERNAME%/Downloads/aws-greengrass-docker-1.9.2/log on your host to see what happened while Greengrass was running inside the Docker container.

Attach an Interactive Shell to the Greengrass Docker Container

You can attach an interactive shell to a running AWS IoT Greengrass Docker container. This can help you to investigate the state of the Greengrass Docker container.

On Linux or Mac OSX

Run the following command in the terminal.

docker exec -it $(docker ps -a -q -f "name=aws-iot-greengrass") /bin/bash
On a Windows Computer

Run the following commands in the command prompt.

docker ps -a -q -f "name=aws-iot-greengrass"

Replace <GG_CONTAINER_ID> with the container_id result from the previous command.

docker exec -it <GG_CONTAINER_ID> /bin/bash

Stopping the Docker Container

To stop the AWS IoT Greengrass Docker Container, press Ctrl+C in your terminal or command prompt.

This action will send SIGTERM to the Greengrass daemon process to tear down the Greengrass daemon process and all Lambda processes that were started by the daemon process. The Docker container is initialized with /dev/init process as PID 1, which helps in removing any leftover zombie processes. For more information, see the Docker run reference: https://docs.docker.com/engine/reference/commandline/run/#options.

Troubleshooting