Rails send request to other container [Failed to open TCP connection]










1














I am trying to do multiple node distribution systems by multiple Rails node + docker + MySQL + Redis.



Therefore, my main node needs to communicate with other nodes.



Here is my docker-compose.yml



**version: '2'
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=pubsub_1_development
- MYSQL_USER=appuser
- MYSQL_PASSWORD=password
ports:
- "3308:3306"

redis:
image: 'redis:4.0-alpine'

app:
image: pubsub_2:1.0.8
command: /bin/sh -c "rm -f ./tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ".:/pubsub_2"
ports:
- "3001:3000"
depends_on:
- db
- redis
- sub_node
- pub_node
links:
- db
- redis
- sub_node
- pub_node
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
SUB_NODE: 0.0.0.0:4001
PUB_NODE: 0.0.0.0:4000
stdin_open: true
tty: true

sub_node:
image: sub_node:1.0.1
command: /bin/sh -c "rm -f /sub_node/tmp/pids/server.pid && bundle exec rails s -p 4001 -b '0.0.0.0'"
ports:
- "4001:4001"
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
tty: true
expose:
- "4001"


pub_node:
image: pub_node:1.0.1
command: /bin/sh -c "rm -f /pub_node/tmp/pids/server.pid && bundle exec rails s -p 4000 -b '0.0.0.0'"
ports:
- "4000:4000"
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
tty: true
expose:
- "4000"**


However, when I try to use app node to send the request to pub_node, it threw this error.



Errno::ECONNREFUSED: Failed to open TCP connection to 127.0.0.1:4000 (Connection refused - connect(2) for "127.0.0.1" port 4000)
from /usr/local/lib/ruby/2.5.0/net/http.rb:939:in `rescue in block in connect'


I was doing post by this code.



rvlist = '127.0.0.1:4000'
HTTParty.post("http://#rvlist/publish", options)


It works in my development mode without the docker environment.










share|improve this question





















  • Can you try to POST with rvlist = 'pub_node:4000' ?
    – Zoran Majstorovic
    Nov 12 '18 at 22:52










  • Yes!!! it works!! Would you please answer the question and maybe leave some document or reference? I will pick you as the answer!
    – Coda Chang
    Nov 13 '18 at 2:26















1














I am trying to do multiple node distribution systems by multiple Rails node + docker + MySQL + Redis.



Therefore, my main node needs to communicate with other nodes.



Here is my docker-compose.yml



**version: '2'
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=pubsub_1_development
- MYSQL_USER=appuser
- MYSQL_PASSWORD=password
ports:
- "3308:3306"

redis:
image: 'redis:4.0-alpine'

app:
image: pubsub_2:1.0.8
command: /bin/sh -c "rm -f ./tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ".:/pubsub_2"
ports:
- "3001:3000"
depends_on:
- db
- redis
- sub_node
- pub_node
links:
- db
- redis
- sub_node
- pub_node
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
SUB_NODE: 0.0.0.0:4001
PUB_NODE: 0.0.0.0:4000
stdin_open: true
tty: true

sub_node:
image: sub_node:1.0.1
command: /bin/sh -c "rm -f /sub_node/tmp/pids/server.pid && bundle exec rails s -p 4001 -b '0.0.0.0'"
ports:
- "4001:4001"
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
tty: true
expose:
- "4001"


pub_node:
image: pub_node:1.0.1
command: /bin/sh -c "rm -f /pub_node/tmp/pids/server.pid && bundle exec rails s -p 4000 -b '0.0.0.0'"
ports:
- "4000:4000"
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
tty: true
expose:
- "4000"**


However, when I try to use app node to send the request to pub_node, it threw this error.



Errno::ECONNREFUSED: Failed to open TCP connection to 127.0.0.1:4000 (Connection refused - connect(2) for "127.0.0.1" port 4000)
from /usr/local/lib/ruby/2.5.0/net/http.rb:939:in `rescue in block in connect'


I was doing post by this code.



rvlist = '127.0.0.1:4000'
HTTParty.post("http://#rvlist/publish", options)


It works in my development mode without the docker environment.










share|improve this question





















  • Can you try to POST with rvlist = 'pub_node:4000' ?
    – Zoran Majstorovic
    Nov 12 '18 at 22:52










  • Yes!!! it works!! Would you please answer the question and maybe leave some document or reference? I will pick you as the answer!
    – Coda Chang
    Nov 13 '18 at 2:26













1












1








1







I am trying to do multiple node distribution systems by multiple Rails node + docker + MySQL + Redis.



Therefore, my main node needs to communicate with other nodes.



Here is my docker-compose.yml



**version: '2'
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=pubsub_1_development
- MYSQL_USER=appuser
- MYSQL_PASSWORD=password
ports:
- "3308:3306"

redis:
image: 'redis:4.0-alpine'

app:
image: pubsub_2:1.0.8
command: /bin/sh -c "rm -f ./tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ".:/pubsub_2"
ports:
- "3001:3000"
depends_on:
- db
- redis
- sub_node
- pub_node
links:
- db
- redis
- sub_node
- pub_node
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
SUB_NODE: 0.0.0.0:4001
PUB_NODE: 0.0.0.0:4000
stdin_open: true
tty: true

sub_node:
image: sub_node:1.0.1
command: /bin/sh -c "rm -f /sub_node/tmp/pids/server.pid && bundle exec rails s -p 4001 -b '0.0.0.0'"
ports:
- "4001:4001"
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
tty: true
expose:
- "4001"


