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

时间:2017-07-17 14:22:57

标签: ruby ruby-on-rails-3 docker discourse

我在rails应用程序上有一个ruby,位于0.0.0.0:3000,端口0.0.0.0:80上有话语docker容器。我想将本地主机RoR应用程序与docker容器绑定,因为postgreSQL在docker容器内运行,并希望将roR应用程序与dockerSQL连接在docker容器内。如何在RoR应用程序和docker容器postgrePSQL之间建立数据库连接。

这是我的RoR控制器。

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

1 个答案:

答案 0 :(得分:1)

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

您也可以在docker yml文件中指定

端口:      - “881:80”      - “3000:3000”