I want to hit the API with Rails on multiple docker-composes set up locally

Completion image diagram

image.png

Premise

Launch a separate Rails App with docker-compose. Here, only the settings in docker-compose are written.

What you want to do

I want to make API communication between Rails apps launched locally with different docker-compose. e.g. I want to hit the Rails App2 API of the image diagram from Rails App1

Error when trying to communicate without doing anything

I'll leave it so that you can get to this memo from the keywords.

Errno::ECONNREFUSED: Connection refused - connect(2)

Overview

  1. Create a common network
  2. Common network settings for services in each docker-compose.yml
  3. Check if the request arrives
  4. Completion

procedure

--docker network create --driver bridge common_link --docker network ls (confirm the created network) --Define networks: in the services hierarchy of docker-compose.yml for Rails App1 and Rails App2

docker-compose.yml

services:
.
.
networks:
  common_link:
    external: true

--Define networks in each container setting location of Rails App1, DB1, Rails App2, DB2

services:
  rails_app1:
    networks:
      - common_link
  db1:
    networks:
      - common_link

--Modified docker-compose.yml to avoid overlapping MySQL port of docker-compose2 with 3306

services:
  db2:
    ports:
      - "127.0.0.1:3307:3307"

--If there is a setting to bind MySQL in Rails App2 of docker-compose2, change the port value as well.

services:
  rails_app2:
    environment:
      DATABASE_URL: mysql2://root:root@mysql:3307

--Specify port in MySQL config of docker-compose2 e.g. /Dockerfiles/mysql/conf.d/mysql.cnf

[client]
port=3307
[mysqld]
port=3307

--Start both docker-compose1 and docker-compose2 --docker network inspect common_link (Confirmation that a total of 4 containers of Rails App1, DB1, Rails App2, DB2 belong to the same network)

e.g. Containers image (8) (1).png

--If it doesn't seem to be a problem, try ping, curl, or hit the API.

  1. docker-compose exec rails_app1 bash
  2. ping 172.19.0.5 # If the IP (172.19.0.5) confirmed in the above temporary image is set to rails_app2
  3. curl 172.19.0.5

Please let me know if there are other good settings.

Reference

https://qiita.com/reneice/items/20e981062b093264cd0a

Recommended Posts

I want to hit the API with Rails on multiple docker-composes set up locally
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
I want to add a browsing function with ruby on rails
I want to introduce the committee with Rails without getting too dirty
I want to play with Firestore from Rails
[Rails] I want to load CSS with webpacker
I want to download a file on the Internet using Ruby and save it locally (with caution)
I want to dark mode with the SWT app
I want to authenticate users to Rails with Devise + OmniAuth
I want to simplify the log output on Android
Rspec: I want to test the post-execution state when I set a method on subject
I want to set the conditions to be displayed in collection_check_boxes
[Rails] [bootstrap] I want to change the font size responsively
I want to use screen sharing on the login screen on Ubuntu 18
I want to push an app made with Rails 6 to GitHub
I want to create a form to select the [Rails] category
What I was addicted to with the Redmine REST API
I want to put the JDK on my Mac PC
I want to distinct the duplicated data with has_many through
I want to return multiple return values for the input argument
I want to pass the startup command to postgres with docker-compose.
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
After posting an article with Rails Simple Calendar, I want to reflect it in the calendar.
I want to convert an array to Active Record Relation with Rails
I want to return to the previous screen with kotlin and java!
I tried to implement the image preview function with Rails / jQuery
I want to docker-compose up Next.js!
I want to add the disabled option to f.radio_button depending on the condition
[Java] I want to perform distinct with the key in the object
[Rails] I want to add data to Params when transitioning with link_to
Rails API mode I tried to implement the keyword multiple search function using arrays and iterative processing.
Memorandum: What I was addicted to when I hit the accounting freee API
I want to select multiple items with a custom layout in Dialog
Minimal steps to set up a Ruby environment with rbenv on Ubuntu 20.04
I can't build if I set the build destination to a simulator with XCode12!
Set up ImpressPages 5.0 with LAMP on CentOS 7.3
I want to display background-ground-image on heroku.
I want to use DBViewer with Eclipse 2018-12! !!
I want to install PHP 7.2 on Ubuntu 20.04.
I tried to summarize the Stream API
Set up Docker Registry locally on CentOS 7
[Rails] [Parent-child relationship] I want to register the foreign key in the child with nil when the parent is deleted.
I want to control the start / stop of servers and databases with Alexa
[Rails / ActiveRecord] I want to validate the value before the type is converted (_before_type_cast)
I want to be able to read a file using refile with administrate [rails6]
[Rails] How to register multiple records in the intermediate table with many-to-many association
I want to play a GIF image on the Andorid app (Java, Kotlin)
Development memo ~ I want to display only the first image posted multiple times ~
Rails6 I want to make an array of values with a check box
[Eclipse] I want to use the completion function, but I want to manage to confirm the completion with spaces.
[Rails] When transitioning to a page with link_to, move to the specified location on the page
I want to separate the handling of call results according to the API caller (call trigger)
[Rails] I want to test with RSpec. We support your step [Introduction procedure]
I want to test Action Cable with RSpec test
I rewrote the Rails tutorial test with RSpec
I want to output the day of the week
Assignment to multiple variables with the ternary operator
I tried to organize the session in Rails
Publish the app made with ruby on rails
I want to use java8 forEach with index
I want to var_dump the contents of the intent