pub_node:
image: pub_node:1.0.1
command: /bin/sh -c "rm -f /pub_node/tmp/pids/server.pid && bundle exec rails s -p 4000 -b '0.0.0.0'"
ports:
- "4000:4000"
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
tty: true
expose:
- "4000"**


However, when I try to use app node to send the request to pub_node, it threw this error.



Errno::ECONNREFUSED: Failed to open TCP connection to 127.0.0.1:4000 (Connection refused - connect(2) for "127.0.0.1" port 4000)
from /usr/local/lib/ruby/2.5.0/net/http.rb:939:in `rescue in block in connect'


I was doing post by this code.



rvlist = '127.0.0.1:4000'
HTTParty.post("http://#rvlist/publish", options)


It works in my development mode without the docker environment.










share|improve this question













I am trying to do multiple node distribution systems by multiple Rails node + docker + MySQL + Redis.



Therefore, my main node needs to communicate with other nodes.



Here is my docker-compose.yml



**version: '2'
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=pubsub_1_development
- MYSQL_USER=appuser
- MYSQL_PASSWORD=password
ports:
- "3308:3306"

redis:
image: 'redis:4.0-alpine'

app:
image: pubsub_2:1.0.8
command: /bin/sh -c "rm -f ./tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ".:/pubsub_2"
ports:
- "3001:3000"
depends_on:
- db
- redis
- sub_node
- pub_node
links:
- db
- redis
- sub_node
- pub_node
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
SUB_NODE: 0.0.0.0:4001
PUB_NODE: 0.0.0.0:4000
stdin_open: true
tty: true

sub_node:
image: sub_node:1.0.1
command: /bin/sh -c "rm -f /sub_node/tmp/pids/server.pid && bundle exec rails s -p 4001 -b '0.0.0.0'"
ports:
- "4001:4001"
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
tty: true
expose:
- "4001"


pub_node:
image: pub_node:1.0.1
command: /bin/sh -c "rm -f /pub_node/tmp/pids/server.pid && bundle exec rails s -p 4000 -b '0.0.0.0'"
ports:
- "4000:4000"
environment:
DB_USER: root
DB_NAME: pubsub_1_development
DB_PASSWORD: password
DB_HOST: db
REDIS_CABLE_PORT: redis://redis:6379/1
tty: true
expose:
- "4000"**


However, when I try to use app node to send the request to pub_node, it threw this error.



Errno::ECONNREFUSED: Failed to open TCP connection to 127.0.0.1:4000 (Connection refused - connect(2) for "127.0.0.1" port 4000)
from /usr/local/lib/ruby/2.5.0/net/http.rb:939:in `rescue in block in connect'


I was doing post by this code.



rvlist = '127.0.0.1:4000'
HTTParty.post("http://#rvlist/publish", options)


It works in my development mode without the docker environment.







ruby-on-rails docker docker-compose






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 20:58









Coda Chang

1,58221941




1,58221941











  • Can you try to POST with rvlist = 'pub_node:4000' ?
    – Zoran Majstorovic
    Nov 12 '18 at 22:52










  • Yes!!! it works!! Would you please answer the question and maybe leave some document or reference? I will pick you as the answer!
    – Coda Chang
    Nov 13 '18 at 2:26
















  • Can you try to POST with rvlist = 'pub_node:4000' ?
    – Zoran Majstorovic
    Nov 12 '18 at 22:52










  • Yes!!! it works!! Would you please answer the question and maybe leave some document or reference? I will pick you as the answer!
    – Coda Chang
    Nov 13 '18 at 2:26















Can you try to POST with rvlist = 'pub_node:4000' ?
– Zoran Majstorovic
Nov 12 '18 at 22:52




Can you try to POST with rvlist = 'pub_node:4000' ?
– Zoran Majstorovic
Nov 12 '18 at 22:52












Yes!!! it works!! Would you please answer the question and maybe leave some document or reference? I will pick you as the answer!
– Coda Chang
Nov 13 '18 at 2:26




Yes!!! it works!! Would you please answer the question and maybe leave some document or reference? I will pick you as the answer!
– Coda Chang
Nov 13 '18 at 2:26












1 Answer
1






active

oldest

votes


















0














Networking in docker-compose:




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.




Reference: https://docs.docker.com/compose/networking/



In your case, any container within docker-compose can open the TCP connection to pub_node container using its container name and port pub_node:4000






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270007%2frails-send-request-to-other-container-failed-to-open-tcp-connection%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Networking in docker-compose:




    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.




    Reference: https://docs.docker.com/compose/networking/



    In your case, any container within docker-compose can open the TCP connection to pub_node container using its container name and port pub_node:4000






    share|improve this answer

























      0














      Networking in docker-compose:




      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.




      Reference: https://docs.docker.com/compose/networking/



      In your case, any container within docker-compose can open the TCP connection to pub_node container using its container name and port pub_node:4000






      share|improve this answer























        0












        0








        0






        Networking in docker-compose:




        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.




        Reference: https://docs.docker.com/compose/networking/



        In your case, any container within docker-compose can open the TCP connection to pub_node container using its container name and port pub_node:4000






        share|improve this answer












        Networking in docker-compose:




        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.




        Reference: https://docs.docker.com/compose/networking/



        In your case, any container within docker-compose can open the TCP connection to pub_node container using its container name and port pub_node:4000







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 10:46









        Zoran Majstorovic

        1,0921114




        1,0921114



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270007%2frails-send-request-to-other-container-failed-to-open-tcp-connection%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto