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

Time:11-12

I am creating a test stage in my GitLab ci in which I create a container for Postgres DB. I will need to inspect the container and use the IP address as database URL in my flask application:

Here is a pseudo-code of what I want to do:

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

How should I rewrite this part to make it work and set it to an env variable for me: db_host=docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' pg_test_container

I will need to use it to create the DATABASE_URL:

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

CodePudding user response:

If this is a shell script, you should be able to set a variable and use it:

- 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"

Other example: "How to set variable within 'script' section of gitlab-ci.yml file"

As the OP Amin Ba adds in the comments:

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"
  • Related