[英]Docker: Docker-compose ruby + postgres - PG::ConnectionBadcould not connect to server
[英]docker-compose: Not able to connect to memcached container
我正在學習碼頭工人。 我想在應用程序中添加緩存功能,並因此使用memcached。 以下是我的docker-compose.yml文件
version: "3"
services:
app:
build: .
volumes:
- .:/project
command: 'rails s -b 0.0.0.0 -p 3000'
container_name: 'test_rails'
ports:
- 3000:3000
depends_on:
- database
links:
- memcached
database:
image: postgres:latest
volumes:
- ./data:/var/lib/postgresql/data
environment:
POSTGRES_USER: docker-user
POSTGRES_PASSWORD: docker-password
POSTGRES_DB: docker-db
memcached:
build:
context: .
dockerfile: Dockerfile.memcached
command: 'tail -f /dev/null'
當我嘗試使用以下代碼從app
容器連接到memcached
容器內部的memcached
服務器時
require 'dalli'
options = { :namespace => "app_v1", :compress => true }
dc = Dalli::Client.new('localhost:11211', options)
然后我變得低於錯誤
WARN -- : localhost:11211 failed (count: 0) Errno::EADDRNOTAVAIL: Cannot assign requested address - connect(2) for "localhost" port 11211
Dalli::RingError: No server available
from /usr/local/bundle/gems/dalli-2.7.10/lib/dalli/ring.rb:46:in `server_for_key'
from /usr/local/bundle/gems/dalli-2.7.10/lib/dalli/client.rb:367:in `perform'
from /usr/local/bundle/gems/dalli-2.7.10/lib/dalli/client.rb:130:in `set'
from (irb):4
from /usr/local/bin/irb:11:in `<main>'
有人可以幫助我理解和解決此問題。
變更:
dc = Dalli::Client.new('localhost:11211', options)
至 :
dc = Dalli::Client.new('memcached:11211', options)
通過compose設置容器時,它們都已連接到compose創建的默認網絡。 在這種情況下, memcached
是memcached
容器的DNS名稱,它將自動解析為容器IP。
您不能使用本地主機從另一個服務訪問一個docker服務,因為每個服務都有自己的IP地址,就像自己的小型vm。 使用服務名稱而不是localhost,而docker將使用目標服務的ip地址解析它,
`dc = Dalli::Client.new('memcached:11211', options)`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.