crontab任务python程序如何读取docker-compose里的environment变量

crontab任务python程序如何读取docker-compose里的environment变量

crontab任务python程序能否直接读取docker-compose里的environment变量?

我知道在不使用crontab下,可以用os.getenv(变量名)来引入,但如果使用crontab情况下该怎么引入呢?

文件目录

.

├── deploy

│   ├── crontab

│   ├── docker-compose.yml

│   └── Dockerfile

└── demo.py

docker-compose.yml

version: '3.5'

services:

demo:

build:

context: ..

dockerfile: ./deploy/Dockerfile

container_name: demo

restart: always

volumes:

- $HOME/logs:/work/logs

environment:

- PROD=1

- MYSQL_HOST=127.0.0.1

- MYSQL_DATABASE=demo

- MYSQL_USER=user_name

- MYSQL_PASSWORD=password

- REDIS_HOST=127.0.0.1

Dockerfile

FROM python:3.6.9-slim-buster

WORKDIR /work

RUN apt-get -y update && apt-get -y upgrade \

&& apt-get install -y cron \

&& apt-get clean && \

rm -rf /var/lib/apt/lists/* \

&& touch /var/log/cron.log \

&& mkdir -p /work/logs

ADD . /work

COPY ./deploy/crontab /etc/cron.d/cjob

RUN chmod 0644 /etc/cron.d/cjob

ENV PYTHONUNBUFFERED 1

CMD cron -f

crontab

SHELL=/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

* * * * * root python /work/demo.py >> /work/logs/demo.log 2>&1

demo.py

import os

import datetime

print('PROD: ', os.getenv('PROD'))

print(f"## {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")

print(f"MYSQL_HOST: {os.getenv('MYSQL_HOST')} REDIS_HOST: {os.getenv('REDIS_HOST')}")

实际输出:

PROD:  None

## 2021-02-05 17:53:01

MYSQL_HOST: None REDIS_HOST: None


回答:

问题终于解决了。crontab的环境变量与系统的环境变量确实不一样,如此解决思路便是使其所在环境变量一致即可。

打印crontab环境变量:

SHELL=/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

* * * * * root env >> /work/logs/demo.log 2>&1

可以看出两者环境变量确实不一样:

$ docker exec -it demo bash

root@b6b6fd26170b:/work# env

PYTHONUNBUFFERED=1

HOSTNAME=xxx

PYTHON_VERSION=3.6.9

REDIS_HOST=127.0.0.1

PWD=/work

MYSQL_PASSWORD=password

MYSQL_USER=user_name

HOME=/root

LANG=C.UTF-8

GPG_KEY=xxx

MYSQL_HOST=127.0.0.1

TERM=xterm

SHLVL=1

PROD=1

MYSQL_DATABASE=demo

PYTHON_PIP_VERSION=19.3.1

PYTHON_GET_PIP_SHA256=b86f36cc4345ae87bfd4f10ef6b2dbfa7a872fbff70608a1e43944d283fd0eee

PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/ffe826207a010164265d9cc807978e3604d18ca0/get-pip.py

PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

_=/usr/bin/env

root@b6b6fd26170b:/work# tail -n 8 logs/demo.log

###################

SHELL=/bin/bash

PWD=/root

LOGNAME=root

HOME=/root

SHLVL=1

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

_=/usr/bin/env

解决方案是让shell包裹python程序,然后用crontab调度shell脚本。

其中shell需要引入系统的环境变量:

#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

export $(cat /proc/1/environ |tr '\0' '\n' | xargs)

python /work/demo.py >> /work/logs/demo.log

crontab调度shell脚本:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

* * * * * /work/deploy/start.sh

如此python便能正常引入环境变量

PROD:  1

## 2021-02-08 17:32:01

MYSQL_HOST: 127.0.0.1 REDIS_HOST: 127.0.0.1

参考链接:

https://www.tony-yin.site/2018/10/29/Why-Crontab-Not-Work/#footer

https://www.cnblogs.com/xuxinkun/p/10531091.html


回答:

如果只是读取镜像中的 env, 你找个能解析 yml 格式的库, 读 docker-compose.yml 文件就行

如果读运行中的容器内的环境变量, 就用 docker exec 方法
比如容器名叫 workspace-centos
就执行:

docker exec  workspace-centos env

以上是 crontab任务python程序如何读取docker-compose里的environment变量 的全部内容, 来源链接: utcz.com/a/158874.html

回到顶部