简体   繁体   English

Docker-compose - Redis为0.0.0.0而不是127.0.0.1

[英]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. 我已经将我的Rails应用程序(本地开发机器)迁移到Docker-Compose。 All is working except the Worker Rails instance (batch) cannot connect to Redis. 一切正常,除了Worker Rails实例(批处理)无法连接到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 在我的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. 我认为Redis实例可以通过Docker主机的IP获得。

$ 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 通过0.0.0.0访问不起作用

$ 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: 通过docker-machine IP访问我觉得有效:

$ 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. 在批处理实例中安装redis-cli之后,我能够使用'redis'主机名命中redis服务器。 I think the problem is possibly in the Rails configuration itself. 我认为问题可能出在Rails配置本身。

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). 您将获得容器所具有的env变量(compose中的链接行将自动生成一些)。 Then all you need to do is look for one along the lines of _REDIS_1_PORT... and use the correct one. 然后你需要做的就是找一条_REDIS_1_PORT ......并使用正确的一行。 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. 但幸运的是,这些env变量总是在启动时生成,因此即使容器IP恰好在启动之间发生变化,它们也是最新的。

您应该使用主机名redis连接到该服务,尽管您可能需要等待redis启动。

Facepalm!!! 捂脸!

The docker containers were communicating just fine, the problem was I hadn't told Resque (the app using Redis) where to find it. docker容器正在通信很好,问题是我没有告诉Resque(使用Redis的应用程序)在哪里找到它。 Thank you to "The Real Bill" for pointing out I should be using docker-cli. 感谢“The Real Bill”指出我应该使用docker-cli。

For anyone else using Docker and Resque, you need this in your config/initializers/resque.rb file: 对于使用Docker和Resque的其他人,您需要在config / initializers / resque.rb文件中使用它:

Resque.redis = Redis.new(host: 'redis', port: 6379)
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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