Cronjob added inside container

This commit is contained in:
2026-04-26 13:25:30 +02:00
parent a5aa6de67b
commit 2eaaf8d6d8
4 changed files with 36 additions and 8 deletions

View File

@@ -1,4 +1,4 @@
FROM ruby:2.7
FROM ruby:3.1
ENV BUILD_PACKAGES="apt-utils build-essential curl less nodejs sudo wget zsh libmariadb-dev libserialport-dev cron"
@@ -14,7 +14,18 @@ COPY Gemfile Gemfile.lock ./
RUN \
apt-get update -qq && \
apt-get install -y $BUILD_PACKAGES && \
bundle install
# Fix bundler version BEFORE bundle install
gem install bundler -v "$(grep -A 1 'BUNDLED WITH' Gemfile.lock | tail -n 1)" && \
bundle install && \
rm -rf /var/lib/apt/lists/*
# Copy cron file (if you use /etc/cron.d style)
COPY reportcron /etc/cron.d/reportcron
RUN chmod 0644 /etc/cron.d/reportcron && touch /var/log/cron.log
# Add entrypoint script
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["/bin/bash -c ruby ./smartmeter.rb"]

View File

@@ -4,7 +4,7 @@ services:
restart: unless-stopped
image: mysql:8.3
volumes:
- $PWD/data:/var/lib/mysql
- mysql-data:/var/lib/mysql
ports:
- 3306:3306
environment:
@@ -12,15 +12,19 @@ services:
MYSQL_DATABASE: smartmeter
smartmeter:
container_name: smartmeter
labels:
- diun.enable=false
restart: unless-stopped
build: .
environment:
MQTT_HOST: 10.0.0.240
command: 'ruby ./smartmeter.rb'
#devices:
# - "/dev/ttyUSB1:/dev/ttyUSB0"
devices:
- "/dev/ttyUSB0:/dev/ttyUSB0"
volumes:
- .:/usr/src/app
depends_on:
- db
volumes:
mysql-data:

10
docker-entrypoint.sh Normal file
View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -e
unset BUNDLE_PATH
unset BUNDLE_BIN
# Start cron in background
cron
# Start your Ruby process in foreground
ruby /usr/src/app/smartmeter.rb

3
reportcron Normal file
View File

@@ -0,0 +1,3 @@
PATH=/usr/local/bundle/bin:/usr/local/bundle/gems/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
GEM_HOME=/usr/local/bundle
1 7 * * * root cd /usr/src/app && bundle exec ruby report_mailer.rb >> /var/log/cron.log 2>&1