How to setup anycable with docker(ruby on rails)? - ruby-on-rails

How can i setup anycable(action cable) port on docker?
this is my Dockerfile for anycable
FROM ruby:2.6.3-alpine3.10
WORKDIR /home/app
COPY . /home/app/
EXPOSE 50051
CMD ["anycable"]
and this is my docker-compose
version: "3"
services:
app:
build:
context: .
dockerfile: ./dockers/app/Dockerfile
container_name: out_app
restart: unless-stopped
volumes:
- .:/app
- /app/node_modules
- /app/public/assets
- /app/public/packs
ports:
- 3000:3000
db:
build:
context: .
dockerfile: ./dockers/postgis/Dockerfile
container_name: out_db
environment:
POSTGRES_USER: ${DOCKER_DB_USER}
POSTGRES_PASSWORD: ${DOCKER_DB_PASSWORD}
POSTGRES_DB: ${DOCKER_DB_NAME}
volumes:
- /docker_data/giggle/postgres:/var/lib/postgresql/data
ports:
- 5435:5432
nginx:
build:
context: .
dockerfile: ./dockers/web/Dockerfile
container_name: out_web
restart: unless-stopped
ports:
- 80:80
- 443:443
depends_on:
- app
volumes:
- ./dockers/web/nginx.conf:/etc/nginx/conf.d/default.conf
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
certbot:
image: certbot/certbot
restart: unless-stopped
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
redis:
image: redis
volumes:
- ../../tmp/db:/var/lib/redis/data
delayed_job:
build:
context: .
dockerfile: ./dockers/delayed_job/Dockerfile
container_name: out_delayed_job
command: bundle exec rails jobs:work
depends_on:
- db
volumes:
- .:/app
anycable:
image: 'anycable/anycable-go:edge-mrb'
ports:
- "3334"
environment:
ANYCABLE_HOST: 0.0.0.0
REDIS_URL: redis://redis:6379/1
ANYCABLE_RPC_HOST: 0.0.0.0:3334
ANYCABLE_DEBUG: 1
command: bundle exec anycable
anycable:
build:
context: .
dockerfile: ./dockers/anycable/Dockerfile
container_name: anycable
command: bundle exec anycable
depends_on:
- redis

You provided anycable-go configuration. To set custom port for anycable-go server add ANYCABLE_PORT: <your port> to anycable-go image environment or expose image port like ports: ['<your_port>:8080'].
Check anycable configuration page (contains env variables info): https://docs.anycable.io/#/anycable-go/configuration

You need to setup anycable-rails by adding anycable-rails gem to your Gemfile:
gem "anycable-rails", "~> 1.1"
when using Redis broadcast adapter
gem "redis", ">= 4.0"
(and don't forget to run bundle install).
Then, run the interactive configuration wizard via Rails generators:
bundle exec rails g anycable:setup
Configuration
Next, update your Action Cable configuration:
# config/cable.yml
production:
# Set adapter to any_cable to activate AnyCable
adapter: any_cable
Install WebSocket server and specify its URL in the configuration:
For development it's likely the localhost
# config/environments/development.rb
config.action_cable.url = "ws://localhost:8080/cable"
For production it's likely to have a sub-domain and secure connection
# config/environments/production.rb
config.action_cable.url = "wss://ws.example.com/cable"
Now you can start AnyCable RPC server for your application:
$ bundle exec anycable
#> Starting AnyCable gRPC server (pid: 48111)
#> Serving Rails application from ./config/environment.rb
Don't forget to provide Rails env in production
$ RAILS_ENV=production bundle exec anycable
NOTE: you don't need to specify `-r option (see CLI docs), your application would be loaded from config/environment.rb.
And, finally, run AnyCable WebSocket server, e.g. anycable-go:
$ anycable-go --host=localhost --port=8080
INFO 2019-08-07T16:37:46.387Z context=main Starting AnyCable v0.6.2-13-gd421927 (with mruby 1.2.0 (2015-11-17)) (pid: 1362)
INFO 2019-08-07T16:37:46.387Z context=main Handle WebSocket connections at /cable
INFO 2019-08-07T16:37:46.388Z context=http Starting HTTP server at localhost:8080
You can store AnyCable-specific configuration in YAML file (similar to Action Cable one):
# config/anycable.yml
development:
redis_url: redis://localhost:6379/1
production:
redis_url: redis://my.redis.io:6379/1

Related

Rails Unable to make API request inside docker SocketError (Failed to open TCP connection to xxx:443 (getaddrinfo: Name does not resolve))

I'm really confused why I'm unable to make API requests to any site. for example, I want to run :
HTTParty.get("https://fakerapi.it/api/v1/persons")
It runs well on my machine. (without docker).
But if I run it inside docker, I got :
SocketError (Failed to open TCP connection to fakerapi.it:443 (getaddrinfo: Name does not resolve))
It happens not only for this site. But for all sites.
So I guess there's something wrong with my docker settings. But I'm not sure where to start.
I'm new to docker. So any advice means a lot to me.
Below is my docker-compose.yaml
version: '3.4'
services:
db:
image: mysql:8.0.17 #using official mysql image from docker hub
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- db_data:/var/lib/mysql
ports:
- "3307:3306"
backend:
build:
context: .
dockerfile: backend-dev.Dockerfile
ports:
- "3001:3001"
volumes:
#the host repos are mapped to the container's repos
- ./backend:/my-project
#volume to cache gems
- bundle:/bundle
depends_on:
- db
stdin_open: true
tty: true
env_file: .env
command: /bin/sh -c "rm -f tmp/pids/server.pid && rm -f tmp/pids/delayed_job.pid && bundle exec bin/delayed_job start && bundle exec rails s -p 3001 -b '0.0.0.0'"
frontend:
build:
context: .
dockerfile: frontend-dev.Dockerfile
ports:
- "3000:3000"
links:
- "backend:bb"
depends_on:
- backend
volumes:
#the host repos are mapped to the container's repos
- ./frontend/:/my-project
# env_file: .env
environment:
- NODE_ENV=development
command: /bin/sh -c "yarn dev --port 3000"
volumes:
db_data:
driver: local
bundle:
driver: local
How I try to run:
docker-compose run backend /bin/sh
rails c
HTTParty.get("https://fakerapi.it/api/v1/persons")
Any idea how can I fix this?

How to Configure Sidekiq in Docker

I need to configure Sidekiq in Docker.Below the docker compose file configuration i am using for build.I am getting cannot locate specified docker file error while building sidekiq. Without sidekiq configuration docker compose build getting success. How to configure sidekiq server in Docker?
version: '3'
volumes:
postgres_data: {}
services:
redis:
image: redis
command: redis-server
ports:
- "6379:6379"
app:
build:
context: .
dockerfile: /Users/admin/git/generic/myapp/docker/app/Dockerfile
depends_on:
- db
ports:
- 3000:3000
db:
image: postgres
volumes:
- postgres_data:/var/lib/postgresql/data
web:
build:
context: .
dockerfile: /Users/admin/git/generic/myapp/docker/web/Dockerfile
depends_on:
- app
ports:
- 80:80
sidekiq:
build: .
command: bundle exec sidekiq
depends_on:
- redis

rails + docker + sidekiq + Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)

