简体   繁体   中英

In gitlab ci pipeline, how can I connect to redis service using python?

My gitlab-ci.yml looks like this:

stage: functional_test
  only:
    - develop
    - master
    - merge_requests
  services:
    - redis:latest
    - docker:18.06.2-dind
  variables:
    PHOTON_ENV: development
    DOCKER_HOST: tcp://localhost:2375
    REDIS_HOST: redis
    REDIS_URL: redis://redis:6379/0

And my python code to connect redis looks like this:

self._redis = redis.Redis(host=_host,
                          port=_port,
                          db=_db,
                          decode_responses=True)

_host = 'redis'
_port = 6379
_db = 0

However, every time I try this code, this error will show up:

      File "/usr/local/lib/python3.6/site-packages/redis/client.py", line 772, in execute_command
403     connection = pool.get_connection(command_name, **options)
404   File "/usr/local/lib/python3.6/site-packages/redis/connection.py", line 994, in get_connection
405     connection.connect()
406   File "/usr/local/lib/python3.6/site-packages/redis/connection.py", line 497, in connect
407     raise ConnectionError(self._error_message(e))
408 redis.exceptions.ConnectionError: Error -2 connecting to redis:6379. Name or service not known.

Going from comments and according to documentation you have to manually install redis on your gitlab runner server if you are using a shell executor.

I am putting here a solution if anyone else has the same problem.

The redis service in gitlab-ci.yml works as follows:

  1. Gitlab runner starts the redis-server in the user image.
  2. The redis server can only be connected by the ip-address of your container(the first one, not docker assigned).
  3. Pass the ip-address mentioned above to python code(replace "redis" with "$ip")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM