如何将 docker 检查的结果设置为 gitlab-ci 脚本中的环境变量

How to set the result of docker inspect to an env variable in gitlab-ci script

我正在我的 GitLab ci 中创建一个测试阶段,我在其中为 Postgres DB 创建了一个容器。我将需要检查容器并在我的 Flask 应用程序中使用 IP 地址作为数据库 URL:

这是我想做的伪代码:

test:

  stage: test
  script:
    - echo "testing"
    - docker run -d -p 5434:5432 --rm --name pg_test_container --env-file test_database.env postgres:latest
    - db_host=docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' pg_test_container 
    - echo db_host
    - ...
    - ...
    - ...
    - ...
    - docker stop pg_test_container 
  tags:
    - test

我应该如何重写这部分以使其工作并将其设置为我的环境变量: db_host=docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' pg_test_container

我需要用它来创建 DATABASE_URL:

DATABASE_URL='postgresql://postgres:1234@db_host:5434/pg_db'

如果这是一个 shell 脚本,您应该能够设置一个变量并使用它:

- db_host=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' pg_test_container)
- echo "db_host='${db_host}'"
- DATABASE_URL="postgresql://postgres:1234@${db_host}:5434/pg_db"

其他示例:“

作为 OP Amin Ba adds in :

The problem is with docker inspect.
Actually, the problem is with the permissions of the user trying to run 'docker run' command.

failed to read subscriptions from "/usr/share/rhel/secrets": 
open /usr/share/rhel/secrets/rhsm/rhsm.conf: permission denied
skipping entry in /usr/share/containers/mounts.conf: 
getting host subscription data: failed to read subscriptions from \"/usr/share/rhel/secrets\": 
open /usr/share/rhel/secrets/rhsm/rhsm.conf: permission denied"