Forem

Cover image for Python containers for ARM
Sergio Méndez
Sergio Méndez

Posted on

Python containers for ARM

Hi, this is the beginning of my blog post series called, 30DaysOfIoTEdge, you will learn about how to use Kubernetes, ARM, and container on the Edge, something that you can use to solve problems around Iot and Edge stuff. So, let's start with Day 1 creating a container for Python that runs on ARM.

What you will learn

In this blog post you will learn:

  • What you need to compile for ARM.
  • Create a basic container for Python using Flask as a basic API.
  • Adapt your container to run on ARM devices.

Requirements

  • Raspberry Pi or ARM instance in the cloud.
  • Docker installed
  • Ubuntu >= 22.04

So Let's get started with this post.

Creating Python containers for ARM

1. Install Docker with permission to execute the command without sudo. For this run the following commands:

sudo apt-get update
sudo apt-get install -y docker.io
sudo groupadd docker
sudo usermod -aG docker $USER
Enter fullscreen mode Exit fullscreen mode

This commands install Docker in your machine, then restarts your terminal, in order to run docker without sudo. This is something that you will like to do instead of putting sudo before the docker command every time.

2. Create your Dockerfile
Create the file with the following command:

cat > Dockerfile <<-EOF
FROM python:3-alpine

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["ash", "-c", "python main.py"]
EOF
Enter fullscreen mode Exit fullscreen mode

3. Create your main.py
Create the file with the following command:

cat > main.py <<-EOF
from flask import Flask
import os
import socket

app = Flask(__name__)

@app.route('/')
def index():
    return "Hello ARM Container"


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000), debug=True)
EOF
Enter fullscreen mode Exit fullscreen mode

4. Create the requeriments.txt
Create the file with the following command:

cat > requeriments.txt <<-EOF
flask
EOF
Enter fullscreen mode Exit fullscreen mode

Note: This file includes all the dependencies of libraries using in your code. In this case we just use Flask, the other libraries are by default included.

5. Login into your registry
For this run the following command:

docker login
Enter fullscreen mode Exit fullscreen mode

Enter your user from DockerHub or specify the registry your are using to push your container like ECR, GCR, AZR or Harbor as an open source container or artifact registry.
6. Build and push your container
For this run the following command:

docker buildx build --platform linux/arm64 \
-t <USERNAME>/<CONTAINER_NAME>:<TAG> --push .
Enter fullscreen mode Exit fullscreen mode

Note: --platform specify the platform where the container is going to run. Common platforms used for ARM are linux/arm64 for ARM v8 and, linux/arm/v7 for v7. Also you can use x86_64 for Intel and Nuc devices.

7. Test your container.
For this run the following command:

docker run -it -p 5000:5000 <USERNAME>/<CONTAINER_NAME>:<TAG>
Enter fullscreen mode Exit fullscreen mode

8. Test your API
For this use curl to test the API running:

curl http://<HOST_IP>:5000
Enter fullscreen mode Exit fullscreen mode

It shows the Hello ARM Container

Tricks

In some cases, you can find precompiled packages or wheels for Python. Let's say for example that you are building for a Raspberry Pi device using Raspbian as its operating system. Raspbian uses some specific wheels precompiled for Raspbian but you can use it for another operating system. Why? Sometimes the libraries in Python are not compiled for ARM and when you build the container it detects that the library needs to be compiled for ARM and fails. You can solve it but it's easier to use the Python wheels precompiled for Raspbian. How you can use that wheels, just by running the following command:

RUN pip install --index-url=https://www.piwheels.org/simple --no-cache-dir -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Instead of

RUN pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Thank you and follow me

If you liked my blog post comment 😀
Also follow me at:

This blog post is an extender version of my book:

Top comments (0)