JeremyKirkham
JeremyKirkham

Reputation: 1077

Coupling Compojure application with rethinkdb and docker-compose

I'm trying to use docker-compose to link my clojure compojure app with a rethinkdb container:

docker-compose.yml:

version: "2"
services:
  web:
    build: .
    ports:
      - "8080:8080"
    links:
      - rethinkdb
  rethinkdb:
    image: rethinkdb
    ports:
      - "8080"
      - "28015"
      - "29015"

However I keep getting an error

"Error connecting to RethinkDB database"

My handler.clj:

(ns test.handler
  (:gen-class)
  (:require [compojure.core :refer :all]
            [compojure.route :as route]
            [environ.core :refer [env]]
            [ring.middleware.defaults :refer [wrap-defaults site-defaults]]
            [ring.middleware.reload :as reload]
            [rethinkdb.query :as r])
  (:use org.httpkit.server
        [compojure.handler :only [site]]))

(defn users [request]
  (with-open [conn (r/connect :host "rethinkdb" :port 28015 :db "test")]
    (r/run (r/db-create "test") conn)

    (-> (r/db "test")
        (r/table-create "authors")
        (r/run conn))))


(defroutes app-routes
  (GET "/" [] { :status  200
                :headers {"Content-Type" "text/html"}
                :body    "Server running!"})
  (GET "/users" [] users))

(defn -main [& args] ;; entry point, lein run will pick up and start from here
  (let [handler (if (in-dev?)
                  (reload/wrap-reload (site #'app-routes)) ;; only reload when dev
                  (site app-routes))]
    (run-server handler {:port 8080})))

The issue occurs when I hit 127.0.0.1:8080/users which is the route that calls the rethinkdb code. To start the service I'm running docker-compose up -d. I took the code that connects to rethinkdb from https://github.com/apa512/clj-rethinkdb.

This is the error I get from my web container logs:

SEVERE: Error connecting to RethinkDB database
java.net.ConnectException: Connection refused: /127.0.0.1:28015

Upvotes: 0

Views: 233

Answers (2)

JeremyKirkham
JeremyKirkham

Reputation: 1077

So the issue was in my docker-compose.yml - I needed to expose the rethinkdb 28015 port for both container AND host:

  rethinkdb:
    image: rethinkdb
    ports:
        - "8080"
        - "28015:28015"

Once I updated this I was successfully able to connect to my rethinkdb container from my clojure container.

Upvotes: 1

Michiel Borkent
Michiel Borkent

Reputation: 34800

What I would try: open a REPL port from the web application so you can diagnose from the REPL. This is Clojure after all.

Also worth checking: can you connect with a different rethink client while docker-compose is running?

Upvotes: 0

Related Questions