在我的Python应用程序中,我试图访问/打印出一个环境变量,但它打印出None。
logger.info(f"OIDC SECRET: {os.getenv('OIDC_CLIENT_SECRET')}")
这是我在 github actions 中的步骤/工作
name: Build and push image to harbor
run: |
IMAGE_VERSION=$GITHUB_RUN_NUMBER
IMAGE_WITH_TAG="$IMAGE_NAME:develop.$IMAGE_VERSION"
docker build \
-t $IMAGE_WITH_TAG --file "$DOCKERFILE_PATH" \
--build-arg OIDC_CLIENT_SECRET=${{ secrets.OIDC_CLIENT_SECRET }} \
--build-arg DB_PWD=${{ secrets.DB_PWD }} \
--build-arg APP_ENV="dev" .
docker login TEST.com -u "$DEV_DEPLOY_USENAME" -p "$DEV_DEPLOY_PWD"
docker push $IMAGE_WITH_TAG
echo "IMAGE_VERSION=$IMAGE_VERSION" >> $GITHUB_ENV
echo "PUSHED_IMAGE=$IMAGE_WITH_TAG" >> $GITHUB_ENV
echo "develop branch"
echo "NAMESPACE=$PROJECT-dev" >> $GITHUB_ENV
env:
DEV_DEPLOY_USENAME: ${{ secrets.DEV_DEPLOY_USENAME }}
DEV_DEPLOY_PWD: ${{ secrets.DEV_DEPLOY_PWD }}
PUSHED_IMAGE: ${{ env.PUSHED_IMAGE }}
BRANCH: ${{ env.BRANCH }}
下面是我的 docker 文件。
FROM TEST.com/library/python:3.10-slim as builder
RUN pip install poetry==1.6.1
ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_VIRTUALENVS_CREATE=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache
WORKDIR /usr/src
ARG OIDC_CLIENT_SECRET
ARG DB_PWD
ARG APP_ENV
ENV OIDC_CLIENT_SECRET=$OIDC_CLIENT_SECRET
ENV DB_PWD=$DB_PWD
ENV APP_ENV=$APP_ENV
RUN echo $OIDC_CLIENT_SECRET
RUN echo $DB_PWD
RUN echo $APP_ENV
COPY pyproject.toml ./
RUN poetry install --without dev --no-root && rm -rf $POETRY_CACHE_DIR
# The runtime image, used to just run the code provided its virtual environment
FROM TEST.com/library/python:3.10-slim as runtime
ARG WORKER_COUNT=1
ENV WORKER_COUNT=${WORKER_COUNT}
RUN mkdir -p /usr/src
WORKDIR /usr/src
ENV VIRTUAL_ENV=/usr/src/.venv \
PATH="/usr/src:/usr/src/.venv/bin:$PATH"
ENV TZ=America/Chicago
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
COPY ./src/ /usr/src/
RUN ls -R
EXPOSE 5000
CMD uvicorn --workers $WORKER_COUNT --host 0.0.0.0 --port 5000 main:app
我的项目部署成功,但是 OIDC_CLIENT_SECRET 和 DB_PWD 的环境变量被设置为 None,我的日志语句证实了这一点。我仔细检查了整个文件中的名称、值,包括 github 中的秘密。我什至在我的 docker 中输出了 *** 的打印语句,我认为它仍然是正确的,它可能隐藏了该值。
我是否做错了什么,导致我的环境变量没有进入我的项目?
所以我终于明白了。问题出在 dockerfile 中。环境变量和参数需要在运行后声明,如下所示。
FROM TEST.com/library/python:3.10-slim as builder
RUN pip install poetry==1.6.1
ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_VIRTUALENVS_CREATE=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache
WORKDIR /usr/src
ARG OIDC_CLIENT_SECRET
ARG DB_PWD
ARG APP_ENV
ENV OIDC_CLIENT_SECRET=$OIDC_CLIENT_SECRET
ENV DB_PWD=$DB_PWD
ENV APP_ENV=$APP_ENV
RUN echo $OIDC_CLIENT_SECRET
RUN echo $DB_PWD
RUN echo $APP_ENV
COPY pyproject.toml ./
RUN poetry install --without dev --no-root && rm -rf $POETRY_CACHE_DIR
# The runtime image, used to just run the code provided its virtual environment
FROM TEST.com/library/python:3.10-slim as runtime
ARG WORKER_COUNT=1
ENV WORKER_COUNT=${WORKER_COUNT}
ARG OIDC_CLIENT_SECRET
ARG DB_PWD
ARG APP_ENV
ENV OIDC_CLIENT_SECRET=$OIDC_CLIENT_SECRET
ENV DB_PWD=$DB_PWD
ENV APP_ENV=$APP_ENV
RUN mkdir -p /usr/src
WORKDIR /usr/src
ENV VIRTUAL_ENV=/usr/src/.venv \
PATH="/usr/src:/usr/src/.venv/bin:$PATH"
ENV TZ=America/Chicago
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
COPY ./src/ /usr/src/
RUN ls -R
EXPOSE 5000
CMD uvicorn --workers $WORKER_COUNT --host 0.0.0.0 --port 5000 main:app
我将尽力解决 Docker 容器内的 Python 应用程序无法访问环境变量的问题。以下是综合回应,结合了专家反馈的见解并解决了潜在原因:
了解工作流程:
GitHub 操作:
构建参数 (
-build-arg
) 用于将机密传递给
Dockerfile.
Dockerfile:
ARG
指令接收构建参数。ENV
指令在构建上下文中设置环境变量。builder
、runtime
)来提高效率。潜在问题和解决方案:
ARG 和 ENV 的错误使用:
环境变量在阶段之间不持久:
虚拟环境隔离:
拼写错误和区分大小写:
解决方案:
FROM TEST.com/library/python:3.10-slim as builder
... (build instructions)
FROM TEST.com/library/python:3.10-slim as runtime
... (runtime setup)
COPY --from=builder /usr/src/.venv /usr/src/.venv
COPY --from=builder /usr/src/OIDC_CLIENT_SECRET /usr/src/OIDC_CLIENT_SECRET
COPY --from=builder /usr/src/DB_PWD /usr/src/DB_PWD
WORKDIR /usr/src
ENV OIDC_CLIENT_SECRET=$(cat /usr/src/OIDC_CLIENT_SECRET)
ENV DB_PWD=$(cat /usr/src/DB_PWD)
CMD uvicorn --workers $WORKER_COUNT --host 0.0.0.0 --port 5000 main:app
记住: