Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for TP4 in Docker #626

Merged
merged 56 commits into from
Nov 22, 2021
Merged

Added support for TP4 in Docker #626

merged 56 commits into from
Nov 22, 2021

Conversation

alec-pinson
Copy link
Contributor

@alec-pinson alec-pinson commented Nov 12, 2021

TLDR @ BOTTOM

Related Issue

[FEATURE]: Docker Image - #620

Information

Allows TP4 to run within a Docker container, this gives many benefits:

  • fully portable and provides quick & easy installs/upgrades
  • easy testing/switching between versions
  • will work on any Raspberry Pi OS as long as Docker is installed
  • no changes are made to underlying OS (apart from hardware flags, i2c, serial, camera etc)

Checklist

BREAKING CHANGE NOTICE

There is some breaking changes here for previous versions which is due to needing to move the database/calendar/logging from
/TerrariumPI/terrariumpi.db to /TerrariumPI/data/terrariumpi.db
/TerrariumPI/calendar.ics to /TerrariumPI/data/calendar.ics
/TerrariumPI/logging.custom.cfg to /TerrariumPI/log/logging.custom.cfg


Notes

I have successfully tested with a SHT3x humidity/temperature sensor (this is all I have at the moment)

If anyone would like to test the image I have temporarily pushed it to Dockerhub. Alternatively you can build the image yourself from this branch (see below). You will need Docker and Docker-compose installed. Instructions can be found here (not yet tested) - https://www.upswift.io/post/install-docker-compose-on-raspberry-pi for this.

Once installed you will need to copy the below to a file named docker-compose.yaml

version: "3.3"
services:
  terrariumpi:
    image: wkd1/test:remove-old-packages
    volumes:
      - /opt/terrariumpi/logs:/TerrariumPI/log
      - /opt/terrariumpi/data:/TerrariumPI/data
      - /opt/terrariumpi/media:/TerrariumPI/media
      - /opt/terrariumpi/webcam-archive:/TerrariumPI/webcam/archive
      - /opt/terrariumpi/DenkoviRelayCommandLineTool:/TerrariumPI/3rdparty/DenkoviRelayCommandLineTool
      - /boot/config.txt:/boot/config.txt
      - /boot/cmdline.txt:/boot/cmdline.txt
      - /etc/modules:/etc/modules
      - /opt/vc/bin:/opt/vc/bin
      - /dev:/dev
    network_mode: host
    restart: always
    privileged: true
    stop_grace_period: 1m
    stop_signal: SIGINT
    environment:
      TZ: "Europe/Amsterdam" # timezone list can be found here https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Then run the below to start in detached mode:

docker-compose up -d

Useful commands

You can view the running container with:

docker ps

You can stop the running container with:

docker-compose down

You can view/tail the logs with:

docker-compose logs -f

There are some extra options you can provide see below example.

Docker Environment Options

Option Description Default
TZ Allows you to set your timezone. A list of timezones can be found here Europe/Amsterdam
ENABLE_I2C Updates /boot/config.txt and /etc/modules to enable the i2c interface (requires reboot if not already enabled). If set to false will not make any changes to /boot/config.txt or /etc/modules. true
ENABLE_1_WIRE Updates /boot/config.txt to enable the 1 wire interface (requires reboot if not already enabled). If set to false will not make any changes to /boot/config.txt. true
ENABLE_CAMERA Updates /boot/config.txt to enable the camera support (requires reboot if not already enabled). If set to false will not make any changes to /boot/config.txt. true
ENABLE_SERIAL Updates /boot/config.txt to enable the serial interface (requires reboot if not already enabled). If set to false will not make any changes to /boot/config.txt. true
ENABLE_CO2_SENSORS Updates /boot/cmdline.txt to enable support for CO2 sensors (requires reboot if not already enabled). If set to false will not make any changes to /boot/cmdline.txt. true
AUTO_REBOOT Will automatically reboot if required unless set to false. true

How to pass environment options:

