关于ruby:如何将localhost RoR应用程序绑定到话语docker容器?

How to bind localhost RoR app to discourse docker container?

我有一个RubyonRails应用程序,在0.0.0.0:3000上,在0.0.0.0:80端口上有一个ConversationDocker容器。我想将localhost ror app与docker container绑定,因为postgresql正在docker container中运行,并且想将ror app与docker container中的postgresql连接。如何在ror app和docker container postgrepsql之间建立db连接?

这是我的ROR控制器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class CronController < ApplicationController

    # slack channel hook
    $slackHook ="https://hooks.slack.com/services/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"
    $discourseHost = 'community.cloudways.com/';
    $messageText ="New Notification";

    # import http module
    require 'net/http'

=begin
    A method which build connection with postgreSQL and fetch last 24hr records
=end


    def slackNotification
        logger.debug"*******Cron Started********"
        begin
            $query ="SELECT users.username AS username, topics.title AS topic_title, topics.id AS topic_id, posts.raw AS raw FROM post_replies
                JOIN posts ON posts.id = post_replies.post_id
                JOIN users ON users.id = posts.user_id
                JOIN topics ON topics.user_id = users.id
                WHERE post_replies.created_at BETWEEN current_date AND current_date - INTERVAL '1' DAY;"

            $res = ActiveRecord::Base.connection.exec_query(query);

            # iteration on each record
            $res.each do |row|
                sendNotifications row
            end

            logger.debug"*******Cron successfully executed.********"
            render :json => {:status =>"true", :message =>"Cron successfully executed."}

        rescue Exception => e
            logger.fatal"Exception: #{e}"
        end
    end

=begin
    such method which take payload and send it to slack hook url
    @params row
=end

    def sendNotifications row
        $title = row['topic_title']
        $topicId = row['topic_id']
        $content = row['raw']
        begin
            uri = URI.parse($slackHook)
            # Full control
            http = Net::HTTP.new(uri.host, uri.port)
            response = Net::HTTP.post_form(uri, {
                'payload' => '{
                   "fallback":"#{$messageText}",
                   "text":"#{$title}",
                   "pretext":"<http://#{$discourseHost}|#{$title}>",
                   "color":"#36a64f",
                   "fields": [
                        {
                           "title":"#{$title}" ,
                           "value":"#{$content}",
                           "short": false
                        }
                    ]
                }'

            })
        rescue Exception => e
            logger.fatal" Attributes: title: #{$title}, topicId: #{$topicId}, content: #{$content}, URL: <http://#{$discourseHost}|#{$title}>"
            logger.fatal"Exception: #{e}"
        end
    end
end

发布选项映射端口docker-compose run --publish 3000:3000

您也可以在Docker YML文件中指定

端口:-"88∶80"-"3000分:3000"