Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rails on Docker: ActiveRecord::ConnectionNotEstablished: could not translate host name to address: Try again

I’m trying to dockerize my Ruby on Rails API, the output of the command "docker-compose ps" is this:

docker-compose ps

Since the containers are up, I tried to migrate my database use this command "docker-compose exec app bundle exec rails db:setup db:migrate", the output I got is this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

docker-compose migrate

About my config file, this is my Dockerfile:

FROM ruby:2.7.0-alpine

ENV BUNDLER_VERSION=2.1.2

RUN apk add --update --no-cache \
      binutils-gold \
      build-base \
      curl \
      file \
      g++ \
      gcc \
      git \
      less \
      libstdc++ \
      libffi-dev \
      libc-dev \
      linux-headers \
      libxml2-dev \
      libxslt-dev \
      libgcrypt-dev \
      make \
      netcat-openbsd \
      nodejs \
      openssl \
      pkgconfig \
      postgresql-dev \
      python \
      tzdata \
      yarn

RUN gem install bundler -v 2.1.2

WORKDIR /app

COPY Gemfile Gemfile.lock ./

RUN bundle config build.nokogiri --use-system-libraries

RUN bundle check || bundle install

#COPY package.json yarn.lock ./

RUN yarn install --check-files

COPY . ./

ENTRYPOINT ["./entrypoints/docker-entrypoint.sh"]

And this is my docker-compose.yml:

version: '3.4'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - database
    ports:
      - "3000:3000"
    volumes:
      - .:/app
      - gem_cache:/usr/local/bundle/gems
      - node_modules:/app/node_modules
    env_file: .env
    environment:
      RAILS_ENV: production
    networks:
      - letsorder

  database:
    image: postgres:13.7
    env_file:
      - ./.env
    volumes:
      - db_data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    networks:
      - letsorder


volumes:
  gem_cache:
  db_data:
  node_modules:

networks:
  letsorder:
    driver: bridge

So, I searched this issue, most of them lead to set-up a network adapter in my docker-compose.yml, I did it but I got the same error.

I would like to request your suggestions and comments how to debug and fix this issue; thanks a lot.

>Solution :

It looks like in your database.yml file you have something like

host: rorletsorderdatabase

But in your docker-compose.yml file you have

  database:
    foo

A possible fix is changing the host inside your database.yml file

  host: database

Another option is changing the service name inside your docker-compose.yml file

  rorletsorderdatabase:
    image: postgres:13.7
      env_file:
        - ./.env
      volumes:
        - db_data:/var/lib/postgresql/data
        - ./init.sql:/docker-entrypoint-initdb.d/init.sql
      networks:
        - letsorder

Also in any of the cases, you need to expose the pg port 5432 in the service definition inside docker-compose.yml

ports:
  - "5432:5432"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading