docker 服务器是否在本地运行并接受Unix域套接字“/tmp/.s.PGSQL.5432”上的连接?

d4so4syb  于 2022-12-11  发布在  Docker
关注(0)|答案(1)|浏览(261)

Docker撰写

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

停靠文件

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

但是,当我运行合成并访问应用程序时,它显示No such file or directory服务器是否在本地运行并接受Unix域套接字“/tmp/.s.PGSQL.5432”上的连接?
不确定我的应用程序有什么问题(postgre db)
对不起,但这是我第一次与码头工作,所以感谢,如果有任何手册,我可以阅读这方面的

kwvwclae

kwvwclae1#

好的,在postgre容器配置中,您有:

ports:
  - 5099:5432

这意味着您将通信从容器的端口5432转发到主机的端口5099。同时,您的数据库配置未指定数据库的连接详细信息,因此pg适配器将使用默认值localhost:5432
我强烈建议让config/database.yml更加灵活:

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

通过上面的步骤,你可以使用环境变量来配置你的连接。现在,你需要做的就是在你的应用程序配置中设置这个变量:

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

相关问题