version: "3.3"
services:
  terrariumpi:
    image: wkd1/test:remove-old-packages
    volumes:
      - /opt/terrariumpi/logs:/TerrariumPI/log
      - /opt/terrariumpi/data:/TerrariumPI/data
      - /opt/terrariumpi/media:/TerrariumPI/media
      - /opt/terrariumpi/webcam-archive:/TerrariumPI/webcam/archive
      - /opt/terrariumpi/DenkoviRelayCommandLineTool:/TerrariumPI/3rdparty/DenkoviRelayCommandLineTool
      - /boot/config.txt:/boot/config.txt
      - /boot/cmdline.txt:/boot/cmdline.txt
      - /etc/modules:/etc/modules
      - /opt/vc/bin:/opt/vc/bin
      - /dev:/dev
    network_mode: host
    restart: always
    privileged: true
    stop_grace_period: 1m
    stop_signal: SIGINT
    environment:
      TZ: "Europe/Amsterdam" # timezone list can be found here https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
      ENABLE_I2C: "false"
      ENABLE_1_WIRE: "false"
      ENABLE_CAMERA: "false"
      ENABLE_SERIAL: "false"
      ENABLE_CO2_SENSORS: "false"
      AUTO_REBOOT: "false"

Docker Build

Build time: 1 hour depending on the speed of your machine, once main layers are cached this time reduces to a minute or so
Build command: Build for Raspberry Pi architecture only

docker buildx build --platform linux/arm/v7 --progress=plain -t theyosh/terrariumpi:4.0.0 .

Size: 475MB

TLDR;

  1. Install Docker & Docker-compose
  2. Create file called docker-compose.yaml with contents:
version: "3.3"
services:
  terrariumpi:
    image: wkd1/test:remove-old-packages
    volumes:
      - /opt/terrariumpi/logs:/TerrariumPI/log
      - /opt/terrariumpi/data:/TerrariumPI/data
      - /opt/terrariumpi/media:/TerrariumPI/media
      - /opt/terrariumpi/webcam-archive:/TerrariumPI/webcam/archive
      - /opt/terrariumpi/DenkoviRelayCommandLineTool:/TerrariumPI/3rdparty/DenkoviRelayCommandLineTool
      - /boot/config.txt:/boot/config.txt
      - /boot/cmdline.txt:/boot/cmdline.txt
      - /etc/modules:/etc/modules
      - /opt/vc/bin:/opt/vc/bin
      - /dev:/dev
    network_mode: host
    restart: always
    privileged: true
    stop_grace_period: 1m
    stop_signal: SIGINT
    environment:
      TZ: "Europe/Amsterdam" # timezone list can be found here https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  1. Run docker-compose up

There is a breaking change here for previous versions which is due to needing to move the database to /TerrariumPI/data
@theyosh
Copy link
Owner

theyosh commented Nov 12, 2021

Hi, thanks for the update. Nice work. I have a found a list of 'issues' and feedback for this. And I do not know what is possible.

But i think you also need to 'patch' the calendar. As I create an *.ics file. https://github.com/theyosh/TerrariumPI/blob/4.x.y.z/terrariumCalendar.py#L13 and custom logging: https://github.com/theyosh/TerrariumPI/blob/4.x.y.z/terrariumLogging.py#L83

I would put the calendar at the 'data' volume and the custom logging config file in the log volume?

And I expect Bluetooth not to be working which we do need. https://github.com/theyosh/TerrariumPI/blob/4.x.y.z/install.sh#L282 here I set some special rights to a certain file so a normal user can run it. Else I need root rights to use Bluetooth.

And how about the following install lines:
https://github.com/theyosh/TerrariumPI/blob/4.x.y.z/install.sh#L124-L134 those are needed for certain usb devices. How does that work in Docker?

And how is the correct timezone configured? Is that depending on the host or can the container run in its own timezone? Here https://github.com/theyosh/TerrariumPI/blob/4.x.y.z/install.sh#L74 I force the user to select the correct timezone so that I can convert date and time to the correct timezone of the user.

I do miss the application pigpiod with package pigpio which is running a service/daemon which reads out some GPIO sensors.

