Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
962 views
in Technique[技术] by (71.8m points)

grpc - How do I enable both ReST API and Anycable in Rails 6 app running in Docker

I have an existing Rails 6.1 app running in Docker. I want to add Anycable with anycable-go running as a service via Docker-compose. I followed this guide: https://medium.com/@inklingviashruti/actioncable-anycable-with-angular-9-d1ecfa39f319

I had to use the following to get grpc working: https://dustri.org/b/error-loading-shared-library-ld-linux-x86-64so2-on-alpine-linux.html

After integrating these solutions, I was able to get the app to build and bring up app. However, only the RPC server is starting.

Docker-compose.yml:

version: "3.7"

services:

  redis:
    image: redis:alpine
    env_file:
      .env
    volumes:
      - redis:/var/lib/redis/data
    networks:
      - app-network

  db:
    image: postgis/postgis:13-3.1-alpine
    env_file:
      .env
    volumes:
      - postgis:/var/lib/postgresql/data
    networks:
      - app-network
      
  # anycable-go -—host=localhost -—port=3334
  anycable:
    image: anycable/anycable-go
    env_file:
      .env
    networks:
      - proxy
      - app-network

  api:
    build: .
    command: bash -c " rm -f tmp/pids/server.pid &&  bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/usr/src/app
    ports:
      - "3000:3000"
    depends_on:
      - db
      - redis
    env_file:
      .env
    networks:
      - proxy
      - app-network

networks:
  app-network:
    driver: bridge
  proxy:
    external:
      name: nginx-proxy

volumes:
  redis:
  postgis:
  

Dockerfile:

FROM ruby:2.7.2-alpine

ENV INSTALL_PATH /usr/src
RUN mkdir -p $INSTALL_PATH

RUN apk add --no-cache build-base bash git libpq postgresql libxml2-dev postgresql-dev postgresql-client redis

RUN gem install bundler

# for anycable
RUN apk add libc6-compat
RUN ln -s /lib/libc.musl-x86_64.so.1 /lib/ld-linux-x86-64.so.2
RUN gem install grpc
RUN gem install grpc-tools

WORKDIR /usr/src/app
COPY Gemfile* ./
RUN bundle install
COPY . .

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT [ "entrypoint.sh" ]

EXPOSE 3000

# Start the main process.
CMD [ "rails", "server", "-b", "0.0.0.0" ]

entrypoint.sh

#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /usr/src/app/tmp/pids/server.pid

#start AnyCable gRPC server
# anycable
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

On initial setup of anycable in the code above, the line # anycable was uncommented but made no difference in the RPC server solely being started.

config/cable.yml:

development:
  adapter: any_cable

test:
  adapter: any_cable

production:
  adapter: any_cable

config/anycable.yml:

development:
  redis_url:  <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
production:
  redis_url:  <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>

config/application.rb:

require_relative 'boot'

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
# require "sprockets/railtie"
# require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module HeadcountApi
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.1

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.

    # Only loads a smaller set of middleware suitable for API only apps.
    # Middleware like session, flash, cookies can be added back manually.
    # Skip views, helpers and assets when generating a new resource.
    config.api_only = true
  end
end

Would appreciate any help getting both the original rails_api interface working along the Anycable.

question from:https://stackoverflow.com/questions/65888485/how-do-i-enable-both-rest-api-and-anycable-in-rails-6-app-running-in-docker

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...