ruby-on-railsdockerdocker-composedockerfiledalli

127.0.0.1:11211 is down party_manager | DalliError: No server available


I am using docker. Whenever my application is trying to read or write the cache it is getting following error :

Cache read: send_otp_request_count_3
Dalli::Server#connect 127.0.0.1:11211
127.0.0.1:11211 failed (count: 0) Errno::ECONNREFUSED: 
Connection refused - connect(2) for "127.0.0.1" port 11211
DalliError: No server available

My Gemfile has:

gem 'dalli'

My Dockerfile is:

FROM ruby:2.3.6

RUN mkdir -p /railsapp
WORKDIR /railsapp

RUN apt-get update && apt-get install -y nodejs --no-install-recommends
RUN apt-get update && apt-get install -y mysql-client  --no-install-recommends

COPY Gemfile /railsapp/
COPY Gemfile.lock /railsapp/

RUN bundle install

COPY . /railsapp

EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]

My docker-compose.yml file is:

version: '3.3'

services:
  cache:
    image: memcached:1.4-alpine

  mysql:
    image: mysql
    restart: always
    ports:
      - "3002:3002"
    volumes:
      - /var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=dev

  web:
    container_name: party_manager
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - RAILS_ENV=development
    ports:
      - '3000:3000'
    volumes:
      - .:/railsapp
    links:
      - mysql

I have also also installed the memchaced in container shell through

docker exec -it 499e3d1efe44 bash

**499e3d1efe44 is my container id.

Then install the gem with command: gem install memcached


Solution

  • By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

    So according to your docker-compose.yaml file you can access you cache container on cache:112111 from web container.