You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Create and Upload a Custom Container Portenta X8
3
3
difficulty: easy
4
-
tags: [Linux, containers]
4
+
tags: [Linux, Python, Containers]
5
5
description: This tutorial will show you how to create and upload your custom container to your Portenta X8
6
6
author: Benjamin Dannegård
7
7
hardware:
8
8
- hardware/04.pro/board/portenta-x8
9
9
software:
10
-
-
11
-
10
+
- adb
12
11
---
13
12
14
13
## Overview
15
14
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.
17
16
18
17
## Goals
19
18
@@ -26,26 +25,125 @@ In this tutorial we will create a simple container that we can then upload to th
26
25
27
26
## Instructions
28
27
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
30
31
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)
31
39
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.
36
41
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.
# Copy requirements.txt first for better cache on later pushes
88
+
COPY requirements.txt requirements.txt
40
89
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
+
ENVUDEV=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
+
defhello_world():
118
+
return'Hello World!'
119
+
120
+
if__name__=='__main__':
121
+
app.run(host='0.0.0.0', port=80)
122
+
```
42
123
43
124
## Upload container
44
125
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
46
145
47
-
###
48
146
49
147
## Conclusion
50
148
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