[英]Github Actions to Connect Postgres service with custom container image
In my Django project, I have a CI workflow for running tests, which requires a Postgres service.在我的 Django 项目中,我有一个用于运行测试的 CI 工作流,它需要 Postgres 服务。 Recently a new app introduced heavier packages such as pandas, matplotlib, pytorch and so on and this increased the
run-tests
job time from 2 to 12 minutes which is absurd.最近一个新的应用程序引入了更重的包,如 pandas、matplotlib、pytorch 等,这将
run-tests
工作时间从 2 分钟增加到 12 分钟,这是荒谬的。 Also in my project, I have a base Docker image with Python and these packages that are heavier to speed up the build of the images.同样在我的项目中,我有一个带有 Python 的基础 Docker 映像,这些包更重以加快映像的构建。 So I was thinking to use this same image in the workflow when running the steps because the packages would be loaded already.
所以我想在运行这些步骤时在工作流中使用相同的图像,因为包已经被加载了。
Unfortunately, all goes well until it reaches the step to actually run the tests because it seems that the postgres service is not connected with the container and I get the following error:不幸的是,一切顺利,直到到达实际运行测试的步骤,因为似乎 postgres 服务未与容器连接,我收到以下错误:
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
This is my workflow right now.这是我现在的工作流程。 Any ideas on what I am doing wrong?
关于我做错了什么的任何想法?
name: server-ci
on:
pull_request:
types: [opened]
env:
DJANGO_SETTINGS_MODULE: settings_test
jobs:
run-tests:
name: Run tests
runs-on: ubuntu-latest
container:
image: myimage/django-server:base
credentials:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
ports:
- 8000:8000
services:
postgres:
image: postgres
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: admin
POSTGRES_DB: mydb
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
env:
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
POSTGRES_PASSWORD: admin
POSTGRES_USER: postgres
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Cache dependencies
uses: actions/cache@v2
with:
path: /opt/venv
key: /opt/venv-${{ hashFiles('**/requirements.txt') }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
if: steps.cache.outputs.cache-hit != 'true'
- name: Run tests
run: |
./manage.py test --parallel --verbosity=2
It turns out that the workflow is now running in a container of its own, next to the postgres container.事实证明,工作流现在在自己的容器中运行,紧挨着 postgres 容器。 So the port mapping to the runner VM doesn't do anything any more (because it affects the host, not Docker containers on it).
因此,映射到运行器 VM 的端口不再执行任何操作(因为它会影响主机,而不是其上的 Docker 容器)。
The job and service containers get attached to the same Docker network, so all I need to do is change POSTGRES_HOST
to postgres
(the name of the service container) and Docker's DNS should do the rest.作业和服务容器连接到同一个 Docker 网络,所以我需要做的就是将
POSTGRES_HOST
更改为postgres
(服务容器的名称),剩下的应该由 Docker 的 DNS 完成。
Credits: https://github.community/t/connect-postgres-service-with-custom-container-image/189994/2?u=everspader学分: https : //github.community/t/connect-postgres-service-with-custom-container-image/189994/2? u =everspader
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.