简体   繁体   中英

Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"? Dockerized Rails

Docker Compose

version: '3.8'
networks:
  development:
  test:
volumes:
  db_data:
  gem_cache:
  shared_data:
services:
  bizzy_listings_redis:
    image: redis:latest
    command: redis-server
    networks:
      - development
      - test
    volumes:
      - shared_data:/var/shared/redis
  bizzy_listings_db:
    image: postgres:14.5-alpine
    container_name: bizzy_listings_db
    volumes:
      - db_data:/var/lib/postgresql/data
      - shared_data:/var/shared
    networks:
      - development
      - test
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
    ports:
      - 5099:5432
  bizzy_listings_app:
    build:
      context: .
      dockerfile: Dockerfile.dev
    container_name: bizzy_listings_app
    volumes:
      - .:/var/app
      - shared_data:/var/shared
      - gem_cache:/usr/local/bundle/gems
    networks:
      - development
    ports:
      - 3000:3000
    stdin_open: true
    tty: true
    env_file: .env.development
    entrypoint: dev-entrypoint.sh
    command: ['rails', 'server', '-p', '3000', '-b', '0.0.0.0']
    environment:
      RAILS_ENV: development
    depends_on:
      - bizzy_listings_db
  bizzy_listings_test:
    image: bizzy_bizzy_listings_app
    container_name: bizzy_listings_test
    volumes:
      - .:/var/app
      - shared_data:/var/shared
      - gem_cache:/usr/local/bundle/gems
    networks:
      - test
    ports:
      - 3001:3000
    stdin_open: true
    tty: true
    env_file: .env.test
    entrypoint: test-entrypoint.sh
    command: ["rails", "-v"]
    environment:
      RAILS_ENV: test
    depends_on:
      - bizzy_listings_db

Dockerfile

FROM ruby:2.6.1

ENV APP_PATH /var/app
ENV BUNDLE_VERSION 2.1.4
ENV BUNDLE_PATH /usr/local/bundle/gems
ENV TMP_PATH /tmp/
ENV RAILS_LOG_TO_STDOUT true
ENV RAILS_PORT 3000

# copy entrypoint scripts and grant execution permissions
COPY ./dev-docker-entrypoint.sh /usr/local/bin/dev-entrypoint.sh
COPY ./test-docker-entrypoint.sh /usr/local/bin/test-entrypoint.sh
RUN chmod +x /usr/local/bin/dev-entrypoint.sh && chmod +x /usr/local/bin/test-entrypoint.sh

# install dependencies for application
RUN apk -U add --no-cache \
build-base \
git \
postgresql-dev \
postgresql-client \
libxml2-dev \
libxslt-dev \
nodejs \
yarn \
imagemagick \
tzdata \
less \
&& rm -rf /var/cache/apk/* \
&& mkdir -p $APP_PATH


RUN gem install bundler --version "$BUNDLE_VERSION" \
&& rm -rf $GEM_HOME/cache/*

# navigate to app directory
WORKDIR $APP_PATH

EXPOSE $RAILS_PORT

ENTRYPOINT [ "bundle", "exec" ]

Database.yml

default: &default
  adapter: postgresql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: bizzy_listings_development

# 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: bizzy_listings_test

production:
  <<: *default
  database: bizzy_listings_production

However, when I run the compose-up and access the app it says No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Not sure what's wrong with my app (postgre db)

Sorry but it's my first time to work with docker so appreciate if there is any manual I can read on this

Right, so in your postgre container configuration you have:

ports:
  - 5099:5432

This means that you forward traffic from container's port 5432 to your host at port 5099. In the meantime, your database configuration specifies no connection details to the database, so pg adapter will use default of localhost:5432 .

I'd strongly suggest making your config/database.yml much ore flexible with:

default: &default
  adapter: postgresql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
  url: <%= ENV.fetch("DATABASE_URL", "postgres://localhost:5432") %>

With the above, you con configure your connection using environment variable. Now, all you need to do is to set that variable in your application configuration:

  bizzy_listings_app:
    build:
      context: .
      dockerfile: Dockerfile.dev
    (...)
    environment:
      RAILS_ENV: development
      DATABASE_URL: postgres://postgres:password@bizzy_listings_db

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM