From 37cfee3c3185c9d95f0df299623e9a3c0cdb83c4 Mon Sep 17 00:00:00 2001 From: Donovan Date: Thu, 5 Dec 2024 15:08:38 -0600 Subject: [PATCH] implement docker remote and local cron job --- Dockerfile | 18 ++++++++++++++---- Taskfile.yaml | 29 +++++++++++++++++++++++++++++ backup_cron | 4 +++- docker-compose.yaml | 2 +- docker_build.sh | 2 +- entrypoint.sh | 7 +++++++ local-compose.yaml | 15 +++++++++++++++ 7 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 Taskfile.yaml create mode 100644 entrypoint.sh create mode 100644 local-compose.yaml diff --git a/Dockerfile b/Dockerfile index b345c12..f83e812 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/Taskfile.yaml b/Taskfile.yaml new file mode 100644 index 0000000..14e82da --- /dev/null +++ b/Taskfile.yaml @@ -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 diff --git a/backup_cron b/backup_cron index 26e8e1a..d3fa52a 100644 --- a/backup_cron +++ b/backup_cron @@ -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 diff --git a/docker-compose.yaml b/docker-compose.yaml index 270f819..2761e90 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,5 +1,5 @@ services: - go-nkode: + cron-nkode: container_name: cron-nkode image: registry.infra.nkode.tech/cron-nkode volumes: diff --git a/docker_build.sh b/docker_build.sh index 5886794..96b23a6 100644 --- a/docker_build.sh +++ b/docker_build.sh @@ -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 . diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..e58135e --- /dev/null +++ b/entrypoint.sh @@ -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 diff --git a/local-compose.yaml b/local-compose.yaml new file mode 100644 index 0000000..1282b44 --- /dev/null +++ b/local-compose.yaml @@ -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