Skip to content

Commit e989a05

Browse files
Added file instructions
1 parent f974ee9 commit e989a05

File tree

1 file changed

+113
-15
lines changed
  • content/hardware/04.pro/boards/portenta-x8/tutorials/custom-container

1 file changed

+113
-15
lines changed
Lines changed: 113 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
---
22
title: Create and Upload a Custom Container Portenta X8
33
difficulty: easy
4-
tags: [Linux, containers]
4+
tags: [Linux, Python, Containers]
55
description: This tutorial will show you how to create and upload your custom container to your Portenta X8
66
author: Benjamin Dannegård
77
hardware:
88
- hardware/04.pro/board/portenta-x8
99
software:
10-
-
11-
10+
- adb
1211
---
1312

1413
## Overview
1514

16-
In this tutorial we will create a simple container that we can then upload to the Portenta X8. A container consists of an image file and all it's dependencies if there are any.
15+
In this tutorial we will create a simple container that we can then upload to the Portenta X8. A container consists of an image file and all it's dependencies if there are any. This tutorial will go through the different files needed to create a container and their functions. Building this container locally and then uploading it to a Portenta X8.
1716

1817
## Goals
1918

@@ -26,26 +25,125 @@ In this tutorial we will create a simple container that we can then upload to th
2625

2726
## Instructions
2827

29-
First we are going to write our Arduino Linux sketch. We will first create a file with the """" format. Open the file in the code editor of your choice, we can now start writng our Linux sketch.
28+
When running a container, it uses an isolated filesystem. This custom filesystem is provided by a container image. Since the image contains the container’s filesystem, it must contain everything needed to run an application - all dependencies, configuration, scripts, binaries, etc. The image also contains other configuration for the container, such as environment variables, a default command to run, and other metadata.
29+
30+
## Container File Structure
3031

32+
To create our container we need to collect our necessary files. Creating a folder called **hello-world**, then putting the following files in the folder:
33+
- docker-build.conf
34+
- docker-compose.yml
35+
- Dockerfile
36+
- requirements.txt
37+
- src folder
38+
- main.py (This file should be inside the src folder)
3139

32-
is a runnable instance of an image. You can create, start, stop, move, or delete a container using the DockerAPI or CLI.
33-
can be run on local machines, virtual machines or deployed to the cloud.
34-
is portable (can be run on any OS)
35-
Containers are isolated from each other and run their own software, binaries, and configurations.
40+
Lets go through what these files contain and do.
3641

37-
When running a container, it uses an isolated filesystem. This custom filesystem is provided by a container image. Since the image contains the container’s filesystem, it must contain everything needed to run an application - all dependencies, configuration, scripts, binaries, etc. The image also contains other configuration for the container, such as environment variables, a default command to run, and other metadata.
42+
### docker-buil.conf
43+
A file containing the minimal "unit test" command to be executed on the container to prove it's working.
44+
45+
```python
46+
TEST_CMD="python3 --help"
47+
```
48+
49+
### docker-compose.yml
50+
This file defines the app name through the factory, permissions and settings for the involved containers.
51+
52+
```python
53+
version: '3.6'
54+
55+
services:
56+
python-hello-world:
57+
image: blob-opera:latest
58+
restart: always
59+
tty: true
60+
read_only: true
61+
user: "63"
62+
tmpfs:
63+
- /run
64+
- /var/lock
65+
- /var/log
66+
- /tmp
67+
```
68+
69+
### Dockerfile
70+
This is used to build the container.
71+
72+
```python
73+
# Copyright (c) 2022 Arduino.cc
74+
#
75+
76+
# Examples:
77+
# docker build --tag "python-hello-world:latest" .
78+
# docker run -it --rm --user "63" python-hello-world:latest
79+
80+
FROM python:3-alpine3.15
81+
82+
LABEL maintainer="Massimo Pennazio <maxipenna@libero.it>"
83+
84+
# Set our working directory
85+
WORKDIR /usr/src/app
3886

39-
##
87+
# Copy requirements.txt first for better cache on later pushes
88+
COPY requirements.txt requirements.txt
4089

41-
###
90+
# pip install python deps from requirements.txt on the resin.io build server
91+
RUN pip install -r requirements.txt
92+
93+
# This will copy all files in our root to the working directory in the container
94+
COPY ./src/main.py ./
95+
96+
# Enable udevd so that plugged dynamic hardware devices show up in our container.
97+
ENV UDEV=1
98+
99+
# main.py will run when container starts up on the device
100+
CMD ["python","-u","main.py"]
101+
```
102+
103+
### requirements.txt
104+
105+
```python
106+
Flask==0.12.3
107+
```
108+
109+
### Source
110+
Here we will keep source code of the app you want to run in the container or simply a startup script. We will create a file and name it **main.py** in this folder. This script will ?????.
111+
112+
```python
113+
from flask import Flask
114+
app = Flask(__name__)
115+
116+
@app.route('/')
117+
def hello_world():
118+
return 'Hello World!'
119+
120+
if __name__ == '__main__':
121+
app.run(host='0.0.0.0', port=80)
122+
```
42123

43124
## Upload container
44125

45-
If you want to learn how to upload your newly created container to your Portenta X8, then please check out our [Uploading container tutorial](). It will show you how to add and remove containers from your Portenta X8 using SSH or ????.
126+
Using docker-compose
127+
128+
Should be the preferred way of testing app/containers since inside docker-compose.yml you specify a lot of settings that may not be trivial to convert to docker run arguments
129+
130+
```
131+
cd /home/fio/hello-world
132+
```
133+
```
134+
docker-compose up --detach
135+
```
136+
137+
Should start your application and register it as a systemd service that will be persistent
138+
139+
accross reboots (e.g. at next boot your docker-compose app will be executed automagically)
140+
141+
```
142+
docker-compose stop
143+
```
144+
This command will stop your docker-compose app from running
46145

47-
###
48146

49147
## Conclusion
50148

51-
Now you should have a better understanding of how the Portenta X8 works with factories and containers. This article also gives a better picture of how to utilize the Portenta X8 to its full potential. Be sure to check out our other tutorials with the Portenta X8 to see how to practically use factories and containers.
149+
This tutorial went through how to create a container for a script or app using Python. And then how to upload this container to a Portenta X8. This is a good method for creating and quickly testing containers. Allowing you to make sure a container works before pushing it to your factory.

0 commit comments

Comments
 (0)