skip to Main Content

I’m working on a Gitlab CI configuration in the .gitlab-ci.yml file for a Laravel project which uses redis as Cache Driver. Within this configuration file I use an image which has all the linux packages, php extension which are used on our production environment. This includes the redis extension installed by pecl so phpredis can be used within Laravel.

After spending a fair amount of time into learning the process of continuous integration and the configuration of the gitlab ci file I encountered the following error on deploy of the image:


In PhpRedisConnector.php line 126:

   Redis::connect() expects parameter 2 to be int, string given  

parameter should be only the port as an int but somehow turned into tcp://111.111.11.11:6379

As documented https://docs.gitlab.com/ee/ci/services/redis.html the host in your .env should be redis so I had. Dumping the config during the deploy resulted in port being some tcp://111.111.11.11:6379 connection string and host being empty. There is no way this is changed within our application or any extension we use.

I use the https://hub.docker.com/_/redis/ as a service in the .gitlab-ci.yml.

...
services:
  - name: redis:4
...

2

Answers


  1. Chosen as BEST ANSWER

    Solution found here: https://laracasts.com/discuss/channels/testing/gitlab-ci-weird-redis-host

    This variable isn't documented anywhere but somehow it solves the problem. Add the following to your .gitlab-ci.yml file:

    variables:
      REDIS_PORT: 6379
    

    If anyone finds documentation about this variable and maybe more options, let me know!


  2. This is expected behaviour from Docker.

    In Gitlab CI, all services you define will be linked with your container which is running your job using Docker linking system.

    As describe here, when we link containers, Docker automatically creates ENVs in format:

    • <_alias>_NAME
    • <_alias>_PORT
    • and some other ENVS

    And those ENVs from Docker will override yours.

    alias here will be the hostname of your container you defined in services. For example you define these services in your .gitlab-ci.yml:

    services:
     - redis:5-alpine
     - name: mongo
       alias: db
    

    So Docker will create the following ENVs respectively:

    • REDIS_PORT="some thing looks like: tcp://172.17.0.3:6379"
    • REDIS_NAME="looks like: /runner-72989761-project-19846207-concurrent-0-62507216079cf651-build-3/redis"
    • DB_PORT=same like redis
    • DB_NAME=same like redis

    And note that, as described here, by default the following ENVs are also auto created based on service image’s name:

    • MONGO_PORT=…
    • MONGO_NAME=…

    So in your code if you have any variable that has same name with the ones Docker creates, you may need to change your variable’s name otherwise it’ll be override by Docker. Or you can use the solution from @MmynameStackflow above, pass variables in your Gitlab CI config file to override what Docker does.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search