Deploying Docker Compose with Greengrass!
In this post, I'll show how to use two major concepts together:
- Docker images that can be privately hosted in Amazon Elastic Container Registry (ECR); and
- AWS IoT Greengrass components containing Docker Compose files.
These Docker Compose files can be used to run public Docker components, or pull private images from ECR. This means that you can deploy your own system of microservices to any platform compatible with AWS Greengrass.
This post is also available in video form - check the video link below if you want to follow along!
What is Docker?
Docker is very widely known in the DevOps world, but if you haven't heard of it, it's a way of taking a software application and bundling it up with all its dependencies so it can be easily moved around and run as an application. For example, if you have a Python application, you have two options:
- Ask the user to install Python, the
pip
dependencies needed to run the application, and give instructions on downloading and running the application; or - Ask the user to install Docker, then provide a single command to download and run your application.
Either option is viable, but it's clear to see the advantages of the Docker method.
A container is a running instance of an application, and an image is the result of saving a container so it can be run again. You can have multiple containers based on the same image. Think of it as a movie saved on a hard disk: the file on disk is the "image", and playing that movie is the "container".
Docker Compose
On top of bundling software into images, Docker has a plugin called Docker Compose, which is a way of defining a set of containers that can be run together. With the right configuration, the containers can talk to each or the host computer. For instance, you might want to run a web server, API server, and database at the same time; with Docker Compose, you can define these services in one file and give them permission to talk to each other.
Building Docker Compose into Greengrass Components
We're now going to use Docker Compose by showing how to deploy and run an application using Greengrass and Docker Compose. Let's take a look at the code.
The code we're using comes from my sample repository.
Clone the Code
The first step is to check the code out on your local machine. The build scripts use Bash, so I'd recommend something Linux-based, like Ubuntu. Execute the following to check out the code:
git clone https://github.com/mikelikesrobots/greengrass-docker-compose.git
Check Dependencies
The next step is to make sure all of our dependencies are installed and set up. Execute all of the following and check the output is what you expect - a help or version message.
aws --version
gdk --version
jq --version
docker --version
The AWS CLI will also need credentials set up such that the following call works:
aws sts get-caller-identity
Docker will need to be able to run containers as a super user. Check this using:
sudo docker run hello-world
Finally, we need to make sure Docker Compose is installed. This is available either as a standalone script or as a plugin to Docker, where the latter is the more recent method of installing. If you have access to the plugin version, I would recommend using that, although you will need to update the Greengrass component build script - docker-compose
is currently used.
# For the script version
docker-compose --version
# For the plugin version
docker compose --version
More information can be found for any of these components using:
- AWS CLI
- Greengrass Development Kit (GDK)
jq
:sudo apt install jq
- Docker
- Docker Compose