bambamboole's avatar

Gitlab CI weird redis host

Hi,

I use gitlab ci for testing a laravel 5.5 project which uses redis.

I have a REDIS_HOST with the value redis.

But in my phpunit pipeline I get the following error:

Predis\Connection\ConnectionException: Connection refused [tcp://redis:tcp://172.17.0.3:6379]

I don't know from where the IP comes and why it writes two times tcp:// .

Local tests works like a charm.

thanks in advance bambamboole

0 likes
7 replies
bambamboole's avatar

Nope, I wasn't not working on this project since I created the thread.

jorgenb's avatar

I got it to work by specifying:

variables:
  REDIS_PORT: 6379

in my gitlab-ci.yml file.

7 likes
ckluska's avatar

Got the same trouble today. You saved my day ! I checked officials pages about redis settings (gitlab and redis docker hub) and nobody talk about this variable ! It's crazy.

james14's avatar

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 your 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 pass variables in your Gitlab CI config file to override what Docker does, like this:

services:
    - redis:5-alpine
    - name: mongo
      alias: db
variables:
  REDIS_PORT: 6379
  DB_NAME: api-gateway
2 likes
arfn's avatar

@james14 makes sense, thank you for your explanation.

but there is still a question hanging in my head:

I thought when running the test command with --env=testing for example, the environment values from the file .env.testing should override the environment comes from the operating system, so even though REDIS_PORT is defined in the container automatically, the value within .env.testing should override it when running the command, isn't it?

Please or to participate in this conversation.