Search code examples
dockerruby-on-rails-7ruby-3

Docker: TCP/IP connections on port 5342? connect to server: Connection refused Is the server running on host


I am using rails 3.0.0 with rails 7.

My dockerfile is as:

FROM ruby:3.0.0-alpine

RUN apk add --update --virtual \
    runtime-deps \
    postgresql-client\
    build-base \
    libxml2-dev \
    libxslt-dev \
    yarn \
    libffi-dev \
    readline \
    build-base \
    postgresql-dev \
    libc-dev \
    linux-headers \
    readline-dev \
    file \
    imagemagick \
    git \
    tzdata \
    && rm -rf /var/cache/apk*

WORKDIR /app
COPY . /app/
ENV BUNDLE_PATH /gems
RUN yarn install
RUN bundle install

ENTRYPOINT ["bin/rails"]
CMD ["s", "-b", "0.0.0.0"]

EXPOSE 3000

docker-compose.yml is as:

version: '3.8'
services:
  db:
    image: postgres:latest
    environment:
      - POSTGRES_PASSWORD=password
    ports:
      - "5433:5432"
    volumes:
      - "dbdata:/var/lib/postgresql/data"

  redis:
    image: redis:latest
    ports:
      - "6380:6379"

  web:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - db
      - redis
    environment:
      - DATABASE_URL=postgres://postgres:password@db:5433/postgres
      - REDIS_URL=redis://redis:6380
    volumes:
      - .:/app

volumes:
  dbdata:

database.yml file is as:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  port: 5432

development:
  <<: *default
  database: my_app_development
  username: postgres
  password: password

test:
  <<: *default
  database: my_app_test

#   production:
#     url: <%= ENV["MY_APP_DATABASE_URL"] %>

production:
  <<: *default
  database: my_app_production
  username: my_app
  password: <%= ENV["MY_APP_DATABASE_PASSWORD"] %>

using ubunut 20.04 LTS. getting error as: ActiveRecord::ConnectionNotEstablished could not connect to server: Connection refused Is the server running on host "172.22.0.3" and accepting TCP/IP connections on port 5433?

How to resolve this issue, any suggestion would help, thanks in advance.


Solution

  • reached to solution from above shared suggestions:

    docker-compose.yml

     version: '3.8'
        services:
          db:
            image: postgres:latest
            environment:
              POSTGRES_PASSWORD: password
              command: -p 5000
              ports: "5000:5432"
            volumes:
              - "dbdata:/var/lib/postgresql/data"
        
          redis:
            image: redis:latest
            ports:
              - "6380:6379"
        
          web:
            build: .
            ports:
              - "3000:3000"
            depends_on:
              - db
              - redis
            environment:
              - DATABASE_URL=postgres://postgres:password@db:5432/postgres
              - REDIS_URL=redis://redis:6379
            volumes:
              - .:/app
        
        volumes:
          dbdata:
    

    database.yml:

    default: &default
      adapter: postgresql
      encoding: unicode
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
    
    development:
      <<: *default
      database: my_app_development
      username: postgres
      password: password
    
    test:
      <<: *default
      database: my_app_test
    
    #   production:
    #     url: <%= ENV["MY_APP_DATABASE_URL"] %>
    
    production:
      <<: *default
      database: my_app_production
      username: my_app
      password: <%= ENV["MY_APP_DATABASE_PASSWORD"] %>
    

    Dockerfile:

    FROM ruby:3.0.0-alpine
    
    RUN apk add --update --virtual \
        runtime-deps \
        postgresql-client\
        build-base \
        libxml2-dev \
        libxslt-dev \
        yarn \
        libffi-dev \
        readline \
        build-base \
        postgresql-dev \
        libc-dev \
        linux-headers \
        readline-dev \
        file \
        imagemagick \
        git \
        tzdata \
        && rm -rf /var/cache/apk*
    
    WORKDIR /app
    COPY . /app/
    ENV BUNDLE_PATH /gems
    RUN yarn install
    RUN bundle install
    
    ENTRYPOINT ["bin/rails"]
    CMD ["s", "-b", "0.0.0.0"]
    
    EXPOSE 3000
    

    Now with these configurations rails app is running. please update how to do it better, thanks.