Docker容器在本地运行,但在云运行时失败以服务DBT文档

发布于 2025-01-24 11:31:50 字数 3277 浏览 3 评论 0原文

这里的想法很简单 - DBT提供了一种生成静态文件并使用命令 dbt docs生成 dbt文档服务的方法,我想以每个人的方式共享在我的组织中,可以看到它们(绕过现在的安全问题)。对于这项任务,我认为云运行将是理想的解决方案,因为我已经有了dockerfile和bash scrips,可以从事一些背景工作(每x小时每次克隆git repo等)。在本地运行此容器正常工作。但是将此图像部署在云运行中并没有成功 - 它在最后一步(即 dbt docs Server -port 8080)带有默认错误消息云运行错误:用户 - 提供的容器无法启动和侦听端口= 8080环境变量所定义的端口。此修订版的日志可能包含更多信息。在未打印之前,日志中没有其他信息。

DockerFile:

FROM --platform=$build_for python:3.9.9-slim-bullseye 
WORKDIR /usr/src/dbtdocs
RUN apt-get update && apt-get install -y --no-install-recommends git apt-transport-https ca-certificates gnupg curl cron \
    && apt-get clean
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install tzdata
RUN echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key --keyring /usr/share/keyrings/cloud.google.gpg  add - && apt-get update -y && apt-get install google-cloud-sdk -y
RUN python -m pip install --upgrade pip setuptools wheel --no-cache-dir
RUN pip install dbt-bigquery
RUN ln -s /usr/local/bin/dbt /usr/bin/
RUN rm -rf /var/lib/apt/lists/*
COPY ./api-entrypoint.sh /usr/src/dbtdocs/
COPY ./cron_dbt_docs.sh /usr/src/dbtdocs/
COPY ./cron_script.sh /usr/src/dbtdocs/
ENV PORT=8080
RUN chmod 755 api-entrypoint.sh
RUN chmod 755 cron_dbt_docs.sh
RUN chmod 755 cron_script.sh
ENTRYPOINT ["/bin/bash", "-c", "/usr/src/dbtdocs/api-entrypoint.sh" ] ```

创建云运行服务时, API-Entrypoint.SH

#!/bin/bash

#set -e
#catch() {
#    echo 'catching!'
#    if [ "$1" != "0" ]; then
#    echo "Error $1 occurred on $2"
#    fi
#}
#trap 'catch $? $LINENO' EXIT
exec 2>&1
echo 'Starting DBT Workload'
echo 'Checking dependencies'

dbt --version
git --version

mkdir -p /data/dbt/ && cd /data/dbt/
echo 'Cloning dbt Repo'
git clone ${GITLINK} /data/dbt/

echo 'Working on dbt directory'
export DBT_PROFILES_DIR=/data/dbt/profile/

echo "Authentificate at GCP"
echo "Decrypting and saving sa.json file"
mkdir -p /usr/src/secret/
echo "${SA_SECRET}" | base64 --decode > /usr/src/secret/sa.json
gcloud auth activate-service-account ${SA_EMAIL} --key-file /usr/src/secret/sa.json
echo 'The Project set'
if test "${PROJECT_ID}"; then
    gcloud config set project ${PROJECT_ID}
    gcloud config set disable_prompts true
else
    echo "Project Name not in environment variables ${PROJECT_ID}"
fi
echo 'Use Google Cloud Secret Manager Secret'
if test "${PROFILE_SECRET_NAME}"; then
    #mkdir -p /root/.dbt/
    mkdir -p /root/secret/
    gcloud secrets versions access latest --secret="${PROFILE_SECRET_NAME}" > /root/secret/creds.json
    export GOOGLE_APPLICATION_CREDENTIALS=/root/secret/creds.json
else
    echo 'No Secret Name described - GCP Secret Manager'
fi

echo 'Apply cron Scheduler'
sh -c "/usr/src/dbtdocs/cron_script.sh install"
/etc/init.d/cron restart
touch /data/dbt_docs_job.log
sh -c "/usr/src/dbtdocs/cron_dbt_docs.sh"
touch /data/cron_up.log
tail -f /data/dbt_docs_job.log &
tail -f /data/cron_up.log &
dbt docs serve --port 8080

容器端口设置为8080,因此我认为这不是问题。 有人实际上遇到了使用云运行的类似问题吗?

Idea here is simple - dbt provides a way to generate static files and serve them by using commands dbt docs generate and dbt docs serve and I want to share in a way that everyone in my organization can see them (bypassing security concerns as of now). For this task I thought Cloud Run would be ideal solution as I already have Dockerfile and bash scrips which do some background work (cron job to clone git repo every x hours, etc.). Running this container locally works fine. But deploying this image in Cloud Run wasn't successful - it fails on the last step (which is dbt docs server --port 8080) with default error message Cloud Run error: The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information. No additional information in logs before that wasn't printed.

Dockerfile:

FROM --platform=$build_for python:3.9.9-slim-bullseye 
WORKDIR /usr/src/dbtdocs
RUN apt-get update && apt-get install -y --no-install-recommends git apt-transport-https ca-certificates gnupg curl cron \
    && apt-get clean
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install tzdata
RUN echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key --keyring /usr/share/keyrings/cloud.google.gpg  add - && apt-get update -y && apt-get install google-cloud-sdk -y
RUN python -m pip install --upgrade pip setuptools wheel --no-cache-dir
RUN pip install dbt-bigquery
RUN ln -s /usr/local/bin/dbt /usr/bin/
RUN rm -rf /var/lib/apt/lists/*
COPY ./api-entrypoint.sh /usr/src/dbtdocs/
COPY ./cron_dbt_docs.sh /usr/src/dbtdocs/
COPY ./cron_script.sh /usr/src/dbtdocs/
ENV PORT=8080
RUN chmod 755 api-entrypoint.sh
RUN chmod 755 cron_dbt_docs.sh
RUN chmod 755 cron_script.sh
ENTRYPOINT ["/bin/bash", "-c", "/usr/src/dbtdocs/api-entrypoint.sh" ] ```

api-entrypoint.sh

#!/bin/bash

#set -e
#catch() {
#    echo 'catching!'
#    if [ "$1" != "0" ]; then
#    echo "Error $1 occurred on $2"
#    fi
#}
#trap 'catch $? $LINENO' EXIT
exec 2>&1
echo 'Starting DBT Workload'
echo 'Checking dependencies'

dbt --version
git --version

mkdir -p /data/dbt/ && cd /data/dbt/
echo 'Cloning dbt Repo'
git clone ${GITLINK} /data/dbt/

echo 'Working on dbt directory'
export DBT_PROFILES_DIR=/data/dbt/profile/

echo "Authentificate at GCP"
echo "Decrypting and saving sa.json file"
mkdir -p /usr/src/secret/
echo "${SA_SECRET}" | base64 --decode > /usr/src/secret/sa.json
gcloud auth activate-service-account ${SA_EMAIL} --key-file /usr/src/secret/sa.json
echo 'The Project set'
if test "${PROJECT_ID}"; then
    gcloud config set project ${PROJECT_ID}
    gcloud config set disable_prompts true
else
    echo "Project Name not in environment variables ${PROJECT_ID}"
fi
echo 'Use Google Cloud Secret Manager Secret'
if test "${PROFILE_SECRET_NAME}"; then
    #mkdir -p /root/.dbt/
    mkdir -p /root/secret/
    gcloud secrets versions access latest --secret="${PROFILE_SECRET_NAME}" > /root/secret/creds.json
    export GOOGLE_APPLICATION_CREDENTIALS=/root/secret/creds.json
else
    echo 'No Secret Name described - GCP Secret Manager'
fi

echo 'Apply cron Scheduler'
sh -c "/usr/src/dbtdocs/cron_script.sh install"
/etc/init.d/cron restart
touch /data/dbt_docs_job.log
sh -c "/usr/src/dbtdocs/cron_dbt_docs.sh"
touch /data/cron_up.log
tail -f /data/dbt_docs_job.log &
tail -f /data/cron_up.log &
dbt docs serve --port 8080

Container port is set to 8080 when creating Cloud Run service, so I don't think it's a problem here.
Have someone actually encountered similar problems using Cloud Run?

Logs in Cloud Logging

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉薄对峙 2025-01-31 11:31:50

您的容器是不是在端口8080上侦听/响应,并在服务器过程开始侦听之前已终止。

查看日志中的最后一行。以前的行是构建目录

您的容器需要花费太长时间才能启动。容器应在10秒内开始,因为云运行只会在10秒内保持待处理的请求。

我在日志中看到的所有工作都应在部署容器之前执行,并且在容器启动过程中

解决方案是重新设计如何构建和部署此容器,以便在容器启动后立即开始对请求响应。

Your container is not listening/responding on port 8080 and has been terminated before the server process starts listening.

Review the last line in the logs. The previous line is building catalog.

Your container is taking too long to startup. Containers should start within 10 seconds because Cloud Run will only keep pending requests for 10 seconds.

All of the work I see in the logs should be performed before the container is deployed and not during container start.

The solution is to redesign how you are building and deploying this container so that the application begins responding to requests as soon as the container starts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文