我有一个Jenkins管道,它在Docker容器中运行Sinatra应用程序的所有测试。
以下是Pipeline的样子:
#!/usr/bin/env groovy
pipeline {
agent {
docker {
image 'ruby'
args '-u root -v /root/.ssh:/root/.ssh'
}
}
stages {
stage('Build') {
steps {
echo 'Building...'
sh 'bundle install'
}
}
stage('Test') {
steps {
echo 'Testing...'
sh 'rspec spec'
}
}
stage('Deploy') {
steps {
echo 'Deploy...'
sh 'cap staging deploy'
}
}
}
}
Sinatra应用程序使用Redis,我似乎无法从包含运行测试的Docker中连接到Redis。 Redis数据库在可访问的其他远程服务器上运行。
我正在使用redis-rb
和以下代码访问Redis:
REDIS_DB = Redis.new(url: 'redis://:strongpassword123@server_ip:6379')
但是测试返回时出现以下错误:
Redis::CannotConnectError:
Error connecting to Redis on server_ip:6379 (Redis::TimeoutError)
# /usr/local/bundle/gems/redis-4.0.0/lib/redis/client.rb:344:in `rescue in establish_connection'
# /usr/local/bundle/gems/redis-4.0.0/lib/redis/client.rb:329:in `establish_connection'
# /usr/local/bundle/gems/redis-4.0.0/lib/redis/client.rb:99:in `block in connect'
# /usr/local/bundle/gems/redis-4.0.0/lib/redis/client.rb:291:in `with_reconnect'
# /usr/local/bundle/gems/redis-4.0.0/lib/redis/client.rb:98:in `connect'
# /usr/local/bundle/gems/redis-4.0.0/lib/redis/client.rb:363:in `ensure_connected'
# /usr/local/bundle/gems/redis-4.0.0/lib/redis/client.rb:219:in `block in process'
# /usr/local/bundle/gems/redis-4.0.0/lib/redis/client.rb:304:in `logging'
# /usr/local/bundle/gems/redis-4.0.0/lib/redis/client.rb:218:in `process'
# /usr/local/bundle/gems/redis-4.0.0/lib/redis/client.rb:118:in `call'
# /usr/local/bundle/gems/redis-4.0.0/lib/redis.rb:877:in `block in get'
# /usr/local/bundle/gems/redis-4.0.0/lib/redis.rb:45:in `block in synchronize'
# /usr/local/bundle/gems/redis-4.0.0/lib/redis.rb:45:in `synchronize'
# /usr/local/bundle/gems/redis-4.0.0/lib/redis.rb:876:in `get'
我尝试过使用本地Redis服务器,但这也无济于事。
我做错了什么?
答案 0 :(得分:1)
Docker容器具有IP 172.17.0.2
。由于我启用了UFW(防火墙),因此我必须添加一条允许Docker通过端口6379连接到Redis的规则。
这样做sudo ufw allow from 172.17.0.2 to any port 6379
解决了这个问题。