[Django]-Cronjob in docker container cannot connect to other container

3👍

environment:
  - DB_HOST=db
  - DB_NAME=app
  - DB_USER=postgres
  - DB_PASS=supersecretpassword

I see that you are passing the environment variables via docker-compose like this. This is fine when the container is running the command directly inside the shell.

However, when putting it inside the crontab, the cronjob will run your command in a separate fresh shell with no environment being passed in at all.

To work around this problem, you can create a separate shell script:

cat <<EOF > /temp/script.sh
#!/bin/bash
export DB_HOST=db
export DB_NAME=app
export DB_USER=postgres
export DB_PASS=supersecretpassword

cd /tourMamaRoot/tourMama/cronjob && fetch_news.py >> /var/log/cron.log 2>&1
EOF

chmod +x /temp/script.sh

and edit your crontab like this:

* * * * * /temp/script.sh

Leave a comment