And I see you are using a build stage. So is it possible to add this: https://theyosh.github.io/TerrariumPI/hardware/relay/energenie-usb/#outdated-software The package sispmctl is outdated. And I do not build it during install, as it takes time. But when we create a docker image we can do this kind of building... Right?

And there are some people using: https://github.com/theyosh/TerrariumPI/tree/4.x.y.z/3rdparty/DenkoviRelayCommandLineTool But I guess just adding a volume is not enough. Because a volume cannot have this readme inside it during install. It needs to be clean the first time you add/create a volume? Or is there a trick....

Then I have a special setup for logging. As it does lot of writing. I write it to memory, and once a day it will be rotated at disk. This way, I have reduced the writing on the SD a lot. https://github.com/theyosh/TerrariumPI/blob/4.x.y.z/install.sh#L312-L318
Is this possible with Docker? If not, then I think we need to add a remark about this. That the SD can wear down a bit faster.

And I got some feedback that Raspian Bullseye is not working. So Buster does work. And I do not know if it will work with Docker if the host is Bullseye.#622 and #623

So I hope this is not scaring you. You have done a lot, but there are still some 'issues' left.

And I can update my installer to move some existing files like the database and calendar file. That way, the file structure is the same for Docker and native/direct.

And again, thanks for you time for testing and trying to make this work

@alec-pinson
Copy link
Contributor Author

Hi, this feedback is great, it's nice to have someone that can provide this kind of information so thank you.

As there is quite a lot to work through I have tried to convert each item you listed to an issue and added them to the checklist above. I created the issues on my PR fork (https://github.com/alec-pinson/TerrariumPI/issues) so we can get them all (hopefully) resolved prior to full PR review. I have commented on each one if you have time to check them that would be great!

Thanks for your time also :)

@theyosh
Copy link
Owner

theyosh commented Nov 13, 2021

So I am trying to build the docker image on my Pi. Just to see how it works, and first thing I have, is that my docker is to old. It does not understand the argument --platform. So I have to install docker according to: https://phoenixnap.com/kb/docker-on-raspberry-pi in order to be able to make the build.

Something for the documentation of this?

So, I can make some changes, and make PRs on your version per issue. I am not sure what is the most used way for this.

@theyosh
Copy link
Owner

theyosh commented Nov 13, 2021

For me the build instruction docker buildx build --platform linux/arm/v7 --progress=plain -t theyosh/terrariumpi:4.0.0 . give an error:

#6 [stage-2 2/7] RUN apt-get update && apt-get install -y --no-install-recommends gnupg ca-certificates &&   echo "deb http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi" > /etc/apt/sources.list.d/raspberrypi.list &&   echo "deb http://archive.raspberrypi.org/debian/ buster main" >> /etc/apt/sources.list.d/raspberrypi.list &&   echo "deb [arch=armhf] https://download.docker.com/linux/raspbian buster stable" > /etc/apt/sources.list.d/docker.list &&   apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 9165938D90FDDD2E 82B129927FA3303E 7EA0A9C3F273FCD8 &&   rm -rf /var/lib/apt/lists/* &&   apt-get update &&   apt-get install -y --no-install-recommends bc screen git watchdog i2c-tools pigpio sqlite3 ffmpeg sispmctl ntp libxslt1.1 libglib2.0-dev &&   apt-get install -y --no-install-recommends libopenexr23 libilmbase23 liblapack3 libatlas3-base &&   apt-get install -y --no-install-recommends python3-opencv libftdi1 libasound-dev &&   apt-get autoremove -y && rm -rf /var/lib/apt/lists/*
#6 0.403 standard_init_linux.go:228: exec user process caused: exec format error
#6 ERROR: executor failed running [/bin/sh -c apt-get update && apt-get install -y --no-install-recommends gnupg ca-certificates &&   echo "deb http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi" > /etc/apt/sources.list.d/raspberrypi.list &&   echo "deb http://archive.raspberrypi.org/debian/ buster main" >> /etc/apt/sources.list.d/raspberrypi.list &&   echo "deb [arch=armhf] https://download.docker.com/linux/raspbian buster stable" > /etc/apt/sources.list.d/docker.list &&   apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 9165938D90FDDD2E 82B129927FA3303E 7EA0A9C3F273FCD8 &&   rm -rf /var/lib/apt/lists/* &&   apt-get update &&   apt-get install -y --no-install-recommends bc screen git watchdog i2c-tools pigpio sqlite3 ffmpeg sispmctl ntp libxslt1.1 libglib2.0-dev &&   apt-get install -y --no-install-recommends libopenexr23 libilmbase23 liblapack3 libatlas3-base &&   apt-get install -y --no-install-recommends python3-opencv libftdi1 libasound-dev &&   apt-get autoremove -y && rm -rf /var/lib/apt/lists/*]: exit code: 1