I am getting this error while running my rails app with docker and docker-compose
Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
Please find my Docker file
# Copy the Gemfile as well as the Gemfile.lock and install
# the RubyGems. This is a separate step so the dependencies
# will be cached unless changes to one of those two files
# are made.
COPY Gemfile Gemfile.lock ./
RUN gem install bundler && bundle install --jobs 20 --retry 5
# Copy the main application.
COPY . ./app
# Expose port 3000 to the Docker host, so we can access it
# from the outside.
EXPOSE 3000
# The main command to run when the container starts. Also
# tell the Rails dev server to bind to all interfaces by
# default.
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
Please find my docker-compose.yml file
version: "2"
services:
redis:
image: redis
command: redis-server
ports:
- "6379:6379"
postgres:
image: postgres:9.4
ports:
- "5432"
app:
build: .
command: rails server -p 3000 -b '0.0.0.0'
volumes:
- .:/app
ports:
- "3000:3000"
links:
- postgres
- redis
- sidekiq
sidekiq:
build: .
command: bundle exec sidekiq
depends_on:
- redis
volumes:
- .:/app
env_file:
- .env
Thanks in Advance!
I use a docker-compose and i add in .env file
REDIS_URL=redis://redis:6379/0
Links are not required to enable services to communicate - by default, any service can reach any other service at that service’s name.
According to your docker-compose.yaml file you can access you redis container on 127.0.0.1:6379 only from host machine.
Containers communicate with each other in their network, so you can access your redis container on redis:6379 from rails app container.

How to connect with client to my postgres container in docker?

I'm following this guide:
https://docs.docker.com/compose/rails/
I have all set up and running, and I'm trying to figure out how to connect with one of my DB client (Sequel Pro or pgAdmin) to the postgres container.
I also tried to map the postgres port in order to have it served outside from the container (docker-compose.yml), without success:
version: '3'
services:
db:
image: postgres
##### Trying this...
ports:
- "5432:5432"
#####
web:
build: .
command: bundle exec rails s -p 3030 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3030:3030"
depends_on:
- db

Adding sphinx container docker-compose shows an error

i have an Ruby on Rails project, which i want to place into the containers( there are database, redis and web(includes rails project) containers). I want to add search feature, so i added a sphinx container in my compose file
docker-compose.yml
web:
dockerfile: Dockerfile-rails
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
ports:
- "3000:3000"
links:
- redis
- db
**- sphinx**
environment:
- REDISTOGO_URL=redis://user#redis:6379/
redis:
image: redis
**sphinx:
image: centurylink/sphinx**
db:
dockerfile: Dockerfile-db
build: .
env_file: .env_db
docker-compose build works fine but when i run docke-compose up i get
ERROR: Cannot start container 096410dafc86666dcf1ffd5f60ecc858760fb7a2b8f2352750f615957072d961: Cannot link to a non running container: /metartaf_sphinx_1 AS /metartaf_web_1/sphinx_1
How can i fix this ?
According to https://hub.docker.com/r/centurylink/sphinx/ the Sphinx container runs needs some amount of configuration files to run properly. See the *Daemonized usage (2). You need data source files and a configuration.
In my test, it fails to start as is with error:
FATAL: no readable config file (looked in /usr/local/etc/sphinx.conf, ./sphinx.conf)
Your docker-compose.yml shouldn't have these * in it.
If you want sphinx latest version you can do this:
web:
dockerfile: Dockerfile-rails
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
ports:
- "3000:3000"
links:
- redis
- db
- sphinx
environment:
- REDISTOGO_URL=redis://user#redis:6379/
redis:
image: redis
sphinx:
image: centurylink/sphinx:latest
db:
dockerfile: Dockerfile-db
build: .
env_file: .env_db
If you want a specific version you write this way : centurylink/sphinx:2.1.8

Resources