PG: Operation timed out Is the server running on that host and accepting TCP/IP connections?

Viewed 35

I have a rails app with docker setup. I have two containers and i have verified they are on the same network but still i am getting error
PG::ConnectionBad: connection to server at "192.168.160.2", port 5432 failed: Operation timed out Is the server running on that host and accepting TCP/IP connections? I have changed network too, also removed all images and volumes but still no luck. Everything was working fine until I composed down the containers and when i tried to run again make setup command i started facing the issue.

Here is my docker-compose.yml


x-toolbox-environment: &toolbox-environment
  - PROJECT=frameworq
  - APPLICATION=portal
  - AWS_REGION
  - AWS_DEFAULT_REGION
  - AWS_ACCESS_KEY_ID
  - AWS_SECRET_ACCESS_KEY
  - AWS_SESSION_TOKEN
  - AWS_SECURITY_TOKEN
  - AWS_SESSION_EXPIRATION

x-web-volumes: &web-volumes
  - .:/app:cached
  - bundle_cache:/usr/local/bundle
  - rails_cache:/app/tmp/cache:cached
  - public_cache:/app/public:cached

x-web-environment: &web-environment
  - RAILS_PORT=4000
  - MAILER_FROM=noreply@frameworq.com
  - APP_DOMAIN=lvh.me

services:
  web:
    build: .
    volumes: *web-volumes
    ports:
      - '4000:4000'
      - '3035:3035'
    environment: *web-environment
    command: bash -c "bundle exec foreman s"
    links:
      - db

  db:
    image: postgres:12
    ports:
      - 5432:5432
    volumes:
      - postgres:/var/lib/postgresql:cached
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust

  toolbox:
    image: partos/ecs_toolbox:latest
    environment: *toolbox-environment
    volumes:
      - .:/app:cached
      - /var/run/docker.sock:/var/run/docker.sock
      - .aws:/root/.aws

  remote:
    build: ./docker/remote
    environment: *toolbox-environment
    entrypoint: ""
    volumes:
      - ./docker/remote:/app:cached
      - .aws:/root/.aws


volumes:
  bundle_cache:
  rails_cache:
  postgres:
  public_cache:
  localstack_data:
    driver: local

database.yml

# # SQL Server (2012 or higher required)
# #
# # Install the adapters and driver
# #   gem install tiny_tds
# #   gem install activerecord-sqlserver-adapter
# #
# # Ensure the activerecord adapter and db driver gems are defined in your Gemfile
# #   gem 'tiny_tds'
# #   gem 'activerecord-sqlserver-adapter'
# #
# default: &default
#   adapter: sqlserver
#   encoding: utf8
#   azure: false
#   mode: dblib
#   host: localhost
#   username: sa
#   password: this-is-the-kms-test-database-p2ssw0rd

# azure: &azure
#   adapter: sqlserver
#   encoding: utf8
#   azure: true
#   mode: dblib
#   host: <%= Rails.application.credentials.database_host %>
#   username: <%= Rails.application.credentials.database_user %>
#   password: <%= Rails.application.credentials.database_password %>

# #----------

# development:
#   <<: *default
#   database: kmsDev

# legacy:
#   <<: *azure
#   database: kmsprod

# # Warning: The database defined as "test" will be erased and
# # re-generated from your development database when you run "rake".
# # Do not set this db to the same as development or production.
# test:
#   <<: *default
#   database: kmsTest

# production:
#   <<: *azure
#   database: <%= ENV['DATABASE_NAME'] %>

default: &default
  adapter: postgresql
  encoding: unicode
  host: <%= ENV["DB_HOST"] %>
  database: <%= ENV["DB_NAME"] %>
  username: <%= ENV["DB_USERNAME"] %>
  password: <%= ENV["DB_PASSWORD"] %>
  port: <%= ENV["DB_PORT"] %>
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>