#7 [remove_git_dir 1/4] FROM docker.io/library/python:3.8.0@sha256:adb48bf76e44cd7d74607db157bba756368af42196aaea945e2114d957cb5558
#7 sha256:6bac76f7ec397955852a282870ca77ba8bc17beaebffc52bc9053aaa18afc45f 45.86MB / 45.86MB 39.9s done
#7 sha256:6a69590e0d6b359167e2d103e39617e28e2d5da74e7327194e72a2a0b4828107 53.48MB / 168.24MB 40.5s
#7 sha256:839c43046833dc87303eef175734beaf61a985a5e570bc0f920fa03a201f45c2 1.05MB / 5.20MB 40.5s
#7 sha256:24c08f485be984f84d1220afcc45f7f5e47db68c99ba646c47fcdbba19d773a4 0B / 26.74MB 40.5s
#7 extracting sha256:6bac76f7ec397955852a282870ca77ba8bc17beaebffc52bc9053aaa18afc45f 0.5s
#7 CANCELED
------
 > [stage-2 2/7] RUN apt-get update && apt-get install -y --no-install-recommends gnupg ca-certificates &&   echo "deb http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi" > /etc/apt/sources.list.d/raspberrypi.list &&   echo "deb http://archive.raspberrypi.org/debian/ buster main" >> /etc/apt/sources.list.d/raspberrypi.list &&   echo "deb [arch=armhf] https://download.docker.com/linux/raspbian buster stable" > /etc/apt/sources.list.d/docker.list &&   apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 9165938D90FDDD2E 82B129927FA3303E 7EA0A9C3F273FCD8 &&   rm -rf /var/lib/apt/lists/* &&   apt-get update &&   apt-get install -y --no-install-recommends bc screen git watchdog i2c-tools pigpio sqlite3 ffmpeg sispmctl ntp libxslt1.1 libglib2.0-dev &&   apt-get install -y --no-install-recommends libopenexr23 libilmbase23 liblapack3 libatlas3-base &&   apt-get install -y --no-install-recommends python3-opencv libftdi1 libasound-dev &&   apt-get autoremove -y && rm -rf /var/lib/apt/lists/*:
#6 0.403 standard_init_linux.go:228: exec user process caused: exec format error
------
error: failed to solve: executor failed running [/bin/sh -c apt-get update && apt-get install -y --no-install-recommends gnupg ca-certificates &&   echo "deb http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi" > /etc/apt/sources.list.d/raspberrypi.list &&   echo "deb http://archive.raspberrypi.org/debian/ buster main" >> /etc/apt/sources.list.d/raspberrypi.list &&   echo "deb [arch=armhf] https://download.docker.com/linux/raspbian buster stable" > /etc/apt/sources.list.d/docker.list &&   apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 9165938D90FDDD2E 82B129927FA3303E 7EA0A9C3F273FCD8 &&   rm -rf /var/lib/apt/lists/* &&   apt-get update &&   apt-get install -y --no-install-recommends bc screen git watchdog i2c-tools pigpio sqlite3 ffmpeg sispmctl ntp libxslt1.1 libglib2.0-dev &&   apt-get install -y --no-install-recommends libopenexr23 libilmbase23 liblapack3 libatlas3-base &&   apt-get install -y --no-install-recommends python3-opencv libftdi1 libasound-dev &&   apt-get autoremove -y && rm -rf /var/lib/apt/lists/*]: exit code: 1

Im an running ubuntu focal (20.04). So, how does that cross building work?

@alec-pinson
Copy link
Contributor Author

So I am trying to build the docker image on my Pi. Just to see how it works, and first thing I have, is that my docker is to old. It does not understand the argument --platform. So I have to install docker according to: https://phoenixnap.com/kb/docker-on-raspberry-pi in order to be able to make the build.

Something for the documentation of this?

So, I can make some changes, and make PRs on your version per issue. I am not sure what is the most used way for this.

If you're building on an actual Pi you should need to specify the --platform argument, this is only required when building on another architecture (I build on a Mac). I was going to look at setting up a workflow to build the image but I'm not sure how cost works around those.

@alec-pinson
Copy link
Contributor Author

0.403 standard_init_linux.go:228: exec user process caused: exec format error

Hmm I think this error normally relates to trying to run an application on the wrong architecture.

I'd never built cross-architecture images before but this was the example I used: https://www.docker.com/blog/multi-arch-images/
I think maybe if you do docker buildx ls then linux/arm/v7 is maybe missing at the moment?
If it is/or isn't it's probably a good idea to do docker buildx create --name mybuilder which apparently gives access to new multi-arch features.

@theyosh
Copy link
Owner

theyosh commented Nov 13, 2021

Another question about this. Why is my image 1.16GB? Sounds a bit big....

docker image ls
REPOSITORY   TAG           IMAGE ID       CREATED        SIZE
wkd1/test    new-slim-10   75986ec2b904   27 hours ago   1.16GB

@alec-pinson
Copy link
Contributor Author

Another question about this. Why is my image 1.16GB? Sounds a bit big....

docker image ls
REPOSITORY   TAG           IMAGE ID       CREATED        SIZE
wkd1/test    new-slim-10   75986ec2b904   27 hours ago   1.16GB

Ahh right docker compresses the images, that will be the size of the image once unpacked.
https://hub.docker.com/repository/docker/wkd1/test/tags?page=1&ordering=last_updated

@alec-pinson
Copy link
Contributor Author

By the way if you ever need to full clear docker cache this command is very useful

docker system prune -a

I think buildx has its own aswell maybe something like

docker buildx prune -a

This will remove cached image layers and make build times slower for future but still useful to know if you're struggling for space.

@alec-pinson
Copy link
Contributor Author

@theyosh if you can test this image wkd1/test:remove-old-packages and if all good I think we're good?

I can create a PR afterwards for any missing documentation.

@alec-pinson
Copy link
Contributor Author

Just spotted the watchdog/healthcheck issue, added that to the list.

@theyosh
Copy link
Owner

theyosh commented Nov 18, 2021

Maybe a bit late, but I have managed to check which OS packages are needed. I did not test your version, because I build my own yesterday. And this is working for me.

during build stage:

  apt-get install -y --no-install-recommends libxslt1.1 libftdi1 libasound-dev libglib2.0-dev && \
  apt-get install -y --no-install-recommends python3-opencv

during image make stage:

  apt-get install -y --no-install-recommends sudo pigpio ffmpeg && \
  apt-get install -y --no-install-recommends python3-opencv libftdi1 libxslt1.1 && \
  mkdir -p /usr/share/man/man1 && apt-get install -y --no-install-recommends openjdk-11-jre-headless && \

@theyosh
Copy link
Owner

theyosh commented Nov 18, 2021

I am now building a new image for testing the healthcheck

@theyosh
Copy link
Owner

theyosh commented Nov 20, 2021

Added a new line to the Dockerfile: RUN echo '[ ! -z "$TERM" -a -r /TerrariumPI/motd.sh ] && /TerrariumPI/motd.sh' >> /etc/bash.bashrc just before the healthcheck line. This will show a nice message when you attach to the image (using bash)

@theyosh
Copy link
Owner

theyosh commented Nov 20, 2021

So, I think we can merge this. If you could add the line above to your Dockerfile, and update with my 4.x.y.z branch, then this PR can be merged. I made some updates in the documentation folder.

Dockerfile Outdated Show resolved Hide resolved
@theyosh
Copy link
Owner

theyosh commented Nov 21, 2021

No problem, just synced everything, and I am now building a complete new image (cleared build cache).

But I think you should get the credits for this, so that is why I asked.

@alec-pinson alec-pinson marked this pull request as ready for review November 21, 2021 09:44
@alec-pinson
Copy link
Contributor Author

I think this should now be ready? I'm currently building a local version to test also

@theyosh
Copy link
Owner

theyosh commented Nov 21, 2021

If you are testing a version, just try to attach it with a bash shell :) Should be nice...

And yes, I think we are there. I did found something that could help the health check, but I will fix that after the merge. It is not that big of a issue.

@alec-pinson
Copy link
Contributor Author

#12 106.5 Err:8 http://archive-bm.raspbian.org/raspbian buster/main armhf gcc-8-base armhf 8.3.0-6+rpi1
#12 106.5   Could not connect to archive-bm.raspbian.org:80 (5.153.225.231), connection timed out
#12 106.5 Err:27 http://archive-bm.raspbian.org/raspbian buster/main armhf libstdc++6 armhf 8.3.0-6+rpi1
#12 106.5   Unable to connect to archive-bm.raspbian.org:http:
#12 106.5 Err:38 http://archive-bm.raspbian.org/raspbian buster/main armhf libgcc1 armhf 1:8.3.0-6+rpi1
#12 106.5   Unable to connect to archive-bm.raspbian.org:http:
#12 106.5 Err:39 http://archive-bm.raspbian.org/raspbian buster/main armhf librsvg2-2 armhf 2.44.10-2.1+rpi1
#12 106.5   Unable to connect to archive-bm.raspbian.org:http:
#12 106.5 Err:50 http://archive-bm.raspbian.org/raspbian buster/main armhf libshine3 armhf 3.1.1-2+b1
#12 106.5   Unable to connect to archive-bm.raspbian.org:http:
#12 106.5 Err:62 http://archive-bm.raspbian.org/raspbian buster/main armhf libgomp1 armhf 8.3.0-6+rpi1
#12 106.5   Unable to connect to archive-bm.raspbian.org:http:
#12 106.5 Err:74 http://archive-bm.raspbian.org/raspbian buster/main armhf libogg0 armhf 1.3.2-1+b2
#12 106.5   Unable to connect to archive-bm.raspbian.org:http:
#12 106.5 Err:87 http://archive-bm.raspbian.org/raspbian buster/main armhf libx264-155 armhf 2:0.155.2917+git0a84d98-2+rpi1
#12 106.5   Unable to connect to archive-bm.raspbian.org:http:
#12 106.5 Err:96 http://archive-bm.raspbian.org/raspbian buster/main armhf libatomic1 armhf 8.3.0-6+rpi1
#12 106.5   Unable to connect to archive-bm.raspbian.org:http:

I've just come back and this bit is failing not sure why yet but I've gotta go out again now, will try figure it out later.

@alec-pinson
Copy link
Contributor Author

image

Looks good I finally finished my local build, it had some issues Sunday (I think maybe some apt repos were down)

@alec-pinson
Copy link
Contributor Author

Before merge, have you setup DOCKERHUB_USERNAME and DOCKERHUB_TOKEN repository secrets?

@ghost
Copy link

ghost commented Nov 22, 2021

Before merge, have you setup DOCKERHUB_USERNAME and DOCKERHUB_TOKEN repository secrets?

Those have been setup for a week now :) I think, I will merge this tonight, as I am currently at work.

…imeout. So timeout needs 60 seconds more then the startup timeout
@theyosh theyosh merged commit 9d1d01b into theyosh:4.x.y.z Nov 22, 2021
@theyosh theyosh deleted the 4.x.y.z-docker branch November 22, 2021 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants