Docker Rails App dont connect Postgres Container

Hello im new to Docker and i cant solve this Problem. Here is my code and my containers log

docker-compose.yml

version: '3'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    command: bundle exec rails s -p 3000 -e development -b '0.0.0.0'
    depends_on: 
      - database
    ports:
      - '3000:3000'
    volumes: 
      - .:/app
      - gem_cache:/usr/local/bundle/gems
      - node_modules:/app/node_modules
    environment:
      RAILS_ENV: development
      POSTGRES_DB: test_app_development
      POSTGRES_USER: testuser
      DATABASE_HOST: database

  database:
    image: postgres
    volumes:
      - db_data:/var/lib/postgresql/data
    ports:
        - '5432'
    environment:
      POSTGRES_DB: test_app_development
      POSTGRES_USER: testuser
      POSTGRES_PASSWORD: test2020

volumes:
  gem_cache:
  db_data:
  node_modules: 

My Dockerfile:

FROM ruby:2.7.1-alpine
ENV BUNDLER_VERSION=2.1.4


# Add basic packages
RUN apk add --update --no-cache 
      build-base 
      postgresql-dev 
      git 
      nodejs 
      yarn 
      tzdata 
      file
    
RUN gem install bundler


WORKDIR /app

# Install standard gems
COPY Gemfile* ./

RUN bundle check || bundle install

# Install standard Node modules
COPY package.json yarn.lock ./
RUN yarn install --check-files

COPY . ./

My database.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: testuser
  password: <%= Rails.application.credentials.test[:pw] %>
  host: localhost
  port: 5432

development:
  <<: *default
  database: test_app_development


  <<: *default
  database: test_app_test

production:
  <<: *default
  database: test_app_production

in my postgres container log it says:

PostgreSQL Database directory appears to contain a database; Skipping initialization
2020-11-10 01:38:43.081 UTC [1] LOG: starting PostgreSQL 13.0 (Debian 13.0-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-11-10 01:38:43.082 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2020-11-10 01:38:43.082 UTC [1] LOG: listening on IPv6 address "::", port 5432
2020-11-10 01:38:43.086 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-11-10 01:38:43.093 UTC [26] LOG: database system was shut down at 2020-11-10 01:38:42 UTC
2020-11-10 01:38:43.099 UTC [1] LOG: database system is ready to accept connections

and in my test_app container i get this

=> Booting Puma
=> Rails 6.0.3.4 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.6 (ruby 2.7.1-p83), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Started GET "/" for 172.18.0.1 at 2020-11-10 02:04:37 +0000
Cannot render console from 172.18.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
PG::ConnectionBad (could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
):
pg (1.2.3) lib/pg.rb:58:in `initialize'
pg (1.2.3) lib/pg.rb:58:in `new'
pg (1.2.3) lib/pg.rb:58:in `connect'

i tried so often to troublshoot in docker, reset the wsl2 containers and compose again, didnt worked. I guess i have an mistake in my code so the app cant connect to postgres, but i cant find it. Please help me.

Source: Docker Questions

LEAVE A COMMENT