Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose - Redis at 0.0.0.0 instead of 127.0.0.1

I havs migrated my Rails app (local dev machine) to Docker-Compose. All is working except the Worker Rails instance (batch) cannot connect to Redis.

Completed 500 Internal Server Error in 40ms (ActiveRecord: 2.3ms)

Redis::CannotConnectError (Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)):

In my docker-compose.yml

redis:
  image: redis
  ports:
    - "6379:6379"

batch:
  build: .
  command: bundle exec rake environment resque:work QUEUE=*
  volumes:
    - .:/app
  links:
    - db
    - redis
  environment:
    - REDIS_URL=redis://redis:6379

I think the Redis instance is available via the IP of the Docker host.

$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                       SWARM   DOCKER    ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v1.10.0

Accessing via 0.0.0.0 doesn't work

$ curl 0.0.0.0:6379 
curl: (7) Failed to connect to 0.0.0.0 port 6379: Connection refused

Accessing via the docker-machine IP I think works:

$ curl http://192.168.99.100:6379
-ERR wrong number of arguments for 'get' command
-ERR unknown command 'Host:'

EDIT

After installing redis-cli in the batch instance, I was able to hit the redis server using the 'redis' hostname. I think the problem is possibly in the Rails configuration itself.

like image 636
port5432 Avatar asked Feb 11 '16 21:02

port5432


People also ask

How to run Redis in Docker Compose?

Run Redis with Compose. 1 Create a config file for Redis. To inject a custom configuration file for Redis when building the Docker container, create the redis.config file ... 2 Create a Docker-Compose file for Redis. 3 Build the Redis with Docker Compose. 4 Run commands in the Redis container.

How do I use an empty password in Docker for Redis?

The default command from the Docker hub profile for Bitnami Redis allows the use of an empty password, as shown in the following example: NOTE: The ALLOW_EMPTY_PASSWORD setting should only be used for development purposes. As shown in the following representation, Docker will download the image and run the container in the foreground:

Why can't Docker connect to Redis-server?

You may need to daemonize the redis-server so that docker can connect to it: Then try again the command you performed, that gives you error. [Looking for a solution to another query?

How do I Fix an invalid reference format error when running Docker?

If the previous docker run command returns an invalid reference format error, try removing the -name tag or use the redis:latest command to pull from the latest image, as shown here: To stop the container, execute the docker stop command followed by the container’s ID, as shown here:


2 Answers

Facepalm!!!

The docker containers were communicating just fine, the problem was I hadn't told Resque (the app using Redis) where to find it. Thank you to "The Real Bill" for pointing out I should be using docker-cli.

For anyone else using Docker and Resque, you need this in your config/initializers/resque.rb file:

Resque.redis = Redis.new(host: 'redis', port: 6379)
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }
like image 108
port5432 Avatar answered Oct 09 '22 07:10

port5432


If you run

docker-compose run --rm batch env | grep REDIS

you will get the env variables that your container has (the link line in the compose will auto-generate some). Then all you need to do is look for one along the lines of _REDIS_1_PORT... and use the correct one. I have never had luck connecting my rails to another service in any other way. But luckily these env variables are always generated on start so they will be up to date even if the container IP happens to change between startups.

like image 20
jacob.mccrumb Avatar answered Oct 09 '22 05:10

jacob.mccrumb