implement docker remote and local cron job

This commit is contained in:
2024-12-05 15:08:38 -06:00
parent 1f6247d3d3
commit 37cfee3c31
7 changed files with 70 additions and 7 deletions

View File

@@ -1,6 +1,10 @@
# Use Ubuntu 20.04 as the base image
FROM ubuntu:20.04
# Set environment variables to avoid interactive prompts
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=America/Chicago
# Install dependencies
RUN apt-get update && apt-get install -y \
sqlite3 \
@@ -10,11 +14,12 @@ RUN apt-get update && apt-get install -y \
gzip \
&& rm -rf /var/lib/apt/lists/*
ARG CACHE_BUST=1
# Create necessary directories
RUN mkdir -p /jobs/data/sqlite
RUN mkdir -p /var/tmp
# Copy the backup script into the container
COPY backup_sqlite.sh /usr/local/bin/backup_script.sh
# Make the script executable
@@ -23,7 +28,6 @@ RUN chmod +x /usr/local/bin/backup_script.sh
# Create the cron log file
RUN touch /var/log/cron.log
# Copy the cron job file into the cron.d directory
COPY backup_cron /etc/cron.d/backup_cron
# Set permissions for the cron job file
@@ -35,5 +39,11 @@ RUN crontab /etc/cron.d/backup_cron
# Expose the cron log file as a volume
VOLUME /var/log
# Start cron in the foreground when the container starts
CMD ["cron", "-f"]
# Copy the entrypoint script
COPY entrypoint.sh /entrypoint.sh
# Make the entrypoint script executable
RUN chmod +x /entrypoint.sh
# Use the entrypoint script to start the container
CMD ["/entrypoint.sh"]

29
Taskfile.yaml Normal file
View File

@@ -0,0 +1,29 @@
version: "3"
vars:
compose_file: "local-compose.yaml"
cache_bust:
sh: "date +%s"
tasks:
build:
cmds:
- docker compose -f {{.compose_file}} build --build-arg CACHE_BUST={{.cache_bust}}
up:
cmds:
- CACHE_BUST={{.cache_bust}} docker compose -f {{.compose_file}} up --build
start:
cmds:
- docker compose -f {{.compose_file}} up -d
down:
cmds:
- docker compose -f {{.compose_file}} down
clean:
cmds:
- docker system prune -f
push:
cmds:
- docker buildx build --build-arg CACHE_BUST={{.cache_bust}} --platform linux/amd64,linux/arm64 -t registry.infra.nkode.tech/cron-nkode:latest --push .
exec:
cmds:
- docker exec -it cron-nkode bash

View File

@@ -1 +1,3 @@
0 2 * * * root /usr/local/bin/backup_script.sh -l true >> /var/log/cron.log 2>&1
SHELL=/bin/bash
BASH_ENV=/etc/environment
00 13 * * * /usr/local/bin/backup_script.sh -l true >> /var/log/cron.log 2>&1

View File

@@ -1,5 +1,5 @@
services:
go-nkode:
cron-nkode:
container_name: cron-nkode
image: registry.infra.nkode.tech/cron-nkode
volumes:

View File

@@ -1 +1 @@
docker buildx build --platform linux/amd64,linux/arm64 -t registry.infra.nkode.tech/cron-nkode:latest --push .
docker buildx build --build-arg CACHE_BUST="$(date +%s)" --platform linux/amd64,linux/arm64 -t registry.infra.nkode.tech/cron-nkode:latest --push .

7
entrypoint.sh Normal file
View File

@@ -0,0 +1,7 @@
#!/bin/bash
# Export AWS environment variables to /etc/environment
printenv | grep '^AWS_' >> /etc/environment
# Start cron in the foreground
cron -f

15
local-compose.yaml Normal file
View File

@@ -0,0 +1,15 @@
services:
cron-nkode:
container_name: cron-nkode
build:
context: .
dockerfile: Dockerfile
args:
CACHE_BUST: "${CACHE_BUST}"
volumes:
- /var/go-nkode/sqlite:/jobs/data/sqlite
environment:
- AWS_ACCESS_KEY_ID=AKIA5VCWLAGKTQVD3RDJ
- AWS_SECRET_ACCESS_KEY=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
- AWS_REGION=us-east-1