[英]How to expose a Github Docker container action's ports to be accessible to the rest of the workflow
I am trying to create a GitHub Action that initializes the ChromaDB server which serves in port 8000
and exposes that to the rest of the workflow.我正在尝试创建一个 GitHub 操作来初始化在端口
8000
中服务的ChromaDB服务器,并将其公开给工作流的 rest。
Currently to initialize the server you simply run docker-compose up -d
and that starts up the server.目前要初始化服务器,您只需运行
docker-compose up -d
即可启动服务器。 That works both locally and in a github action workflow.这在本地和 github 操作工作流中都有效。
name: Docker Compose Workflow
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Build and run Docker Compose
run: |
docker-compose up -d
- name: Wait for server to be ready
run: |
while ! curl -s http://localhost:8000 > /dev/null; do
echo "Waiting for server..."
sleep 5
done
- name: Verify server is running
run: |
curl http://localhost:8000
The issue lies when I refactor it, so that running the server can be its own GitHub Action.问题出在我重构它的时候,让运行服务器可以是它自己的 GitHub Action。 The refactor involves creating the
action.yml
file like so:重构涉及创建
action.yml
文件,如下所示:
name: 'ChromaDB'
description: 'Instantiates a chroma database server'
runs:
using: 'docker'
image: 'docker/compose:1.29.2'
entrypoint: docker-compose
args:
- up
- -d
and then replace the "Build and run Docker Compose"
with running the local action instead.然后将
"Build and run Docker Compose"
替换为运行本地操作。 Transforming the workflow into this:将工作流程转换为:
name: Docker Compose Workflow
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Build and run Docker Compose
uses: ./
- name: Wait for server to be ready
run: |
while ! curl -s http://localhost:8000 > /dev/null; do
echo "Waiting for server..."
sleep 5
done
- name: Verify server is running
run: |
curl http://localhost:8000
This does not work, the wait for server to be ready runs endlessly implying that server at port 8000
is never started.这是行不通的,等待服务器准备就绪的等待无休止地运行,这意味着端口
8000
上的服务器永远不会启动。
How could I create the action that runs the server and exposes the server at port 8000
for the rest of the workflow?我如何创建运行服务器并在端口
8000
为工作流的 rest 公开服务器的操作?
Note: GPT-4 suggested that the Docker container action runs as a self hosted environment.注意:GPT-4 建议 Docker 容器操作作为自托管环境运行。 chat
聊天
The issue arises because the localhost in your Docker container (running the Docker Compose) is not the same as the localhost of the runner environment (which is the host of your Docker container).
出现此问题是因为您的 Docker 容器(运行 Docker Compose)中的本地主机与运行器环境的本地主机(这是您的 Docker 容器的主机)不同。 Thus, when your workflow tries to access localhost:8000 after your Docker container has started the services, it's trying to access localhost:8000 of the runner environment and not of the Docker container, where your services are actually running.
因此,当您的工作流在 Docker 容器启动服务后尝试访问 localhost:8000 时,它会尝试访问运行器环境的 localhost:8000,而不是您的服务实际运行的 Docker 容器。
Thanks in advance!提前致谢!
Logs for "Build and run docker compose" step (highlights): “Build and run docker compose”步骤的日志(重点):
/usr/bin/docker run --name dockercompose1292_63ab6b --label ed866e --workdir /github/workspace --rm -e "HOME" -e "GITHUB_JOB" -e "GITHUB_REF" -e "GITHUB_SHA" -e "GITHUB_REPOSITORY" -e "GITHUB_REPOSITORY_OWNER" -e "GITHUB_REPOSITORY_OWNER_ID" -e "GITHUB_RUN_ID" -e "GITHUB_RUN_NUMBER" -e "GITHUB_RETENTION_DAYS" -e "GITHUB_RUN_ATTEMPT" -e "GITHUB_REPOSITORY_ID" -e "GITHUB_ACTOR_ID" -e "GITHUB_ACTOR" -e "GITHUB_TRIGGERING_ACTOR" -e "GITHUB_WORKFLOW" -e "GITHUB_HEAD_REF" -e "GITHUB_BASE_REF" -e "GITHUB_EVENT_NAME" -e "GITHUB_SERVER_URL" -e "GITHUB_API_URL" -e "GITHUB_GRAPHQL_URL" -e "GITHUB_REF_NAME" -e "GITHUB_REF_PROTECTED" -e "GITHUB_REF_TYPE" -e "GITHUB_WORKFLOW_REF" -e "GITHUB_WORKFLOW_SHA" -e "GITHUB_WORKSPACE" -e "GITHUB_ACTION" -e "GITHUB_EVENT_PATH" -e "GITHUB_ACTION_REPOSITORY" -e "GITHUB_ACTION_REF" -e "GITHUB_PATH" -e "GITHUB_ENV" -e "GITHUB_STEP_SUMMARY" -e "GITHUB_STATE" -e "GITHUB_OUTPUT" -e "RUNNER_OS" -e "RUNNER_ARCH" -e "RUNNER_NAME" -e "RUNNER_TOOL_CACHE" -e "RUNNER_TEMP" -e "RUNNER_WORKSPACE" -e "ACTIONS_RUNTIME_URL" -e "ACTIONS_RUNTIME_TOKEN" -e "ACTIONS_CACHE_URL" -e GITHUB_ACTIONS=true -e CI=true --entrypoint "docker-compose" -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "/home/runner/work/chroma/chroma":"/github/workspace" docker/compose:1.29.2 "up" "-d" Unable to find image 'docker/compose:1.29.2' locally 1.29.2: Pulling from docker/compose... Get:92 http://deb.debian.org/debian bullseye/main amd64 libldap-common all 2.4.57+dfsg-3+deb11u1 [95.8 kB] Get:93 http://deb.debian.org/debian bullseye/main amd64 libsasl2-modules amd64 2.1.27+dfsg-2.1+deb11u1 [104 kB] Get:94 http://deb.debian.org/debian bullseye/main amd64 manpages-dev all 5.10-1 [2309 kB] debconf: delaying package configuration, since apt-utils is not installed Fetched 86.7 MB in 1s (113 MB/s) Selecting previously unselected package perl-modules-5.32.
/usr/bin/docker run --name dockercompose1292_63ab6b --label ed866e --workdir /github/workspace --rm -e "HOME" -e "GITHUB_JOB" -e "GITHUB_REF" -e "GITHUB_SHA" -e "GITHUB_REPOSITORY" -e "GITHUB_REPOSITORY_OWNER" -e "GITHUB_REPOSITORY_OWNER_ID" -e "GITHUB_RUN_ID" -e "GITHUB_RUN_NUMBER" -e "GITHUB_RETENTION_DAYS" -e "GITHUB_RUN_ATTEMPT" -e "GITHUB_REPOSITORY_ID" -e "GITHUB_ACTOR_ID" -e " GITHUB_ACTOR" -e "GITHUB_TRIGGERING_ACTOR" -e "GITHUB_WORKFLOW" -e "GITHUB_HEAD_REF" -e "GITHUB_BASE_REF" -e "GITHUB_EVENT_NAME" -e "GITHUB_SERVER_URL" -e "GITHUB_API_URL" -e "GITHUB_GRAPHQL_URL" -e "GITHUB_REF_NAME" -e "GITHUB_REF_PROTECTED" -e "GI THUB_REF_TYPE" -e "GITHUB_WORKFLOW_REF" -e "GITHUB_WORKFLOW_SHA" -e "GITHUB_WORKSPACE" -e "GITHUB_ACTION" -e "GITHUB_EVENT_PATH" -e "GITHUB_ACTION_REPOSITORY" -e "GITHUB_ACTION_REF" -e "GITHUB_PATH" -e "GITHUB_ENV" -e "GITHUB_STEP_S总结” -e "GITHUB_STATE" -e "GITHUB_OUTPUT" -e "RUNNER_OS" -e "RUNNER_ARCH" -e "RUNNER_NAME" -e "RUNNER_TOOL_CACHE" -e "RUNNER_TEMP" -e "RUNNER_WORKSPACE" -e "ACTIONS_RUNTIME_URL" -e "ACTIONS_RUNTIME_TOKEN" -e "ACTIONS_CACHE_URL" -e GITHUB_ACTIONS=true -e CI=true --entrypoint "docker-compose" -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/ home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/ _runner_file_commands":"/github/file_commands" -v "/home/runner/work/chroma/chroma":"/github/workspace" docker/compose:1.29.2 "up" "-d" 无法找到图像 'docker /compose:1.29.2' locally 1.29.2: 从 docker/compose 拉取... Get:92 http://deb.debian.org/debian bullseye/main amd64 libldap-common all 2.4.57+dfsg-3+ deb11u1 [95.8 kB] 获取:93 http://deb.debian.org/debian bullseye/main amd64 libsasl2-modules amd64 2.1.27+dfsg-2.1+deb11u1 [104 kB] 获取:94 8825412274 4288://deb.debian .org/debian bullseye/main amd64 manpages-dev all 5.10-1 [2309 kB] debconf:延迟 package 配置,因为未安装 apt-utils 在 1 秒内获取 86.7 MB(113 MB/s) 选择以前未选择的 package perl-modules -5.32。 ... Successfully installed anyio-3.7.0 backoff-2.2.1 certifi-2023.5.7 charset-normalizer-2.1.1 click-8.1.3 clickhouse-connect-0.5.7 coloredlogs-15.0.1 duckdb-0.7.1 exceptiongroup-1.1.1 fastapi-0.85.1 flatbuffers-23.5.26 h11-0.14.0 hnswlib-0.7.0 httptools-0.5.0 humanfriendly-10.0 idna-3.4 lz4-4.3.2 monotonic-1.6 mpmath-1.3.0 numpy-1.21.6 onnxruntime-1.14.1 overrides-7.3.1 packaging-23.1 pandas-1.3.5 posthog-2.4.0 protobuf-4.23.2 pulsar-client-3.1.0 pydantic-1.9.0 pypika-0.48.9 python-dateutil-2.8.2 python-dotenv-1.0.0 pytz-2023.3 pyyaml-6.0 requests-2.28.1 six-1.16.0 sniffio-1.3.0 starlette-0.20.4 sympy-1.12 tokenizers-0.13.2 tqdm-4.65.0 typing_extensions-4.5.0 urllib3-1.26.16 uvicorn-0.18.3 uvloop-0.17.0 watchfiles-0.19.0 websockets-11.0.3 zstandard-0.21.0 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager.
...成功安装anyio-3.7.0 backoff-2.2.1 certifi-2023.5.7 charset-normalizer-2.1.1 click-8.1.3 clickhouse-connect-0.5.7 coloredlogs-15.0.1 duckdb-0.7.1 exceptiongroup -1.1.1 fastapi-0.85.1 flatbuffers-23.5.26 h11-0.14.0 hnswlib-0.7.0 httptools-0.5.0 humanfriendly-10.0 idna-3.4 lz4-4.3.2 monotonic-1.6 mpmath-1.3.0 numpy- 1.21.6 onnxruntime-1.14.1 overrides-7.3.1 packaging-23.1 pandas-1.3.5 posthog-2.4.0 protobuf-4.23.2 pulsar-client-3.1.0 pydantic-1.9.0 pypika-0.48.9 python- dateutil-2.8.2 python-dotenv-1.0.0 pytz-2023.3 pyyaml-6.0 requests-2.28.1 six-1.16.0 sniffio-1.3.0 starlette-0.20.4 sympy-1.12 tokenizers-0.13.2 tqdm-4.65。 0 typing_extensions-4.5.0 urllib3-1.26.16 uvicorn-0.18.3 uvloop-0.17.0 watchfiles-0.19.0 websockets-11.0.3 zstandard-0.21.0 警告:以“root”用户身份运行 pip 可能导致损坏权限和与系统 package 经理的冲突行为。 It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
建议改用虚拟环境: https://pip.pypa.io/warnings/venv
Notice: A new release of pip is available: 23.0.1 -> 23.1.2 Notice: To update, run: pip install --upgrade pip Removing intermediate container ca89af45e5ee ---> 97537465f160 Step 7/14: FROM python:3.10-slim-bullseye as final ---> 06b56278a68d Step 8/14: RUN mkdir /chroma... Image for service server was built because it did not already exist.
注意:pip 的新版本可用:23.0.1 -> 23.1.2 注意:要更新,请运行:pip install --upgrade pip 删除中间容器 ca89af45e5ee ---> 97537465f160 步骤 7/14:从 88 276540434488:3.10- slim-bullseye as final ---> 06b56278a68d Step 8/14: RUN mkdir /chroma... 构建服务服务器的图像,因为它尚不存在。 To rebuild this image you must use
docker-compose build
ordocker-compose up --build
.要重建此映像,您必须使用
docker-compose build
或docker-compose up --build
。 Creating workspace_clickhouse_1... Creating workspace_clickhouse_1... done Creating workspace_server_1... Creating worksp ace_server_1... done创建 workspace_clickhouse_1... 创建 workspace_clickhouse_1... 完成 创建 workspace_server_1... 创建 workspace_server_1... 完成
Then on the "Wait for server" step然后在“等待服务器”步骤
Run while: curl -s http://localhost;8000 > /dev/null.
运行时:curl -s http://localhost;8000 > /dev/null。 do Waiting for server... Waiting for server... Waiting for server... Waiting for server... Waiting for server... Waiting for server... ...
do 等待服务器... 等待服务器... 等待服务器... 等待服务器... 等待服务器... 等待服务器... ...
Endlessly.不休。
Using a composite action did the trick.使用复合动作就可以了。 So refactoring the
action.yml
into the following:因此将
action.yml
重构为以下内容:
name: 'ChromaDB'
description: 'Instantiates a chroma database server.'
runs:
using: 'composite'
steps:
- name: Checkout current repository
uses: actions/checkout@v2
- name: Start server by running docker-compose
run: docker-compose up -d
shell: bash
- name: Wait for server to be ready
run: |
while ! curl -s http://localhost:8000 > /dev/null; do
echo "Waiting for server..."
sleep 5
done
shell: bash
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.