1

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'

Amin Ba
  • 1,603
  • 1
  • 13
  • 38
  • did you try using services for your postgres: https://docs.gitlab.com/ee/ci/services/ ? – Phillip -Zyan K Lee- Stockmann Nov 11 '21 at 23:45
  • 1
    I tried and I am still stuck because of the permission errors. I have opened a ticket for IT to give permissions to the user I am using because with `sudo -i` I have no problem. Thank you for your follow up – Amin Ba Nov 12 '21 at 11:26

1 Answers1

1

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"
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @AminBa The second line is just a debug echo, which prints the name of the variable and then its value. The '=' is not for assignment, only to separate name and value. – VonC Nov 11 '21 at 13:20
  • @AminBa That means the first line did not assign any value to the variable. Replace, for testing, the first line with `- db_host="test"`, just to confirm assignment is working. – VonC Nov 11 '21 at 13:22
  • @AminBa That is the question indeed: I would make a simpler `docker inspect` (without assigning it to a variable), just to see if it outputs anything. Once that simpler `docker inspect` works, I would add back all the template parameters. – VonC Nov 11 '21 at 13:27
  • 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 – Amin Ba Nov 11 '21 at 17:08
  • 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" – Amin Ba Nov 11 '21 at 17:12
  • @AminBa Is is related to https://forums.rockylinux.org/t/rootless-podman-issues/3366 and https://bugzilla.redhat.com/show_bug.cgi?id=1874621#c5 ? – VonC Nov 11 '21 at 17:17