2

I am trying to create a container with django and a container with a crontab. Where the crontab container is launching a django managment command. I saw their is a django app for it but i wanted to try it the hard way first :)

So the unexpected behavior is that i am setting an env var in the docker-compose.yml file and i cant see it in the my_command.sh ran by crontab.

First i run docker-compose up -d --build

  • when followed by docker-compose run mycron /bin/bash i cant see SOME_KEY

  • when followed by docker-compose run mycron /bin/bash with the entrypoint commented out i can see SOME_KEY by running printenv or sh my_command.sh

docker-compose.yml

version: '3.8'
services:
  myapp:
    build: ./app
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - my_volume:/home/app
    ports:
      - 8000:8000
    environment:
      - SOME_KEY=some_value
  mycron:
    build: ./app
    volumes:
      - my_volume:/home/app
    entrypoint: sh /home/app/crontab_entry.sh
    environment:
      - SOME_KEY=some_value
volumes:
  my_volume:

crontab_entry.sh:

#!/usr/bin/env bash

touch /home/app/logs/crontab.log
chmod a+x /home/app/my_command.sh
echo "* * * * * /home/app/my_command.sh >> /home/app/logs/crontab.log 2>&1" > /etc/crontab
crontab /etc/crontab
/usr/sbin/service cron start
tail -f /home/app/logs/crontab.log

my_command.sh:

#!/bin/sh

printenv
#cd "$(dirname "$0")";
#/usr/local/bin/python3.8 manage.py trade

Dockerfile

FROM python:3.8.1-slim-buster
RUN apt-get update --yes --quiet && apt-get install --yes --quiet --no-install-recommends \
    cron \
 && rm -rf /var/lib/apt/lists/*

RUN mkdir -p /home/app/logs

WORKDIR /home/app

COPY . .
RUN pip install -r ./requirements.txt
Gerrit Geeraerts
  • 924
  • 1
  • 7
  • 14
  • 2
    You have to define your specific environmental variables at the top of your crontab file or in front of the command to be run. See https://stackoverflow.com/questions/2229825/where-can-i-set-environment-variables-that-crontab-will-use. – fpmurphy Feb 10 '22 at 03:49

0 Answers0