development:
  <<: *default
  host: <%= ENV.fetch("PGHOST") { 'db' } %>
  database: <%= ENV.fetch("POSTGRES_DB") { 'frameworq_development' } %>
  username: <%= ENV.fetch("POSTGRES_USER") { 'postgres' } %>
  password: <%= ENV.fetch("POSTGRES_PASSWORD") { '' } %>
  port: <%= ENV.fetch("PGPORT") { 5432 } %>

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: frameworq_test
  host: <%= ENV.fetch("PGHOST") { 'db' } %>
  database: <%= ENV.fetch("POSTGRES_DB") { 'frameworq_test' } %>
  username: <%= ENV.fetch("POSTGRES_USER") { 'postgres' } %>
  password: <%= ENV.fetch("POSTGRES_PASSWORD") { '' } %>
  port: <%= ENV.fetch("PGPORT") { 5432 } %>

qa:
  <<: *default

staging:
  <<: *default

production:
  <<: *default

Dockerfile

FROM ruby:3.0.4-alpine

RUN apk --update add --no-cache build-base bash git vim curl postgresql-dev openssl-dev nodejs npm yarn tzdata less \
imagemagick postgresql-client gcompat chromium chromium-chromedriver

RUN mkdir /app
WORKDIR /app

COPY Gemfile Gemfile.lock ./

RUN bundle update --bundler
RUN bundle install --jobs 5

ADD . /app

RUN yarn install

RUN RAILS_SECRET_KEY_BASE=secret_key_base RAILS_ENV=production bundle exec rails assets:precompile apipie:cache

EXPOSE 5000

VOLUME ["/app/public", "/usr/local/bundle"]

CMD bash -c "bundle exec puma -C config/puma.rb"

Makefile

# DEVELOPMENT TARGETS
setup: destroy_shell build_shell prepare_shell up

destroy_shell:
    docker-compose down -v

build_shell:
    docker-compose build

prepare_shell:
    docker-compose run --rm web bash -c "make prepare"

up:
    docker-compose run --rm --service-ports --use-aliases web

shell:
    docker-compose run --rm web bash

toolbox:
    docker-compose run --rm toolbox bash

prepare_bundle:
    bundle -j 4
    yarn

prepare_db_first_time:
    bundle exec rails db:create db:schema:load

prepare_db:
    bundle exec rails db:migrate:with_data

prepare_test_db:
    bundle exec rails db:create db:schema:load

prepare: prepare_bundle prepare_db_first_time

check: test_web fix_lint_web lint_front

lint_web:
    bundle exec rubocop

fix_lint_web:
    bundle exec rubocop -A

test_web:
    RAILS_ENV=test bundle exec rails assets:precompile && bundle exec rails test

test_compiled_web:
    RAILS_ENV=test bundle exec rails test

prepare_test_assets:
    # We're not using webpacker now, importmaps instead
    # cp -R public/packs public/packs-test
    RAILS_ENV=test bundle exec rails assets:precompile

test_web_with_db: prepare_test_assets prepare_test_db test_compiled_web

lint_front:
    # We're using importmaps, not yarn now
    # yarn install && yarn run eslint --fix app/javascript/

generate_ts_routes:
    docker-compose run --rm web bash -c 'bundle exec rails ts:routes'

db-reset:
    bundle exec rails db:drop
    bundle exec rails db:create
    bundle exec rails db:schema:load
    bundle exec rails db:migrate
    bundle exec rails db:seed

eslint:
    # We're using importmaps, not yarn now
    # NODE_ENV=development yarn install
    # yarn eslint --ext .js --ext .jsx /app/app/javascript/

tailwind:
    docker-compose run --rm web bash -c 'bin/rails tailwindcss:watch'

guard:
    docker-compose run --rm web bash -c 'bundle exec guard'

console:
    docker-compose run --rm web bash -c 'rails c'

taildev:
    docker-compose run --rm web bash -c 'tail -f log/development.log'

tailstaging:
    docker-compose run --rm web bash -c 'tail -f log/staging.log'
0 Answers
Related