4

I am attempting to create a docker-compose file that will start a Rails 3.2.13 server and link it to an Oracle DB. So far I have been unable to make the connection from Rails to the Oracle DB. This is the error from Rails

oci8.c:513:in oci8lib_191.so: ORA-12154: TNS:could not resolve the connect identifier specified (OCIError)

docker-compose.yml

version: '3.1'

services:
    app:
        image: local-rails:0.0.1
        build: .
        ports:
            - "3000:3000"
        volumes:
            - "${PWD}:/usr/src/app"
        depends_on:
            - db
        command: bundle exec rails s -p 3000 -b '0.0.0.0'
    db:
        image: wnameless/oracle-xe-11g
        environment:
          - ORACLE_ALLOW_REMOTE=true
        ports:
            - "49160:22"
            - "49161:1521"

Variables in my Dockerfile

ENV RAILS_ENV local

# Notice the use of "db" service name as the HOST
ENV RAILS_DB (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db)(PORT=49161))(CONNECT_DATA=(SID=xe)))

# I've also tried setting DATABASE_URL directly w/ no yml config
# ENV DATABASE_URL db:49161/xe

database.yml

local:
  adapter: oracle_enhanced
  database: <%= RAILS_DB %>
  username: ****
  password: ****

I have successfully started both services separately - Rails pointing to external Oracle DB with known working configurations. I have also been able to connect to the containerized Oracle DB via Oracle SQL Developer. The problem is getting them to talk to one another.

5
  • 1
    It seems that your links configuration is working. keyforms service can talk to oracledb. ORA-12154 is a common error related to a file named as tnsnames.ora which is a configuration file of Oracle. I can't help you further now but you can check this error code on Google where you will find a lot of results. Hope this helps: youtube.com/watch?v=QVJ1xO2V7Dc Commented Jul 17, 2017 at 19:35
  • I should mention that I am able to connect to this database using Oracle SQL Developer Commented Jul 17, 2017 at 20:49
  • 1
    depends_on also implies an entry in links, so no need for both. Commented Jul 18, 2017 at 6:14
  • 2
    I think the docker setup is correct and you are having issues with oracle configuration. Commented Jul 18, 2017 at 6:15
  • I think you may be right about that Grimmy. I will have our DBAs look at the settings. Commented Jul 18, 2017 at 16:26

2 Answers 2

3

I was able to get this working by declaring the env variable RAILS_DB to the docker-compose file environment declaration instead of declaring it in the Dockerfile. Here's what I did to get Rails working with an Oralce DB locally.

docker-compose.yml

version: '3.1'

services:
    app:
        image: rails-app:0.0.1
        build: .
        ports:
            - "3000:3000"
        volumes:
            - "${PWD}:/home/app"
        environment:
            RAILS_ENV: local
            # Note the use of "db" as the HOST
            RAILS_DB: (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db)(PORT=1521))(CONNECT_DATA=(SID=xe)))
        depends_on:
            - db
        networks:
            - network
        command: bundle exec rails s -p 3000 -b '0.0.0.0'
    db:
        image: sath89/oracle-12c
        ports:
            - 1521:1521
        volumes:
            - oracle/data:/u01/app/oracle
        networks:
            - network
        environment:
            - ORACLE_ALLOW_REMOTE=true
            - WEB_CONSOLE=false
            - ORACLE_SID=xe

database.yml

local:
    adapter: oracle_enhanced
    database: <%= ENV['RAILS_DB'] %>
    username: user
    password: pw
    encodeing: utf8

Dockerfile

In order to use this, you must first download 3 files from Oracle and put them in a new directory in your project called "oracle".

  1. oracle-instantclient12.2-basiclite-12.2.0.1.0-1.x86_64.rpm
  2. oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64.rpm
  3. oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm

Now you may refer to them as I do below.

FROM ruby:1.9.3

EXPOSE 3000

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    && apt-get install alien -y \
    && apt-get install freetds-dev -y \
    && apt-get install libaio1

COPY ./oracle /home/oracle

# Get these files from http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
RUN alien -i /home/oracle/oracle-instantclient12.2-basiclite-12.2.0.1.0-1.x86_64.rpm \
    && alien -i /home/oracle/oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64.rpm \
    && alien -i /home/oracle/oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm

ENV ORACLE_HOME /usr/lib/oracle/12.2/client64
ENV PATH ${ORACLE_HOME}/bin:$PATH
ENV LD_LIBRARY_PATH ${ORACLE_HOME}/lib
ENV ORACLE_SID=xe

WORKDIR /home/app

COPY Gemfile* ./

RUN bundle install

RUN apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /home/app/tmp/pids/*

This will only get you started. You'll have to configure the database users, tablespaces etc. Remember to run your migrations.

While this will get anyone who wants to run Rails with an Oracle DB locally, keep in mind that you may have to tweak some of my settings in order to get it to work for your specific setup. Hopefully this is a good start.

Sign up to request clarification or add additional context in comments.

Comments

0

Using links in docker is deprecated: https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/

In my project we are using custom networks in docker-compose like this:

version: "3"
services:
  app:
    ...
    networks:
      - my_network_name
  db:
    ...
    networks:
      - my_network_name

networks:
    my_network_name:
        driver: bridge

After running containers you can verify that connection is working:

$ docker-compose exec app bash
[root@9939116aec31 /]# ping db
PING db (172.18.0.3) 56(84) bytes of data.
64 bytes from nettest_db_1.nettest_my_network_name (172.18.0.3): icmp_seq=1 ttl=64 time=0.078 ms
64 bytes from nettest_db_1.nettest_my_network_name (172.18.0.3): icmp_seq=2 ttl=64 time=0.104 ms
^C
--- db ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.