To access Docker environment variables from a cron job, you can use the -e option when running the docker run command to pass the environment variables to the container, and then use them in your cron job command.
🔭 Want to get alerted when your Cron doesn’t run correctly?
Go to Better Stack and start monitoring in 5 minutes.
Here's an example of how you can do it:
- Define your environment variable(s) when running your Docker container:
- In your cron job command, use the environment variable(s) like this:
This will set the VAR_NAME environment variable for the env command, which will then be available for the rest of the command. Replace /path/to/command with the command you want to run.
Here's a breakdown of the format:
- The first
0represents the minute (0-59) - The second
0represents the hour (0-23) - The
*in the third, fourth, and fifth positions represent the day of the month, the month, and the day of the week (respectively). Using*means the job will run every day of the month, every month, and every day of the week. - The sixth field is the command to be executed.
- The
/usr/bin/envcommand is used to set the environment variable for the command that follows. - The
VAR_NAME=$VAR_NAMEsets the environment variable in the command. ReplaceVAR_NAMEwith the name of the environment variable you want to use.
So the cron job in the above format will run the command with the environment variable defined in the Docker container.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.